NasaApodBridge.php 1.3 KB

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