CNETBridge.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 topic found in some section URLs, else all topics are selected.';
  8. const PARAMETERS = array( array(
  9. 'topic'=>array('name'=>'Topic name')
  10. ));
  11. public function collectData(){
  12. function ExtractFromDelimiters($string, $start, $end) {
  13. if (strpos($string, $start) !== false) {
  14. $section_retrieved = substr($string, strpos($string, $start) + strlen($start));
  15. $section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
  16. return $section_retrieved;
  17. } return false;
  18. }
  19. function StripWithDelimiters($string, $start, $end) {
  20. while (strpos($string, $start) !== false) {
  21. $section_to_remove = substr($string, strpos($string, $start));
  22. $section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
  23. $string = str_replace($section_to_remove, '', $string);
  24. } return $string;
  25. }
  26. function CleanArticle($article_html) {
  27. $article_html = '<p>'.substr($article_html, strpos($article_html, '<p>') + 3);
  28. $article_html = StripWithDelimiters($article_html, '<span class="credit">', '</span>');
  29. $article_html = StripWithDelimiters($article_html, '<script', '</script>');
  30. $article_html = StripWithDelimiters($article_html, '<div class="shortcode related-links', '</div>');
  31. $article_html = StripWithDelimiters($article_html, '<a class="clickToEnlarge">', '</a>');
  32. return $article_html;
  33. }
  34. $pageUrl = self::URI.(empty($this->getInput('topic')) ? '' : 'topics/'.$this->getInput('topic').'/');
  35. $html = getSimpleHTMLDOM($pageUrl) or returnServerError('Could not request CNET: '.$pageUrl);
  36. $limit = 0;
  37. foreach($html->find('div.assetBody') as $element) {
  38. if ($limit < 8) {
  39. $article_title = trim($element->find('h2', 0)->plaintext);
  40. $article_uri = self::URI.($element->find('a', 0)->href);
  41. $article_timestamp = strtotime($element->find('time.assetTime', 0)->plaintext);
  42. $article_author = trim($element->find('a[rel=author]', 0)->plaintext);
  43. if (!empty($article_title) && !empty($article_uri) && strpos($article_uri, '/news/') !== false) {
  44. $article_html = getSimpleHTMLDOM($article_uri) or returnServerError('Could not request CNET: '.$article_uri);
  45. $article_content = trim(CleanArticle(ExtractFromDelimiters($article_html, '<div class="articleContent', '<footer>')));
  46. $item = array();
  47. $item['uri'] = $article_uri;
  48. $item['title'] = $article_title;
  49. $item['author'] = $article_author;
  50. $item['timestamp'] = $article_timestamp;
  51. $item['content'] = $article_content;
  52. $this->items[] = $item;
  53. $limit++;
  54. }
  55. }
  56. }
  57. }
  58. public function getName() {
  59. $topic=$this->getInput('topic');
  60. return 'CNET News Bridge'.(empty($topic) ? '' : ' - '.$topic);
  61. }
  62. }