2015-10-22 14:51:56 +02:00
|
|
|
<?php
|
|
|
|
class CNETBridge extends BridgeAbstract {
|
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const MAINTAINER = 'ORelio';
|
|
|
|
const NAME = 'CNET News';
|
|
|
|
const URI = 'http://www.cnet.com/';
|
|
|
|
const CACHE_TIMEOUT = 1800; // 30min
|
|
|
|
const DESCRIPTION = 'Returns the newest articles. <br /> You may specify a
|
|
|
|
topic found in some section URLs, else all topics are selected.';
|
2016-01-23 21:45:16 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
const PARAMETERS = array( array(
|
|
|
|
'topic' => array(
|
|
|
|
'name' => 'Topic name'
|
|
|
|
)
|
|
|
|
));
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function collectData(){
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
function extractFromDelimiters($string, $start, $end){
|
|
|
|
if(strpos($string, $start) !== false){
|
|
|
|
$section_retrieved = substr($string, strpos($string, $start) + strlen($start));
|
|
|
|
$section_retrieved = substr($section_retrieved, 0, strpos($section_retrieved, $end));
|
|
|
|
return $section_retrieved;
|
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
function stripWithDelimiters($string, $start, $end){
|
|
|
|
while(strpos($string, $start) !== false){
|
|
|
|
$section_to_remove = substr($string, strpos($string, $start));
|
|
|
|
$section_to_remove = substr($section_to_remove, 0, strpos($section_to_remove, $end) + strlen($end));
|
|
|
|
$string = str_replace($section_to_remove, '', $string);
|
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
return $string;
|
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
function cleanArticle($article_html){
|
|
|
|
$article_html = '<p>' . substr($article_html, strpos($article_html, '<p>') + 3);
|
|
|
|
$article_html = stripWithDelimiters($article_html, '<span class="credit">', '</span>');
|
|
|
|
$article_html = stripWithDelimiters($article_html, '<script', '</script>');
|
|
|
|
$article_html = stripWithDelimiters($article_html, '<div class="shortcode related-links', '</div>');
|
|
|
|
$article_html = stripWithDelimiters($article_html, '<a class="clickToEnlarge">', '</a>');
|
|
|
|
return $article_html;
|
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$pageUrl = self::URI . (empty($this->getInput('topic')) ? '' : 'topics/' . $this->getInput('topic') . '/');
|
|
|
|
$html = getSimpleHTMLDOM($pageUrl) or returnServerError('Could not request CNET: ' . $pageUrl);
|
|
|
|
$limit = 0;
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
foreach($html->find('div.assetBody') as $element){
|
|
|
|
if($limit < 8){
|
|
|
|
$article_title = trim($element->find('h2', 0)->plaintext);
|
|
|
|
$article_uri = self::URI . ($element->find('a', 0)->href);
|
|
|
|
$article_timestamp = strtotime($element->find('time.assetTime', 0)->plaintext);
|
|
|
|
$article_author = trim($element->find('a[rel=author]', 0)->plaintext);
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
if(!empty($article_title) && !empty($article_uri) && strpos($article_uri, '/news/') !== false){
|
|
|
|
$article_html = getSimpleHTMLDOM($article_uri)
|
|
|
|
or returnServerError('Could not request CNET: ' . $article_uri);
|
|
|
|
$article_content = trim(
|
|
|
|
cleanArticle(
|
|
|
|
extractFromDelimiters(
|
|
|
|
$article_html,
|
|
|
|
'<div class="articleContent',
|
|
|
|
'<footer>'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2016-01-23 21:45:16 +01:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
$item = array();
|
|
|
|
$item['uri'] = $article_uri;
|
|
|
|
$item['title'] = $article_title;
|
|
|
|
$item['author'] = $article_author;
|
|
|
|
$item['timestamp'] = $article_timestamp;
|
|
|
|
$item['content'] = $article_content;
|
|
|
|
$this->items[] = $item;
|
|
|
|
$limit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2017-02-11 16:16:56 +01:00
|
|
|
public function getName(){
|
2017-02-14 22:20:55 +01:00
|
|
|
if(!is_null($this->getInput('topic'))){
|
|
|
|
$topic = $this->getInput('topic');
|
|
|
|
return 'CNET News Bridge' . (empty($topic) ? '' : ' - ' . $topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
2017-02-11 16:16:56 +01:00
|
|
|
}
|
2015-10-22 14:51:56 +02:00
|
|
|
}
|