array(
'name' => 'News Feed',
'type' => 'list',
'title' => 'Feeds from Reuters U.S/International edition',
'values' => array(
'Top News' => 'home/topnews',
'Fact Check' => 'chan:abtpk0vm',
'Entertainment' => 'chan:8ym8q8dl',
'Politics' => 'politics',
'Wire' => 'wire',
'Breakingviews' => '/breakingviews',
'World' => array(
'World' => 'world',
'Africa' => '/world/africa',
'Americas' => '/world/americas',
'Asia-Pacific' => '/world/asia-pacific',
'China' => 'china',
'europe' => '/world/europe',
'India' => '/world/india',
'Middle East' => '/world/middle-east',
'UK' => 'chan:61leiu7j',
'USA News' => 'us',
'The Great Reboot' => '/world/the-great-reboot',
'Reuters Next' => '/world/reuters-next'
),
'Business' => array(
'Business' => 'business',
'Aerospace and Defense' => 'aerospace',
'Autos Transportation' => '/business/autos-transportation',
'Energy' => 'energy',
'Finance' => '/business/finance',
'Health' => 'chan:8hw7807a',
'Media Telecom' => '/business/media-telecom',
'Retail Consumer' => '/business/retail-consumer',
'Sustainable Business' => '/business/sustainable-business',
'Change Suite' => '/business/change-suite',
'Future of Health' => '/business/future-of-health',
'Future of Money' => '/business/future-of-money',
'Take Five' => '/business/take-five',
'Reuters Impact' => '/business/reuters-impact',
),
'Legal' => array(
'Legal' => '/legal',
'Government' => '/legal/government',
'Legal Industry' => '/legal/legalindustry',
'Litigation' => '/legal/litigation',
'Transactional' => '/legal/transactional',
),
'Markets' => array(
'Markets' => 'markets',
'Asian Markets' => '/markets/asia',
'Commodities' => '/markets/commodities',
'Currencies' => '/markets/currencies',
'Deals' => '/markets/deals',
'European Markets' => '/markets/europe',
'Funds' => '/markets/fund',
'Global Market Data' => '/markets/global-market-data',
'Rates & Bonds' => '/markets/rates-bonds',
'Stocks' => '/markets/stocks',
'U.S Markets' => '/markets/us',
'Wealth' => '/markets/wealth',
'Macro Matters' => '/markets/macromatters',
),
'Technology' => array(
'Technology' => 'tech',
'Disrupted' => '/technology/disrupted',
'Reuters Momentum' => '/technology/reuters-momentum',
),
'Sports' => array(
'Sports' => 'sports',
'Athletics' => '/lifestyle/sports/athletics',
'Cricket' => '/lifestyle/sports/cricket',
'Cycling' => '/lifestyle/sports/cycling',
'Golf' => '/lifestyle/sports/golf',
'Motor Sports' => '/lifestyle/sports/motor-sports',
'Soccer' => '/lifestyle/sports/soccer',
'Tennis' => '/lifestyle/sports/tennis',
),
'Lifestyle' => array(
'Lifestyle' => 'life',
'Oddly Enough' => '/lifestyle/oddly-enough',
'Science' => 'science',
)
)
)
)
);
const BACKWARD_COMPATIBILITY = array(
'world' => '/world',
'china' => '/world/china',
'chan:61leiu7j' => '/world/uk',
'us' => '/world/us',
'business' => '/business',
'aerospace' => '/business/aerospace-defense',
'energy' => '/business/energy',
'environment' => '/business/environment',
'chan:8hw7807a' => '/business/healthcare-pharmaceuticals',
'markets' => '/markets',
'tech' => '/technology',
'sports' => '/lifestyle/sports',
'life' => '/lifestyle',
'science' => '/lifestyle/science',
);
const OLD_WIRE_SECTION = array(
'home/topnews',
'chan:abtpk0vm',
'chan:8ym8q8dl',
'politics',
'wire'
);
/**
* Performs an HTTP request to the Reuters API and returns decoded JSON
* in the form of an associative array
* @param string $feed_uri Full API URL to fetch data
* @return array
*/
private function getJson($uri)
{
$returned_data = getContents($uri);
return json_decode($returned_data, true);
}
/**
* Takes in data from Reuters Wire API and
* creates structured data in the form of a list
* of story information.
* @param array $data JSON collected from the Reuters Wire API
*/
private function processData($data)
{
/**
* Gets a list of wire items which are groups of templates
*/
$reuters_allowed_wireitems = array_filter(
$data, function ($wireitem) {
return in_array(
$wireitem['wireitem_type'],
self::ALLOWED_WIREITEM_TYPES
);
}
);
/*
* Gets a list of "Templates", which is data containing a story
*/
$reuters_wireitem_templates = array_reduce(
$reuters_allowed_wireitems,
function (array $carry, array $wireitem) {
$wireitem_templates = $wireitem['templates'];
return array_merge(
$carry,
array_filter(
$wireitem_templates, function (
array $template_data
) {
return in_array(
$template_data['type'],
self::ALLOWED_TEMPLATE_TYPES
);
}
)
);
},
array()
);
return $reuters_wireitem_templates;
}
private function getSectionEndpoint() {
$endpoint = $this->getInput('feed');
if(isset(self::BACKWARD_COMPATIBILITY[$endpoint])) {
$endpoint = self::BACKWARD_COMPATIBILITY[$endpoint];
} elseif (in_array($endpoint, self::OLD_WIRE_SECTION)) {
$this->useWireAPI = true;
}
return $endpoint;
}
/**
* @param string $endpoint - Provide section's endpoint to Reuters API.
* @param string $fetch_type - Provide what kind of fetch do you want? Article or Section.
* @return string A completed API URL to fetch data
*/
private function getAPIURL($endpoint, $fetch_type) {
$base_url = self::URI . '/pf/api/v3/content/fetch/';
$wire_url = 'https://wireapi.reuters.com/v8';
switch($fetch_type) {
case 'article':
if($this->useWireAPI) {
return $wire_url . $endpoint;
}
$query = array(
'website_url' => $endpoint,
'website' => 'reuters'
);
$json_query = json_encode($query);
return $base_url . 'article-by-id-or-url-v1?query=' . $json_query;
break;
case 'section':
if($this->useWireAPI) {
if(strpos($endpoint, 'chan:') !== false) {
// Now checking whether that feed has unique ID or not.
$feed_uri = "/feed/rapp/us/wirefeed/$endpoint";
} else {
$feed_uri = "/feed/rapp/us/tabbar/feeds/$endpoint";
}
return $wire_url . $feed_uri;
}
$query = array(
'fetch_type' => 'section',
'section_id' => $endpoint,
'size' => 30,
'website' => 'reuters'
);
$json_query = json_encode($query);
return $base_url . 'articles-by-section-alias-or-id-v1?query=' . $json_query;
break;
}
returnServerError('unsupported endpoint');
}
private function getArticle($feed_uri)
{
// This will make another request to API to get full detail of article and author's name.
$url = $this->getAPIURL($feed_uri, 'article');
$rawData = $this->getJson($url);
$article_content = '';
$authorlist = '';
$category = array();
$image_list = array();
$published_at = '';
if($this->useWireAPI) {
$reuters_wireitems = $rawData['wireitems'];
$processedData = $this->processData($reuters_wireitems);
$first = reset($processedData);
$article_content = $first['story']['body_items'];
$authorlist = $first['story']['authors'];
$category = array($first['story']['channel']['name']);
$image_list = $first['story']['images'];
$published_at = $first['story']['published_at'];
} else {
$article_content = $rawData['result']['content_elements'];
$authorlist = $rawData['result']['authors'];
$category = array($rawData['result']['taxonomy']['ads_primary_section']['name']);
$image_list = array();
if(!empty($rawData['result']['related_content']['galleries'])) {
$galleries = $rawData['result']['related_content']['galleries'];
foreach($galleries as $gallery) {
$image_list = array_merge($image_list, $gallery['content_elements']);
}
} else if(!empty($rawData['result']['related_content']['images'])) {
$image_list = $rawData['result']['related_content']['images'];
}
$published_at = $rawData['result']['published_time'];
}
$content_detail = array(
'content' => $this->handleArticleContent($article_content),
'author' => $this->handleAuthorName($authorlist),
'category' => $category,
'images' => $this->handleImage($image_list),
'published_at' => $published_at
);
return $content_detail;
}
private function handleImage($images) {
$img_placeholder = '';
foreach($images as $image) { // Add more image to article.
$image_url = $image['url'];
$image_caption = $image['caption'];
$image_alt_text = '';
if(isset($image['alt_text'])) {
$image_alt_text = $image['alt_text'];
} else {
$image_alt_text = $image_caption;
}
$img = "";
$img_caption = "
$data
"; break; case 'heading': $description = $description . "'; foreach ($item_list as $item) { if($item['type'] == 'text') { $description = $description . $item['content']; } else { $description = $description . $item['symbol']; } } $description = $description . '
'; break; case 'p_table': $description = $description . $content['content']; break; case 'upstream_embed': $media_type = $content['media_type']; $cid = $content['cid']; $embed = ''; switch ($media_type) { case 'tweet': try { $tweet_url = "https://twitter.com/dummyname/statuses/$cid"; $get_embed_url = 'https://publish.twitter.com/oembed?url=' . urlencode($tweet_url) . '&partner=&hide_thread=false'; $oembed_json = json_decode(getContents($get_embed_url), true); $embed .= $oembed_json['html']; } catch (Exception $e) { // In case not found any tweet. $embed .= ''; } break; case 'instagram': $url = "https://instagram.com/p/$cid/media/?size=l"; $embed .= <<' . $header . ' | '; } $tr .= '
---|
' . $data . ' | '; } $tr .= '