maintainer = "ORelio";
$this->name = "CNET News";
$this->uri = "http://www.cnet.com/";
$this->description = "Returns the newest articles.
You may specify a topic, else all topics are selected.";
$this->update = "2015-09-10";
$this->parameters[] =
'[
{
"name" : "Topic name",
"identifier" : "topic"
}
]';
}
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) {
$article_html = '
'.substr($article_html, strpos($article_html, '
') + 18);
$article_html = StripWithDelimiters($article_html, '');
$article_html = StripWithDelimiters($article_html, '
', '');
return $article_html;
}
if (!empty($param['topic']))
$this->topicName = $param['topic'];
$pageUrl = 'http://www.cnet.com/'.(empty($this->topicName) ? '' : 'topics/'.$this->topicName.'/');
$html = file_get_html($pageUrl) or $this->returnError('Could not request CNET: '.$pageUrl, 500);
$limit = 0;
foreach($html->find('div.socialSharingSmall') as $element) {
if ($limit < 4) {
$article_meta = json_decode(ExtractFromDelimiters($element->outertext, 'data-social-counts-options=\'', '\'>'));
$article_title = $article_meta->title;
$article_uri = $article_meta->url;
if (!empty($article_title) && !empty($article_uri) && strpos($article_uri, '/news/') !== false) {
$article_html = file_get_html($article_uri) or $this->returnError('Could not request CNET: '.$article_uri, 500);
$article_timestamp = strtotime(ExtractFromDelimiters($article_html->innertext, '
', '