CNETBridge.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. class CNETBridge extends BridgeAbstract {
  3. private $topicName = '';
  4. public function loadMetadatas() {
  5. $this->maintainer = 'ORelio';
  6. $this->name = 'CNET News';
  7. $this->uri = 'http://www.cnet.com/';
  8. $this->description = 'Returns the newest articles. <br /> You may specify a topic found in some section URLs, else all topics are selected.';
  9. $this->update = '2016-03-16';
  10. $this->parameters[] =
  11. '[
  12. {
  13. "name" : "Topic name",
  14. "identifier" : "topic"
  15. }
  16. ]';
  17. }
  18. public function collectData(array $param) {
  19. function ExtractFromDelimiters($string, $start, $end) {
  20. if (strpos($string, $start) !== false) {
  21. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  22. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  23. return $section_retrieved;
  24. } return false;
  25. }
  26. function StripWithDelimiters($string, $start, $end) {
  27. while (strpos($string, $start) !== false) {
  28. $section_to_remove = substr($string, strpos($string, $start));
  29. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  30. $string = str_replace($section_to_remove, '', $string);
  31. } return $string;
  32. }
  33. function CleanArticle($article_html) {
  34. $article_html = '<p>'.substr($article_html, strpos($article_html, '<p>') + 3);
  35. $article_html = StripWithDelimiters($article_html, '<span class="credit">', '</span>');
  36. $article_html = StripWithDelimiters($article_html, '<script', '</script>');
  37. $article_html = StripWithDelimiters($article_html, '<div class="shortcode related-links', '</div>');
  38. $article_html = StripWithDelimiters($article_html, '<a class="clickToEnlarge">', '</a>');
  39. return $article_html;
  40. }
  41. if (!empty($param['topic']))
  42. $this->topicName = $param['topic'];
  43. $pageUrl = 'http://www.cnet.com/'.(empty($this->topicName) ? '' : 'topics/'.$this->topicName.'/');
  44. $html = $this->file_get_html($pageUrl) or $this->returnError('Could not request CNET: '.$pageUrl, 500);
  45. $limit = 0;
  46. foreach($html->find('div.assetBody') as $element) {
  47. if ($limit < 8) {
  48. $article_title = trim($element->find('h2', 0)->plaintext);
  49. $article_uri = 'http://www.cnet.com'.($element->find('a', 0)->href);
  50. $article_thumbnail = $element->parent()->find('img', 0)->src;
  51. $article_timestamp = strtotime($element->find('time.assetTime', 0)->plaintext);
  52. $article_author = trim($element->find('a[rel=author]', 0)->plaintext);
  53. if (!empty($article_title) && !empty($article_uri) && strpos($article_uri, '/news/') !== false) {
  54. $article_html = $this->file_get_html($article_uri) or $this->returnError('Could not request CNET: '.$article_uri, 500);
  55. if (is_null($article_thumbnail))
  56. $article_thumbnail = $article_html->find('div.originalImage', 0);
  57. if (is_null($article_thumbnail))
  58. $article_thumbnail = $article_html->find('span.imageContainer', 0);
  59. if (is_object($article_thumbnail))
  60. $article_thumbnail = $article_thumbnail->find('img', 0)->src;
  61. $article_content = trim(CleanArticle(ExtractFromDelimiters($article_html, '<div class="articleContent', '<footer>')));
  62. $item = new \Item();
  63. $item->uri = $article_uri;
  64. $item->thumbnailUri = $article_thumbnail;
  65. $item->title = $article_title;
  66. $item->author = $article_author;
  67. $item->timestamp = $article_timestamp;
  68. $item->content = $article_content;
  69. $this->items[] = $item;
  70. $limit++;
  71. }
  72. }
  73. }
  74. }
  75. public function getName() {
  76. return 'CNET News Bridge'.(empty($this->topicName) ? '' : ' - '.$this->topicName);
  77. }
  78. public function getURI() {
  79. return 'http://www.cnet.com/';
  80. }
  81. public function getCacheDuration() {
  82. return 1800; // 30 minutes
  83. // return 0;
  84. }
  85. }