2015-10-22 14:51:56 +02:00
|
|
|
<?php
|
|
|
|
class CNETBridge extends BridgeAbstract {
|
|
|
|
|
|
|
|
private $topicName = '';
|
|
|
|
|
2016-01-23 21:45:16 +01:00
|
|
|
public function loadMetadatas() {
|
|
|
|
|
|
|
|
$this->maintainer = 'ORelio';
|
|
|
|
$this->name = 'CNET News';
|
|
|
|
$this->uri = 'http://www.cnet.com/';
|
|
|
|
$this->description = 'Returns the newest articles. <br /> You may specify a topic found in some section URLs, else all topics are selected.';
|
2016-02-13 13:52:07 +01:00
|
|
|
$this->update = '2016-02-06';
|
2016-01-23 21:45:16 +01:00
|
|
|
|
|
|
|
$this->parameters[] =
|
|
|
|
'[
|
|
|
|
{
|
|
|
|
"name" : "Topic name",
|
|
|
|
"identifier" : "topic"
|
|
|
|
}
|
|
|
|
]';
|
|
|
|
}
|
2015-11-05 16:50:18 +01:00
|
|
|
|
2015-10-22 14:51:56 +02:00
|
|
|
public function collectData(array $param) {
|
|
|
|
|
|
|
|
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;
|
|
|
|
} return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
} return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function CleanArticle($article_html) {
|
2016-01-23 21:45:16 +01:00
|
|
|
$article_html = '<p>'.substr($article_html, strpos($article_html, '<p>') + 3);
|
|
|
|
$article_html = StripWithDelimiters($article_html, '<span class="credit">', '</span>');
|
2016-02-13 13:52:07 +01:00
|
|
|
$article_html = StripWithDelimiters($article_html, '<script', '</script>');
|
2015-10-22 14:51:56 +02:00
|
|
|
$article_html = StripWithDelimiters($article_html, '<div class="shortcode related-links', '</div>');
|
|
|
|
$article_html = StripWithDelimiters($article_html, '<a class="clickToEnlarge">', '</a>');
|
|
|
|
return $article_html;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($param['topic']))
|
|
|
|
$this->topicName = $param['topic'];
|
|
|
|
|
|
|
|
$pageUrl = 'http://www.cnet.com/'.(empty($this->topicName) ? '' : 'topics/'.$this->topicName.'/');
|
2015-10-22 15:20:42 +02:00
|
|
|
$html = file_get_html($pageUrl) or $this->returnError('Could not request CNET: '.$pageUrl, 500);
|
2015-10-22 14:51:56 +02:00
|
|
|
$limit = 0;
|
|
|
|
|
2016-01-23 21:45:16 +01:00
|
|
|
foreach($html->find('div.assetBody') as $element) {
|
|
|
|
if ($limit < 8) {
|
2015-10-22 14:51:56 +02:00
|
|
|
|
2016-01-23 21:45:16 +01:00
|
|
|
$article_title = trim($element->find('h2', 0)->plaintext);
|
|
|
|
$article_uri = 'http://www.cnet.com'.($element->find('a', 0)->href);
|
|
|
|
$article_thumbnail = $element->parent()->find('img', 0)->src;
|
|
|
|
$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
|
|
|
|
|
|
|
if (!empty($article_title) && !empty($article_uri) && strpos($article_uri, '/news/') !== false) {
|
|
|
|
|
2015-10-22 15:20:42 +02:00
|
|
|
$article_html = file_get_html($article_uri) or $this->returnError('Could not request CNET: '.$article_uri, 500);
|
2016-01-23 21:45:16 +01:00
|
|
|
|
|
|
|
if (is_null($article_thumbnail))
|
|
|
|
$article_thumbnail = $article_html->find('div.originalImage', 0);
|
2015-10-22 14:51:56 +02:00
|
|
|
|
|
|
|
if (is_null($article_thumbnail))
|
|
|
|
$article_thumbnail = $article_html->find('span.imageContainer', 0);
|
|
|
|
|
2016-01-23 21:45:16 +01:00
|
|
|
if (is_object($article_thumbnail))
|
2015-10-22 14:51:56 +02:00
|
|
|
$article_thumbnail = $article_thumbnail->find('img', 0)->src;
|
2016-01-23 21:45:16 +01:00
|
|
|
|
2015-10-22 14:51:56 +02:00
|
|
|
$article_content = trim(CleanArticle(ExtractFromDelimiters($article_html, '</div></div></div><div class="col-8">', '<footer>')));
|
|
|
|
|
|
|
|
$item = new \Item();
|
|
|
|
$item->uri = $article_uri;
|
|
|
|
$item->thumbnailUri = $article_thumbnail;
|
|
|
|
$item->title = $article_title;
|
|
|
|
$item->author = $article_author;
|
|
|
|
$item->timestamp = $article_timestamp;
|
|
|
|
$item->content = $article_content;
|
|
|
|
$this->items[] = $item;
|
|
|
|
$limit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'CNET News Bridge'.(empty($this->topicName) ? '' : ' - '.$this->topicName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURI() {
|
|
|
|
return 'http://www.cnet.com/';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheDuration() {
|
|
|
|
return 1800; // 30 minutes
|
|
|
|
// return 0;
|
|
|
|
}
|
|
|
|
}
|