NasaApodBridge.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. class NasaApodBridge extends BridgeAbstract{
  3. public function loadMetadatas() {
  4. $this->maintainer = "corenting";
  5. $this->name = "NASA APOD Bridge";
  6. $this->uri = "http://apod.nasa.gov/apod/astropix.html";
  7. $this->description = "Returns the 3 latest NASA APOD pictures and explanations";
  8. }
  9. public function collectData(array $param) {
  10. $html = $this->getSimpleHTMLDOM('http://apod.nasa.gov/apod/archivepix.html') or $this->returnServerError('Error while downloading the website content');
  11. $list = explode("<br>", $html->find('b', 0)->innertext);
  12. for($i = 0; $i < 3;$i++)
  13. {
  14. $line = $list[$i];
  15. $item = array();
  16. $uri_page = $html->find('a',$i + 3)->href;
  17. $uri = 'http://apod.nasa.gov/apod/'.$uri_page;
  18. $item['uri'] = $uri;
  19. $picture_html = $this->getSimpleHTMLDOM($uri);
  20. $picture_html_string = $picture_html->innertext;
  21. //Extract image and explanation
  22. $media = $picture_html->find('p',1)->innertext;
  23. $media = strstr($media, '<br>');
  24. $media = preg_replace('/<br>/', '', $media, 1);
  25. $explanation = $picture_html->find('p',2)->innertext;
  26. //Extract date from the picture page
  27. $date = explode(" ", $picture_html->find('p',1)->innertext);
  28. $item['timestamp'] = strtotime($date[4].$date[3].$date[2]);
  29. //Other informations
  30. $item['content'] = $media.'<br />'.$explanation;
  31. $item['title'] = $picture_html->find('b',0)->innertext;
  32. $this->items[] = $item;
  33. }
  34. }
  35. public function getCacheDuration(){
  36. return 3600*12; // 12 hours
  37. }
  38. }