NasaApodBridge.php 1.4 KB

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