-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OpenlyBridge.php
255 lines (224 loc) · 8.25 KB
/
OpenlyBridge.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
class OpenlyBridge extends BridgeAbstract
{
const NAME = 'Openly Bridge';
const URI = 'https://www.openlynews.com/';
const DESCRIPTION = 'Returns news articles';
const MAINTAINER = 'VerifiedJoseph';
const PARAMETERS = [
'All News' => [],
'All Opinion' => [],
'By Region' => [
'region' => [
'name' => 'Region',
'type' => 'list',
'values' => [
'Africa' => 'africa',
'Asia Pacific' => 'asia-pacific',
'Europe' => 'europe',
'Latin America' => 'latin-america',
'Middle Easta' => 'middle-east',
'North America' => 'north-america'
]
],
'content' => [
'name' => 'Content',
'type' => 'list',
'values' => [
'News' => 'news',
'Opinion' => 'people'
],
'defaultValue' => 'news'
]
],
'By Tag' => [
'tag' => [
'name' => 'Tag',
'type' => 'text',
'required' => true,
'exampleValue' => 'lgbt-law',
],
'content' => [
'name' => 'Content',
'type' => 'list',
'values' => [
'News' => 'news',
'Opinion' => 'people'
],
'defaultValue' => 'news'
]
],
'By Author' => [
'profileId' => [
'name' => 'Profile ID',
'type' => 'text',
'required' => true,
'exampleValue' => '003D000002WZGYRIA5',
]
]
];
const TEST_DETECT_PARAMETERS = [
'https://www.openlynews.com/profile/?id=0033z00002XUTepAAH' => [
'context' => 'By Author', 'profileId' => '0033z00002XUTepAAH'
],
'https://www.openlynews.com/news/?page=1&theme=lgbt-law' => [
'context' => 'By Tag', 'content' => 'news', 'tag' => 'lgbt-law'
],
'https://www.openlynews.com/news/?page=1®ion=north-america' => [
'context' => 'By Region', 'content' => 'news', 'region' => 'north-america'
],
'https://www.openlynews.com/news/?theme=lgbt-law' => [
'context' => 'By Tag', 'content' => 'news', 'tag' => 'lgbt-law'
],
'https://www.openlynews.com/news/?region=north-america' => [
'context' => 'By Region', 'content' => 'news', 'region' => 'north-america'
]
];
const CACHE_TIMEOUT = 900; // 15 mins
const ARTICLE_CACHE_TIMEOUT = 3600; // 1 hour
private $feedTitle = '';
private $itemLimit = 10;
private $profileUrlRegex = '/openlynews\.com\/profile\/\?id=([a-zA-Z0-9]+)/';
private $tagUrlRegex = '/openlynews\.com\/([a-z]+)\/\?(?:page=(?:[0-9]+)&)?theme=([\w-]+)/';
private $regionUrlRegex = '/openlynews\.com\/([a-z]+)\/\?(?:page=(?:[0-9]+)&)?region=([\w-]+)/';
public function detectParameters($url)
{
$params = [];
if (preg_match($this->profileUrlRegex, $url, $matches) > 0) {
$params['context'] = 'By Author';
$params['profileId'] = $matches[1];
return $params;
}
if (preg_match($this->tagUrlRegex, $url, $matches) > 0) {
$params['context'] = 'By Tag';
$params['content'] = $matches[1];
$params['tag'] = $matches[2];
return $params;
}
if (preg_match($this->regionUrlRegex, $url, $matches) > 0) {
$params['context'] = 'By Region';
$params['content'] = $matches[1];
$params['region'] = $matches[2];
return $params;
}
return null;
}
public function collectData()
{
$url = $this->getAjaxURI();
if ($this->queriedContext === 'By Author') {
$url = $this->getURI();
}
$html = getSimpleHTMLDOM($url);
$html = defaultLinkTo($html, $this->getURI());
if ($html->find('h1', 0)) {
$this->feedTitle = $html->find('h1', 0)->plaintext;
}
if ($html->find('h2.title-v4', 0)) {
$html->find('span.tooltiptext', 0)->innertext = '';
$this->feedTitle = $html->find('a.tooltipitem', 0)->plaintext;
}
$items = $html->find('div.item');
$limit = 5;
foreach (array_slice($items, 0, $limit) as $div) {
$this->items[] = $this->getArticle($div->find('a', 0)->href);
if (count($this->items) >= $this->itemLimit) {
break;
}
}
}
public function getURI()
{
switch ($this->queriedContext) {
case 'All News':
return self::URI . 'news';
break;
case 'All Opinion':
return self::URI . 'people';
break;
case 'By Tag':
return self::URI . $this->getInput('content') . '/?theme=' . $this->getInput('tag');
case 'By Region':
return self::URI . $this->getInput('content') . '/?region=' . $this->getInput('region');
break;
case 'By Author':
return self::URI . 'profile/?id=' . $this->getInput('profileId');
break;
default:
return parent::getURI();
}
}
public function getName()
{
switch ($this->queriedContext) {
case 'All News':
return 'News - Openly';
break;
case 'All Opinion':
return 'Opinion - Openly';
break;
case 'By Tag':
if (empty($this->feedTitle)) {
$this->feedTitle = $this->getInput('tag');
}
if ($this->getInput('content') === 'people') {
return $this->feedTitle . ' - Opinion - Openly';
}
return $this->feedTitle . ' - Openly';
break;
case 'By Region':
if (empty($this->feedTitle)) {
$this->feedTitle = $this->getInput('region');
}
if ($this->getInput('content') === 'people') {
return $this->feedTitle . ' - Opinion - Openly';
}
return $this->feedTitle . ' - Openly';
break;
case 'By Author':
if (empty($this->feedTitle)) {
$this->feedTitle = $this->getInput('profileId');
}
return $this->feedTitle . ' - Author - Openly';
break;
default:
return parent::getName();
}
}
private function getAjaxURI()
{
$part = '/ajax.html?';
switch ($this->queriedContext) {
case 'All News':
return self::URI . 'news' . $part;
break;
case 'All Opinion':
return self::URI . 'people' . $part;
break;
case 'By Tag':
return self::URI . $this->getInput('content') . $part . 'theme=' . $this->getInput('tag');
break;
case 'By Region':
return self::URI . $this->getInput('content') . $part . 'region=' . $this->getInput('region');
break;
}
}
private function getArticle($url)
{
$article = getSimpleHTMLDOMCached($url, self::ARTICLE_CACHE_TIMEOUT);
$article = defaultLinkTo($article, $this->getURI());
$item = [];
$item['title'] = $article->find('h1', 0)->plaintext;
$item['uri'] = $url;
$item['content'] = $article->find('div.body-text', 0);
$item['enclosures'][] = $article->find('meta[name="twitter:image"]', 0)->content;
$item['timestamp'] = $article->find('div.meta.small', 0)->plaintext;
if ($article->find('div.meta a', 0)) {
$item['author'] = $article->find('div.meta a', 0)->plaintext;
}
foreach ($article->find('div.themes li') as $li) {
$item['categories'][] = trim(htmlspecialchars($li->plaintext, ENT_QUOTES));
}
return $item;
}
}