TheOatMealBridge.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. define("THE_OATMEAL", "http://theoatmeal.com/");
  3. define("THE_OATMEAL_RSS", "http://feeds.feedburner.com/oatmealfeed");
  4. class TheOatmealBridge extends RssExpander{
  5. public function loadMetadatas() {
  6. $this->maintainer = "Riduidel";
  7. $this->name = "The Oatmeal";
  8. $this->uri = "http://theoatmeal.com/";
  9. $this->description = "Un petit site de dessins assez rigolos";
  10. }
  11. public function collectData(array $param){
  12. parent::collectExpandableDatas($param, THE_OATMEAL_RSS);
  13. }
  14. /**
  15. * Since the oatmeal produces a weird RSS feed, I have to fix it by loading the items separatly from the feed infos
  16. */
  17. protected function collect_RSS_2_0_data($rssContent) {
  18. $rssContent->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/");
  19. $rssHeaderContent = $rssContent->channel[0];
  20. $this->debugMessage("RSS content is ===========\n".var_export($rssHeaderContent, true)."===========");
  21. $this->load_RSS_2_0_feed_data($rssHeaderContent);
  22. foreach($rssContent->item as $item) {
  23. $this->debugMessage("parsing item ".var_export($item, true));
  24. $this->items[] = $this->parseRSSItem($item);
  25. }
  26. }
  27. protected function parseRSSItem($newsItem) {
  28. $namespaces = $newsItem->getNameSpaces(true);
  29. $dc = $newsItem->children($namespaces['dc']);
  30. $rdf = $newsItem->children($namespaces['rdf']);
  31. $item = array();
  32. $item['title'] = trim($newsItem->title);
  33. $this->debugMessage("browsing Oatmeal item ".var_export($newsItem, true));
  34. $item['uri']=(string) $newsItem->attributes($namespaces['rdf'])->about;
  35. // now load that uri from cache
  36. $this->debugMessage("now loading page ".$item['uri']);
  37. $articlePage = str_get_html($this->get_cached($item['uri']));
  38. $content = $articlePage->find('#comic', 0);
  39. if($content==null) {
  40. $content = $articlePage->find('#blog');
  41. }
  42. $item['content'] = $content->innertext;
  43. $this->debugMessage("dc content is ".var_export($dc, true));
  44. $item['author'] = (string) $dc->creator;
  45. $item['timestamp'] = DateTime::createFromFormat(DateTime::ISO8601, $dc->date)->getTimestamp();
  46. $this->debugMessage("writtem by ".$item['author']." on ".$item['timestamp']);
  47. return $item;
  48. }
  49. public function getCacheDuration(){
  50. return 7200; // 2h hours
  51. }
  52. }