NumeramaBridge.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. class NumeramaBridge extends BridgeAbstract{
  3. public function loadMetadatas() {
  4. $this->maintainer = 'mitsukarenai';
  5. $this->name = 'Numerama';
  6. $this->uri = 'http://www.numerama.com/';
  7. $this->description = 'Returns the 5 newest posts from Numerama (full text)';
  8. }
  9. public function collectData(array $param) {
  10. function NumeramaStripCDATA($string) {
  11. $string = str_replace('<![CDATA[', '', $string);
  12. $string = str_replace(']]>', '', $string);
  13. return $string;
  14. }
  15. $feed = $this->uri.'feed/';
  16. $html = $this->getSimpleHTMLDOM($feed) or $this->returnServerError('Could not request Numerama: '.$feed);
  17. $limit = 0;
  18. foreach($html->find('item') as $element) {
  19. if($limit < 5) {
  20. $item = array();
  21. $item['title'] = html_entity_decode(NumeramaStripCDATA($element->find('title', 0)->innertext));
  22. $item['author'] = NumeramaStripCDATA($element->find('dc:creator', 0)->innertext);
  23. $item['uri'] = NumeramaStripCDATA($element->find('guid', 0)->plaintext);
  24. $item['timestamp'] = strtotime($element->find('pubDate', 0)->plaintext);
  25. $article_url = NumeramaStripCDATA($element->find('guid', 0)->plaintext);
  26. $article_html = $this->getSimpleHTMLDOM($article_url) or $this->returnServerError('Could not request Numerama: '.$article_url);
  27. $contents = $article_html->find('section[class=related-article]', 0)->innertext = ''; // remove related articles block
  28. $contents = '<img alt="" style="max-width:300px;" src="'.$article_html->find('meta[property=og:image]', 0)->getAttribute('content').'">'; // add post picture
  29. $contents = $contents.$article_html->find('article[class=post-content]', 0)->innertext; // extract the post
  30. $item['content'] = $contents;
  31. $this->items[] = $item;
  32. $limit++;
  33. }
  34. }
  35. }
  36. public function getCacheDuration() {
  37. return 1800; // 30min
  38. }
  39. }