CNETBridge.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class CNETBridge extends BridgeAbstract {
  3. const MAINTAINER = 'ORelio';
  4. const NAME = 'CNET News';
  5. const URI = 'http://www.cnet.com/';
  6. const CACHE_TIMEOUT = 1800; // 30min
  7. const DESCRIPTION = 'Returns the newest articles. <br /> You may specify a
  8. topic found in some section URLs, else all topics are selected.';
  9. const PARAMETERS = array( array(
  10. 'topic' => array(
  11. 'name' => 'Topic name'
  12. )
  13. ));
  14. public function collectData(){
  15. function extractFromDelimiters($string, $start, $end){
  16. if(strpos($string, $start) !== false) {
  17. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  18. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  19. return $section_retrieved;
  20. }
  21. return false;
  22. }
  23. function stripWithDelimiters($string, $start, $end){
  24. while(strpos($string, $start) !== false) {
  25. $section_to_remove = substr($string, strpos($string, $start));
  26. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  27. $string = str_replace($section_to_remove, '', $string);
  28. }
  29. return $string;
  30. }
  31. function cleanArticle($article_html){
  32. $article_html = '<p>' . substr($article_html, strpos($article_html, '<p>') + 3);
  33. $article_html = stripWithDelimiters($article_html, '<span class="credit">', '</span>');
  34. $article_html = stripWithDelimiters($article_html, '<script', '</script>');
  35. $article_html = stripWithDelimiters($article_html, '<div class="shortcode related-links', '</div>');
  36. $article_html = stripWithDelimiters($article_html, '<a class="clickToEnlarge">', '</a>');
  37. return $article_html;
  38. }
  39. $pageUrl = self::URI . (empty($this->getInput('topic')) ? '' : 'topics/' . $this->getInput('topic') . '/');
  40. $html = getSimpleHTMLDOM($pageUrl) or returnServerError('Could not request CNET: ' . $pageUrl);
  41. $limit = 0;
  42. foreach($html->find('div.assetBody') as $element) {
  43. if($limit < 8) {
  44. $article_title = trim($element->find('h2', 0)->plaintext);
  45. $article_uri = self::URI . ($element->find('a', 0)->href);
  46. $article_timestamp = strtotime($element->find('time.assetTime', 0)->plaintext);
  47. $article_author = trim($element->find('a[rel=author]', 0)->plaintext);
  48. if(!empty($article_title) && !empty($article_uri) && strpos($article_uri, '/news/') !== false) {
  49. $article_html = getSimpleHTMLDOM($article_uri)
  50. or returnServerError('Could not request CNET: ' . $article_uri);
  51. $article_content = trim(
  52. cleanArticle(
  53. extractFromDelimiters(
  54. $article_html,
  55. '<div class="articleContent',
  56. '<footer>'
  57. )
  58. )
  59. );
  60. $item = array();
  61. $item['uri'] = $article_uri;
  62. $item['title'] = $article_title;
  63. $item['author'] = $article_author;
  64. $item['timestamp'] = $article_timestamp;
  65. $item['content'] = $article_content;
  66. $this->items[] = $item;
  67. $limit++;
  68. }
  69. }
  70. }
  71. }
  72. public function getName(){
  73. if(!is_null($this->getInput('topic'))) {
  74. $topic = $this->getInput('topic');
  75. return 'CNET News Bridge' . (empty($topic) ? '' : ' - ' . $topic);
  76. }
  77. return parent::getName();
  78. }
  79. }