2021-01-29 20:57:40 +03:00
|
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
class ReutersBridge extends BridgeAbstract
|
|
|
|
|
{
|
|
|
|
|
const MAINTAINER = 'hollowleviathan, spraynard, csisoap';
|
|
|
|
|
const NAME = 'Reuters Bridge';
|
2021-12-08 19:16:40 +03:00
|
|
|
|
const URI = 'https://www.reuters.com';
|
2021-01-29 20:57:40 +03:00
|
|
|
|
const CACHE_TIMEOUT = 1800; // 30min
|
|
|
|
|
const DESCRIPTION = 'Returns news from Reuters';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
private $feedName = self::NAME;
|
2021-12-08 19:16:40 +03:00
|
|
|
|
private $useWireAPI = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
/**
|
|
|
|
|
* Wireitem types allowed in the final story output
|
|
|
|
|
*/
|
|
|
|
|
const ALLOWED_WIREITEM_TYPES = [
|
|
|
|
|
'story',
|
|
|
|
|
'headlines'
|
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
/**
|
|
|
|
|
* Wireitem template types allowed in the final story output
|
|
|
|
|
*/
|
|
|
|
|
const ALLOWED_TEMPLATE_TYPES = [
|
2021-03-09 09:15:48 +03:00
|
|
|
|
'story',
|
|
|
|
|
'headlines'
|
2021-01-29 20:57:40 +03:00
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
const PARAMETERS = [
|
2022-07-01 16:10:30 +03:00
|
|
|
|
[
|
2021-01-29 20:57:40 +03:00
|
|
|
|
'feed' => [
|
|
|
|
|
'name' => 'News Feed',
|
|
|
|
|
'type' => 'list',
|
|
|
|
|
'title' => 'Feeds from Reuters U.S/International edition',
|
|
|
|
|
'values' => [
|
2021-12-08 19:16:40 +03:00
|
|
|
|
'Top News' => 'home/topnews',
|
2021-07-20 10:54:07 +03:00
|
|
|
|
'Fact Check' => 'chan:abtpk0vm',
|
2021-12-08 19:16:40 +03:00
|
|
|
|
'Entertainment' => 'chan:8ym8q8dl',
|
2021-01-29 20:57:40 +03:00
|
|
|
|
'Politics' => 'politics',
|
|
|
|
|
'Wire' => 'wire',
|
2021-12-08 19:16:40 +03:00
|
|
|
|
'Breakingviews' => '/breakingviews',
|
|
|
|
|
'World' => [
|
|
|
|
|
'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' => [
|
|
|
|
|
'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' => [
|
|
|
|
|
'Legal' => '/legal',
|
|
|
|
|
'Government' => '/legal/government',
|
|
|
|
|
'Legal Industry' => '/legal/legalindustry',
|
|
|
|
|
'Litigation' => '/legal/litigation',
|
|
|
|
|
'Transactional' => '/legal/transactional',
|
|
|
|
|
],
|
|
|
|
|
'Markets' => [
|
|
|
|
|
'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' => [
|
|
|
|
|
'Technology' => 'tech',
|
|
|
|
|
'Disrupted' => '/technology/disrupted',
|
|
|
|
|
'Reuters Momentum' => '/technology/reuters-momentum',
|
|
|
|
|
],
|
|
|
|
|
'Sports' => [
|
|
|
|
|
'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' => [
|
|
|
|
|
'Lifestyle' => 'life',
|
|
|
|
|
'Oddly Enough' => '/lifestyle/oddly-enough',
|
|
|
|
|
'Science' => 'science',
|
2022-07-01 16:10:30 +03:00
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
2021-01-29 20:57:40 +03:00
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
const BACKWARD_COMPATIBILITY = [
|
|
|
|
|
'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',
|
2022-03-28 21:34:41 +03:00
|
|
|
|
'home/topnews' => '/home',
|
2021-12-08 19:16:40 +03:00
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
const OLD_WIRE_SECTION = [
|
|
|
|
|
'home/topnews',
|
|
|
|
|
'chan:abtpk0vm',
|
|
|
|
|
'chan:8ym8q8dl',
|
|
|
|
|
'politics',
|
|
|
|
|
'wire'
|
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
},
|
2022-07-01 16:10:30 +03:00
|
|
|
|
[]
|
2021-01-29 20:57:40 +03:00
|
|
|
|
);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
return $reuters_wireitem_templates;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
/**
|
2022-03-28 21:34:41 +03:00
|
|
|
|
* @param string $endpoint - A endpoint is provided could be article URI or ID.
|
2021-12-08 19:16:40 +03:00
|
|
|
|
* @param string $fetch_type - Provide what kind of fetch do you want? Article or Section.
|
2022-03-28 21:34:41 +03:00
|
|
|
|
* @param boolean $is_article_uid {true|false} - A boolean flag to determined if using UID instead of url to fetch.
|
2021-12-08 19:16:40 +03:00
|
|
|
|
* @return string A completed API URL to fetch data
|
|
|
|
|
*/
|
2022-03-28 21:34:41 +03:00
|
|
|
|
private function getAPIURL($endpoint, $fetch_type, $is_article_uid = false)
|
|
|
|
|
{
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$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;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
$base_query = [
|
|
|
|
|
'website' => 'reuters',
|
2021-12-08 19:16:40 +03:00
|
|
|
|
];
|
2022-03-28 21:34:41 +03:00
|
|
|
|
$query = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
if ($is_article_uid) {
|
|
|
|
|
$query = [
|
|
|
|
|
'id' => $endpoint
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
$query = [
|
|
|
|
|
'website_url' => $endpoint,
|
|
|
|
|
];
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
$query = array_merge($base_query, $query);
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$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 = [
|
|
|
|
|
'section_id' => $endpoint,
|
|
|
|
|
'size' => 30,
|
|
|
|
|
'website' => 'reuters'
|
|
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
if ($endpoint != '/home') {
|
|
|
|
|
$query = array_merge($query, [
|
|
|
|
|
'fetch_type' => 'section',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$json_query = json_encode($query);
|
|
|
|
|
return $base_url . 'articles-by-section-alias-or-id-v1?query=' . $json_query;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
returnServerError('unsupported endpoint');
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
private function addStories($title, $content, $timestamp, $author, $url, $category)
|
|
|
|
|
{
|
|
|
|
|
$item = [];
|
|
|
|
|
$item['categories'] = $category;
|
|
|
|
|
$item['author'] = $author;
|
|
|
|
|
$item['content'] = $content;
|
|
|
|
|
$item['title'] = $title;
|
|
|
|
|
$item['timestamp'] = $timestamp;
|
|
|
|
|
$item['uri'] = $url;
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
private function getArticle($feed_uri, $is_article_uid = false)
|
2021-01-29 20:57:40 +03:00
|
|
|
|
{
|
|
|
|
|
// This will make another request to API to get full detail of article and author's name.
|
2022-03-28 21:34:41 +03:00
|
|
|
|
$url = $this->getAPIURL($feed_uri, 'article', $is_article_uid);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2023-07-29 01:14:30 +03:00
|
|
|
|
try {
|
|
|
|
|
$json = getContents($url);
|
|
|
|
|
$rawData = Json::decode($json);
|
|
|
|
|
} catch (\JsonException $e) {
|
|
|
|
|
return [
|
|
|
|
|
'content' => '',
|
|
|
|
|
'author' => '',
|
|
|
|
|
'category' => '',
|
|
|
|
|
'images' => '',
|
|
|
|
|
'published_at' => ''
|
|
|
|
|
];
|
|
|
|
|
}
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$article_content = '';
|
|
|
|
|
$authorlist = '';
|
|
|
|
|
$category = [];
|
|
|
|
|
$image_list = [];
|
|
|
|
|
$published_at = '';
|
|
|
|
|
if ($this->useWireAPI) {
|
|
|
|
|
$reuters_wireitems = $rawData['wireitems'];
|
|
|
|
|
$processedData = $this->processData($reuters_wireitems);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$first = reset($processedData);
|
|
|
|
|
$article_content = $first['story']['body_items'];
|
|
|
|
|
$authorlist = $first['story']['authors'];
|
|
|
|
|
$category = [$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 = [$rawData['result']['taxonomy']['ads_primary_section']['name']];
|
|
|
|
|
$image_list = [];
|
|
|
|
|
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']);
|
|
|
|
|
}
|
|
|
|
|
} elseif (!empty($rawData['result']['related_content']['images'])) {
|
|
|
|
|
$image_list = $rawData['result']['related_content']['images'];
|
|
|
|
|
}
|
|
|
|
|
$published_at = $rawData['result']['published_time'];
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-03-09 09:15:48 +03:00
|
|
|
|
$content_detail = [
|
|
|
|
|
'content' => $this->handleArticleContent($article_content),
|
|
|
|
|
'author' => $this->handleAuthorName($authorlist),
|
|
|
|
|
'category' => $category,
|
|
|
|
|
'images' => $this->handleImage($image_list),
|
2021-07-20 10:54:07 +03:00
|
|
|
|
'published_at' => $published_at
|
2021-03-09 09:15:48 +03:00
|
|
|
|
];
|
|
|
|
|
return $content_detail;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-03-09 09:15:48 +03:00
|
|
|
|
private function handleImage($images)
|
|
|
|
|
{
|
2021-01-29 20:57:40 +03:00
|
|
|
|
$img_placeholder = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2023-07-03 01:39:01 +03:00
|
|
|
|
foreach ($images as $image) {
|
|
|
|
|
// Add more image to article.
|
2021-01-29 20:57:40 +03:00
|
|
|
|
$image_url = $image['url'];
|
2023-07-03 01:39:01 +03:00
|
|
|
|
$image_caption = $image['caption'] ?? $image['alt_text'] ?? $image['subtitle'] ?? '';
|
2022-03-02 07:50:02 +03:00
|
|
|
|
$image_alt_text = '';
|
2023-07-03 01:39:01 +03:00
|
|
|
|
$image_alt_text = $image['alt_text'] ?? $image_caption;
|
2022-03-02 07:50:02 +03:00
|
|
|
|
$img = "<img src=\"$image_url\" alt=\"$image_alt_text\">";
|
2021-01-29 20:57:40 +03:00
|
|
|
|
$img_caption = "<figcaption style=\"text-align: center;\"><i>$image_caption</i></figcaption>";
|
|
|
|
|
$figure = "<figure>$img \t $img_caption</figure>";
|
|
|
|
|
$img_placeholder = $img_placeholder . $figure;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-03-09 09:15:48 +03:00
|
|
|
|
return $img_placeholder;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-03-09 09:15:48 +03:00
|
|
|
|
private function handleAuthorName($authors)
|
|
|
|
|
{
|
2021-01-29 20:57:40 +03:00
|
|
|
|
$author = '';
|
|
|
|
|
$counter = 0;
|
2021-03-09 09:15:48 +03:00
|
|
|
|
foreach ($authors as $data) {
|
2021-01-29 20:57:40 +03:00
|
|
|
|
//Formatting author's name.
|
|
|
|
|
$name = $data['name'];
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$counter++;
|
|
|
|
|
if ($counter == count($authors)) {
|
|
|
|
|
$author .= $name;
|
2021-01-29 20:57:40 +03:00
|
|
|
|
} else {
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$author .= $name . ', ';
|
2021-01-29 20:57:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 09:15:48 +03:00
|
|
|
|
return $author;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-03-09 09:15:48 +03:00
|
|
|
|
private function handleArticleContent($contents)
|
|
|
|
|
{
|
2021-01-29 20:57:40 +03:00
|
|
|
|
$description = '';
|
2021-03-09 09:15:48 +03:00
|
|
|
|
foreach ($contents as $content) {
|
2022-11-15 02:30:51 +03:00
|
|
|
|
$data = '';
|
2021-01-29 20:57:40 +03:00
|
|
|
|
if (isset($content['content'])) {
|
|
|
|
|
$data = $content['content'];
|
|
|
|
|
}
|
|
|
|
|
switch ($content['type']) {
|
|
|
|
|
case 'paragraph':
|
|
|
|
|
$description = $description . "<p>$data</p>";
|
|
|
|
|
break;
|
|
|
|
|
case 'heading':
|
|
|
|
|
$description = $description . "<h3>$data</h3>";
|
|
|
|
|
break;
|
|
|
|
|
case 'infographics':
|
|
|
|
|
$description = $description . "<img src=\"$data\">";
|
|
|
|
|
break;
|
|
|
|
|
case 'inline_items':
|
|
|
|
|
$item_list = $content['items'];
|
|
|
|
|
$description = $description . '<p>';
|
|
|
|
|
foreach ($item_list as $item) {
|
|
|
|
|
if ($item['type'] == 'text') {
|
|
|
|
|
$description = $description . $item['content'];
|
|
|
|
|
} else {
|
|
|
|
|
$description = $description . $item['symbol'];
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
}
|
2021-01-29 20:57:40 +03:00
|
|
|
|
$description = $description . '</p>';
|
|
|
|
|
break;
|
|
|
|
|
case 'p_table':
|
|
|
|
|
$description = $description . $content['content'];
|
|
|
|
|
break;
|
2021-07-20 10:54:07 +03:00
|
|
|
|
case 'upstream_embed':
|
|
|
|
|
$media_type = $content['media_type'];
|
|
|
|
|
$cid = $content['cid'];
|
|
|
|
|
$embed = '';
|
|
|
|
|
switch ($media_type) {
|
|
|
|
|
case 'tweet':
|
2021-12-08 19:16:40 +03:00
|
|
|
|
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 .= '';
|
|
|
|
|
}
|
2021-07-20 10:54:07 +03:00
|
|
|
|
break;
|
|
|
|
|
case 'instagram':
|
|
|
|
|
$url = "https://instagram.com/p/$cid/media/?size=l";
|
|
|
|
|
$embed .= <<<EOD
|
|
|
|
|
<img
|
|
|
|
|
src="{$url}"
|
|
|
|
|
alt="instagram-image-$cid"
|
|
|
|
|
>
|
|
|
|
|
EOD;
|
|
|
|
|
break;
|
|
|
|
|
case 'youtube':
|
|
|
|
|
$url = "https://www.youtube.com/embed/$cid";
|
|
|
|
|
$embed .= <<<EOD
|
|
|
|
|
<iframe
|
|
|
|
|
width="560"
|
|
|
|
|
height="315"
|
|
|
|
|
src="{$url}"
|
|
|
|
|
frameborder="0"
|
|
|
|
|
allowfullscreen
|
|
|
|
|
>
|
|
|
|
|
</iframe>
|
|
|
|
|
EOD;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$description .= $embed;
|
|
|
|
|
break;
|
2021-12-08 19:16:40 +03:00
|
|
|
|
case 'social_media':
|
|
|
|
|
if ($content['sub_type'] == 'twitter') {
|
|
|
|
|
$description .= $content['html'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'table':
|
|
|
|
|
$table = '<table>';
|
2022-10-26 01:47:45 +03:00
|
|
|
|
$theaders = $content['header'] ?? null;
|
|
|
|
|
if ($theaders) {
|
|
|
|
|
$tr = '<tr>';
|
|
|
|
|
foreach ($theaders as $header) {
|
|
|
|
|
$tr .= '<th>' . $header . '</th>';
|
|
|
|
|
}
|
|
|
|
|
$tr .= '</tr>';
|
|
|
|
|
$table .= $tr;
|
2021-12-08 19:16:40 +03:00
|
|
|
|
}
|
|
|
|
|
$rows = $content['rows'];
|
|
|
|
|
foreach ($rows as $row) {
|
2023-09-22 21:59:45 +03:00
|
|
|
|
if (!is_array($row)) {
|
|
|
|
|
// some rows are null
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$tr = '<tr>';
|
|
|
|
|
foreach ($row as $data) {
|
|
|
|
|
$tr .= '<td>' . $data . '</td>';
|
|
|
|
|
}
|
|
|
|
|
$tr .= '</tr>';
|
|
|
|
|
$table .= $tr;
|
|
|
|
|
}
|
|
|
|
|
$table .= '</table>';
|
|
|
|
|
$description .= $table;
|
|
|
|
|
break;
|
|
|
|
|
case 'image':
|
|
|
|
|
$description .= $this->handleImage([$content]);
|
2021-01-29 20:57:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-03-09 09:15:48 +03:00
|
|
|
|
return $description;
|
2021-01-29 20:57:40 +03:00
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
public function getName()
|
|
|
|
|
{
|
|
|
|
|
return $this->feedName;
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-01-29 20:57:40 +03:00
|
|
|
|
public function collectData()
|
|
|
|
|
{
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$endpoint = $this->getSectionEndpoint();
|
|
|
|
|
$url = $this->getAPIURL($endpoint, 'section');
|
2023-07-29 01:14:30 +03:00
|
|
|
|
$json = getContents($url);
|
|
|
|
|
$data = Json::decode($json);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$stories = [];
|
|
|
|
|
$section_name = '';
|
|
|
|
|
if ($this->useWireAPI) {
|
|
|
|
|
$reuters_wireitems = $data['wireitems'];
|
|
|
|
|
$section_name = $data['wire_name'];
|
|
|
|
|
$processedData = $this->processData($reuters_wireitems);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
// Merge all articles from Editor's Highlight section into existing array of templates.
|
|
|
|
|
$top_section = reset($processedData);
|
|
|
|
|
if ($top_section['type'] == 'headlines') {
|
|
|
|
|
$top_section = array_shift($processedData);
|
|
|
|
|
$articles = $top_section['headlines'];
|
|
|
|
|
$processedData = array_merge($articles, $processedData);
|
|
|
|
|
}
|
|
|
|
|
$stories = $processedData;
|
2021-01-29 20:57:40 +03:00
|
|
|
|
} else {
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$section_name = $data['result']['section']['name'];
|
|
|
|
|
if (isset($data['arcResult']['articles'])) {
|
|
|
|
|
$stories = $data['arcResult']['articles'];
|
|
|
|
|
} else {
|
|
|
|
|
$stories = $data['result']['articles'];
|
2021-01-29 20:57:40 +03:00
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
}
|
2021-12-08 19:16:40 +03:00
|
|
|
|
$this->feedName = $section_name . ' | Reuters';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2021-12-08 19:16:40 +03:00
|
|
|
|
foreach ($stories as $story) {
|
|
|
|
|
$uid = '';
|
|
|
|
|
$author = '';
|
|
|
|
|
$category = [];
|
|
|
|
|
$content = '';
|
|
|
|
|
$title = '';
|
|
|
|
|
$timestamp = '';
|
|
|
|
|
$url = '';
|
|
|
|
|
$article_uri = '';
|
2022-03-02 07:50:02 +03:00
|
|
|
|
$source_type = '';
|
2021-12-08 19:16:40 +03:00
|
|
|
|
if ($this->useWireAPI) {
|
|
|
|
|
$uid = $story['story']['usn'];
|
|
|
|
|
$article_uri = $story['template_action']['api_path'];
|
|
|
|
|
$title = $story['story']['hed'];
|
|
|
|
|
$url = $story['template_action']['url'];
|
|
|
|
|
} else {
|
|
|
|
|
$uid = $story['id'];
|
|
|
|
|
$url = self::URI . $story['canonical_url'];
|
|
|
|
|
$title = $story['title'];
|
|
|
|
|
$article_uri = $story['canonical_url'];
|
2022-03-02 07:50:02 +03:00
|
|
|
|
$source_type = $story['source']['name'];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 07:50:02 +03:00
|
|
|
|
// Some article cause unexpected behaviour like redirect to another site not API.
|
|
|
|
|
// Attempt to check article source type to avoid this.
|
2022-03-28 21:34:41 +03:00
|
|
|
|
if (!$this->useWireAPI && $source_type != 'Package') { // Only Reuters PF api have this, Wire don't.
|
2022-09-09 21:18:50 +03:00
|
|
|
|
$author = $this->handleAuthorName($story['authors'] ?? []);
|
2022-03-02 07:50:02 +03:00
|
|
|
|
$timestamp = $story['published_time'];
|
|
|
|
|
$image_placeholder = '';
|
|
|
|
|
if (isset($story['thumbnail'])) {
|
|
|
|
|
$image_placeholder = $this->handleImage([$story['thumbnail']]);
|
|
|
|
|
}
|
|
|
|
|
$content = $story['description'] . $image_placeholder;
|
2023-07-03 01:39:01 +03:00
|
|
|
|
if (isset($story['primary_section']['name'])) {
|
|
|
|
|
$category = [$story['primary_section']['name']];
|
|
|
|
|
} else {
|
|
|
|
|
$category = [];
|
|
|
|
|
}
|
2022-03-02 07:50:02 +03:00
|
|
|
|
} else {
|
|
|
|
|
$content_detail = $this->getArticle($article_uri);
|
|
|
|
|
$description = $content_detail['content'];
|
|
|
|
|
$description = defaultLinkTo($description, $this->getURI());
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-02 07:50:02 +03:00
|
|
|
|
$author = $content_detail['author'];
|
|
|
|
|
$images = $content_detail['images'];
|
|
|
|
|
$category = $content_detail['category'];
|
|
|
|
|
$content = "$description $images";
|
|
|
|
|
$timestamp = $content_detail['published_at'];
|
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
2022-03-28 21:34:41 +03:00
|
|
|
|
$this->addStories($title, $content, $timestamp, $author, $url, $category);
|
2021-01-29 20:57:40 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|