NasaApodBridge.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. $this->update = "2014-08-27";
  9. }
  10. public function collectData(array $param) {
  11. $html = $this->file_get_html('http://apod.nasa.gov/apod/archivepix.html') or $this->returnError('Error while downloading the website content', 404);
  12. $list = explode("<br>", $html->find('b', 0)->innertext);
  13. for($i = 0; $i < 3;$i++)
  14. {
  15. $line = $list[$i];
  16. $item = new \Item();
  17. $uri_page = $html->find('a',$i + 3)->href;
  18. $uri = 'http://apod.nasa.gov/apod/'.$uri_page;
  19. $item->uri = $uri;
  20. $picture_html = $this->file_get_html($uri);
  21. $picture_html_string = $picture_html->innertext;
  22. //Extract image and explanation
  23. $media = $picture_html->find('p',1)->innertext;
  24. $media = strstr($media, '<br>');
  25. $media = preg_replace('/<br>/', '', $media, 1);
  26. $explanation = $picture_html->find('p',2)->innertext;
  27. //Extract date from the picture page
  28. $date = explode(" ", $picture_html->find('p',1)->innertext);
  29. $item->timestamp = strtotime($date[4].$date[3].$date[2]);
  30. //Other informations
  31. $item->content = $media.'<br />'.$explanation;
  32. $item->title = $picture_html->find('b',0)->innertext;
  33. $this->items[] = $item;
  34. }
  35. }
  36. public function getName(){
  37. return 'NASA APOD';
  38. }
  39. public function getURI(){
  40. return 'http://apod.nasa.gov/apod/astropix.html';
  41. }
  42. public function getCacheDuration(){
  43. return 3600*12; // 12 hours
  44. }
  45. }