NumeramaBridge.php 1.8 KB

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