-
Notifications
You must be signed in to change notification settings - Fork 1k
/
NovayaGazetaEuropeBridge.php
151 lines (143 loc) · 5.22 KB
/
NovayaGazetaEuropeBridge.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
<?php
class NovayaGazetaEuropeBridge extends BridgeAbstract
{
const MAINTAINER = 'sqrtminusone';
const NAME = 'Novaya Gazeta Europe Bridge';
const URI = 'https://novayagazeta.eu';
const CACHE_TIMEOUT = 3600; // 1 hour
const DESCRIPTION = 'Returns articles from Novaya Gazeta Europe';
const PARAMETERS = [
'' => [
'language' => [
'name' => 'Language',
'type' => 'list',
'defaultValue' => 'ru',
'values' => [
'Russian' => 'ru',
'English' => 'en',
]
],
'limit' => [
'name' => 'Limit',
'type' => 'number',
'required' => false,
'title' => 'Maximum number of items to return',
'defaultValue' => 20
]
]
];
public function collectData()
{
$url = 'https://novayagazeta.eu/api/v1/get/main';
if ($this->getInput('language') != 'ru') {
$url .= '?lang=' . $this->getInput('language');
}
$json = getContents($url);
$data = json_decode($json);
foreach ($data->records as $record) {
if (!isset($record->blocks)) {
continue;
}
foreach ($record->blocks as $block) {
if (!property_exists($block, 'date')) {
continue;
}
$title = strip_tags($block->title);
if (!empty($block->subtitle)) {
$title .= '. ' . strip_tags($block->subtitle);
}
$item = [
'uri' => self::URI . '/articles/' . $block->slug,
'block' => $block,
'title' => $title,
'author' => join(', ', array_map(function ($author) {
return $author->name;
}, $block->authors)),
'timestamp' => $block->date / 1000,
'categories' => $block->tags
];
$this->items[] = $item;
}
}
usort($this->items, function ($item1, $item2) {
return $item2['timestamp'] <=> $item1['timestamp'];
});
if ($this->getInput('limit') !== null) {
$this->items = array_slice($this->items, 0, $this->getInput('limit'));
}
foreach ($this->items as &$item) {
$block = $item['block'];
$body = '';
if (property_exists($block, 'body') && $block->body !== null) {
$body = self::convertBody($block);
} else {
$record_json = getContents("https://novayagazeta.eu/api/v1/get/record?slug={$block->slug}");
$record_data = json_decode($record_json);
$body = self::convertBody($record_data->record);
}
$item['content'] = $body;
unset($item['block']);
}
}
private static function convertBody($data)
{
$body = '';
if ($data->previewUrl !== null && !$data->isPreviewHidden) {
$body .= '<figure><img src="' . $data->previewUrl . '"/>';
if ($data->previewCaption !== null) {
$body .= '<figcaption>' . $data->previewCaption . '</figcaption>';
}
$body .= '</figure>';
}
if ($data->lead !== null) {
$body .= "<p><b>{$data->lead}</b></p>";
}
if (!empty($data->body)) {
foreach ($data->body as $datum) {
$body .= self::convertElement($datum);
}
}
return $body;
}
private static function convertElement($datum)
{
switch ($datum->type) {
case 'text':
return $datum->data;
case 'image/single':
$alt = strip_tags($datum->data);
$res = "<figure><img src=\"{$datum->previewUrl}\" alt=\"{$alt}\" />";
if ($datum->data !== null) {
$res .= "<figcaption>{$datum->data}</figcaption>";
}
$res .= '</figure>';
return $res;
case 'text/quote':
return "<figure><blockquote>{$datum->data}</blockquote></figure><br>";
case 'embed/native':
if (isset($datum->link)) {
$desc = $datum->link;
if (isset($datum->caption)) {
$desc = $datum->caption;
}
return sprintf('<p><a href="%s">%s</a></p>', $datum->link, $desc);
}
return '';
case 'text/framed':
$res = '';
if (property_exists($datum, 'typeDisplay')) {
$res .= "<p><b>{$datum->typeDisplay}</b></p>";
}
$res .= "<p>{$datum->data}</p>";
if (
property_exists($datum, 'attachment')
&& property_exists($datum->attachment, 'type')
) {
$res .= self::convertElement($datum->attachment);
}
return $res;
default:
return '';
}
}
}