-
Notifications
You must be signed in to change notification settings - Fork 1k
/
EuronewsBridge.php
215 lines (204 loc) · 8.83 KB
/
EuronewsBridge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
class EuronewsBridge extends BridgeAbstract
{
const MAINTAINER = 'sqrtminusone';
const NAME = 'Euronews Bridge';
const URI = 'https://www.euronews.com/';
const CACHE_TIMEOUT = 600; // 10 minutes
const DESCRIPTION = 'Return articles from the "Just In" feed of Euronews.';
const PARAMETERS = [
'' => [
'lang' => [
'name' => 'Language',
'type' => 'list',
'defaultValue' => 'www.euronews.com',
'values' => [
'English' => 'www.euronews.com',
'French' => 'fr.euronews.com',
'German' => 'de.euronews.com',
'Italian' => 'it.euronews.com',
'Spanish' => 'es.euronews.com',
'Portuguese' => 'pt.euronews.com',
'Russian' => 'ru.euronews.com',
'Turkish' => 'tr.euronews.com',
'Greek' => 'gr.euronews.com',
'Hungarian' => 'hu.euronews.com',
'Persian' => 'per.euronews.com',
'Arabic' => 'arabic.euronews.com',
/* These versions don't have timeline.json */
// 'Albanian' => 'euronews.al',
// 'Romanian' => 'euronews.ro',
// 'Georigian' => 'euronewsgeorgia.com',
// 'Bulgarian' => 'euronewsbulgaria.com'
// 'Serbian' => 'euronews.rs'
]
],
'limit' => [
'name' => 'Limit of items per feed',
'required' => true,
'type' => 'number',
'defaultValue' => 10,
'title' => 'Maximum number of returned feed items. Maximum 50, default 10'
],
]
];
public function collectData()
{
$limit = $this->getInput('limit');
$lang = $this->getInput('lang');
if ($lang === 'euronews.com') {
$lang = 'www.euronews.com';
}
$root_url = 'https://' . $lang;
$url = $root_url . '/api/timeline.json?limit=' . $limit;
$json = getContents($url);
$data = json_decode($json, true);
foreach ($data as $datum) {
$datum_uri = $root_url . $datum['path'];
$url_datum = $this->getItemContent($datum_uri);
$categories = [];
if (array_key_exists('program', $datum)) {
if (array_key_exists('title', $datum['program'])) {
$categories[] = $datum['program']['title'];
}
}
if (array_key_exists('themes', $datum)) {
foreach ($datum['themes'] as $theme) {
$categories[] = $theme['title'];
}
}
$item = [
'uri' => $datum_uri,
'title' => $datum['title'],
'uid' => strval($datum['id']),
'timestamp' => $datum['publishedAt'],
'content' => $url_datum['content'],
'author' => $url_datum['author'],
'enclosures' => $url_datum['enclosures'],
'categories' => array_unique($categories)
];
$this->items[] = $item;
}
}
private function getItemContent($url)
{
try {
$html = getSimpleHTMLDOMCached($url);
} catch (Exception $e) {
// Every once in a while it fails with too many redirects
return ['author' => null, 'content' => null, 'enclosures' => null];
}
$data = $html->find('script[type="application/ld+json"]', 0)->innertext;
$json = json_decode($data, true);
$author = 'Euronews';
$content = '';
$enclosures = [];
if (array_key_exists('@graph', $json)) {
foreach ($json['@graph'] as $item) {
if ($item['@type'] == 'NewsArticle') {
if (array_key_exists('author', $item)) {
$author = $item['author']['name'];
}
if (array_key_exists('image', $item)) {
$content .= '<figure>';
$content .= '<img src="' . $item['image']['url'] . '">';
$content .= '<figcaption>' . $item['image']['caption'] . '</figcaption>';
$content .= '</figure><br>';
}
if (array_key_exists('video', $item)) {
$enclosures[] = $item['video']['contentUrl'];
}
}
}
}
// Normal article
$article_content = $html->find('.c-article-content', 0);
if ($article_content) {
// Usually the .c-article-content is the root of the
// content, but once in a blue moon the root is the second
// div
if (
(count($article_content->children()) == 2)
&& ($article_content->children(1)->tag == 'div')
) {
$article_content = $article_content->children(1);
}
// The content is interspersed with links and stuff, so we
// iterate over the children
foreach ($article_content->children() as $element) {
if ($element->tag == 'p') {
$scribble_live = $element->find('#scribblelive-items', 0);
if (is_null($scribble_live)) {
// A normal paragraph
$content .= '<p>' . $element->innertext . '</p>';
} else {
// LIVE mode
foreach ($scribble_live->children() as $child) {
if ($child->tag == 'div') {
$content .= '<div>' . $child->innertext . '</div>';
}
}
}
} elseif (preg_match('/h[1-6]/', $element->tag)) {
// Header
$content .= '<h' . $element->tag[1] . '>' . $element->innertext . '</h' . $element->tag[1] . '>';
} elseif ($element->tag == 'div') {
if (preg_match('/.*widget--type-image.*/', $element->class)) {
// Image
$content .= '<figure>';
$content .= '<img src="' . $element->find('img', 0)->src . '">';
$caption = $element->find('figcaption', 0);
if ($caption) {
$content .= '<figcaption>' . $element->plaintext . '</figcaption>';
}
$content .= '</figure><br>';
} elseif (preg_match('/.*widget--type-quotation.*/', $element->class)) {
// Quotation
$quote = $element->find('.widget__quoteText', 0);
$author = $element->find('.widget__author', 0);
$content .= '<figure>';
$content .= '<blockquote>' . $quote->plaintext . '</blockquote>';
if ($author) {
$content .= '<figcaption>' . $author->plaintext . '</figcaption>';
}
$content .= '</figure><br>';
}
}
}
}
// Video article
if (is_null($article_content)) {
$image = $html->find('.c-article-media__img', 0);
if ($image) {
$content .= '<figure>';
$content .= '<img src="' . $image->src . '">';
$content .= '</figure><br>';
}
$description = $html->find('.m-object__description', 0);
if ($description) {
// In some editions the description is a link to the
// current page
$content .= '<div>' . $description->plaintext . '</div>';
}
// Euronews usually hosts videos on dailymotion...
$player_div = $html->find('.dmPlayer', 0);
if ($player_div) {
$video_id = $player_div->getAttribute('data-video-id');
$video_url = 'https://www.dailymotion.com/video/' . $video_id;
$content .= '<a href="' . $video_url . '">' . $video_url . '</a>';
}
// ...or on YouTube
$player_div = $html->find('.js-player-pfp', 0);
if ($player_div) {
$video_id = $player_div->getAttribute('data-video-id');
$video_url = 'https://www.youtube.com/watch?v=' . $video_id;
$content .= '<a href="' . $video_url . '">' . $video_url . '</a>';
}
}
return [
'author' => $author,
'content' => $content,
'enclosures' => $enclosures
];
}
}