NumeramaBridge.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. $this->update = '2016-07-19';
  9. }
  10. public function collectData(array $param) {
  11. function NumeramaStripCDATA($string) {
  12. $string = str_replace('<![CDATA[', '', $string);
  13. $string = str_replace(']]>', '', $string);
  14. return $string;
  15. }
  16. $feed = $this->getURI().'feed/';
  17. $html = $this->file_get_html($feed) or $this->returnError('Could not request Numerama: '.$feed, 500);
  18. $limit = 0;
  19. foreach($html->find('item') as $element) {
  20. if($limit < 5) {
  21. $item = new \Item();
  22. $item->title = html_entity_decode(NumeramaStripCDATA($element->find('title', 0)->innertext));
  23. $item->author = NumeramaStripCDATA($element->find('dc:creator', 0)->innertext);
  24. $item->uri = NumeramaStripCDATA($element->find('guid', 0)->plaintext);
  25. $item->timestamp = strtotime($element->find('pubDate', 0)->plaintext);
  26. $article_url = NumeramaStripCDATA($element->find('guid', 0)->plaintext);
  27. $article_html = $this->file_get_html($article_url) or $this->returnError('Could not request Numerama: '.$article_url, 500);
  28. $contents = $article_html->find('section[class=related-article]', 0)->innertext = ''; // remove related articles block
  29. $contents = '<img alt="" style="max-width:300px;" src="'.$article_html->find('meta[property=og:image]', 0)->getAttribute('content').'">'; // add post picture
  30. $contents = $contents.$article_html->find('article[class=post-content]', 0)->innertext; // extract the post
  31. $item->content = $contents;
  32. $this->items[] = $item;
  33. $limit++;
  34. }
  35. }
  36. }
  37. public function getName() {
  38. return 'Numerama';
  39. }
  40. public function getURI() {
  41. return 'http://www.numerama.com/';
  42. }
  43. public function getCacheDuration() {
  44. return 1800; // 30min
  45. }
  46. }