-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FlickrBridge.php
284 lines (245 loc) · 9.08 KB
/
FlickrBridge.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/* This is a mashup of FlickrExploreBridge by sebsauvage and FlickrTagBridge
* by erwang, providing the functionality of both in one.
*/
class FlickrBridge extends BridgeAbstract
{
const MAINTAINER = 'logmanoriginal';
const NAME = 'Flickr Bridge';
const URI = 'https://www.flickr.com/';
const CACHE_TIMEOUT = 21600; // 6 hours
const DESCRIPTION = 'Returns images from Flickr';
const PARAMETERS = [
'Explore' => [],
'By keyword' => [
'q' => [
'name' => 'Keyword',
'type' => 'text',
'required' => true,
'title' => 'Insert keyword',
'exampleValue' => 'bird'
],
'media' => [
'name' => 'Media',
'type' => 'list',
'values' => [
'All (Photos & videos)' => 'all',
'Photos' => 'photos',
'Videos' => 'videos',
],
'defaultValue' => 'all',
],
'sort' => [
'name' => 'Sort By',
'type' => 'list',
'values' => [
'Relevance' => 'relevance',
'Date uploaded' => 'date-posted-desc',
'Date taken' => 'date-taken-desc',
'Interesting' => 'interestingness-desc',
],
'defaultValue' => 'relevance',
]
],
'By username' => [
'u' => [
'name' => 'Username',
'type' => 'text',
'required' => true,
'title' => 'Insert username (as shown in the address bar)',
'exampleValue' => 'flickr'
],
'content' => [
'name' => 'Content',
'type' => 'list',
'values' => [
'Uploads' => 'uploads',
'Favorites' => 'faves',
],
'defaultValue' => 'uploads',
],
'media' => [
'name' => 'Media',
'type' => 'list',
'values' => [
'All (Photos & videos)' => 'all',
'Photos' => 'photos',
'Videos' => 'videos',
],
'defaultValue' => 'all',
],
'sort' => [
'name' => 'Sort By',
'type' => 'list',
'values' => [
'Relevance' => 'relevance',
'Date uploaded' => 'date-posted-desc',
'Date taken' => 'date-taken-desc',
'Interesting' => 'interestingness-desc',
],
'defaultValue' => 'date-posted-desc',
]
]
];
private $username = '';
public function collectData()
{
switch ($this->queriedContext) {
case 'Explore':
$filter = 'photo-lite-models';
$html = getSimpleHTMLDOM($this->getURI());
break;
case 'By keyword':
$filter = 'photo-lite-models';
$html = getSimpleHTMLDOM($this->getURI());
break;
case 'By username':
//$filter = 'photo-models';
$filter = 'photo-lite-models';
$html = getSimpleHTMLDOM($this->getURI());
$this->username = $this->getInput('u');
if ($html->find('span.search-pill-name', 0)) {
$this->username = $html->find('span.search-pill-name', 0)->plaintext;
}
break;
default:
returnClientError('Invalid context: ' . $this->queriedContext);
}
$model_json = $this->extractJsonModel($html);
$photo_models = $this->getPhotoModels($model_json, $filter);
foreach ($photo_models as $model) {
$item = [];
/* Author name depends on scope. On a keyword search the
* author is part of the picture data. On a username search
* the author is part of the owner data.
*/
if (array_key_exists('username', $model)) {
$item['author'] = urldecode($model['username']);
} elseif (array_key_exists('owner', reset($model_json)[0])) {
$item['author'] = urldecode(reset($model_json)[0]['owner']['username']);
}
$item['title'] = urldecode((array_key_exists('title', $model) ? $model['title'] : 'Untitled'));
$item['uri'] = self::URI . 'photo.gne?id=' . $model['id'];
$description = (array_key_exists('description', $model) ? $model['description'] : '');
$item['content'] = '<a href="'
. $item['uri']
. '"><img src="'
. $this->extractContentImage($model)
. '" style="max-width: 640px; max-height: 480px;"/></a><br><p>'
. urldecode($description)
. '</p>';
$item['enclosures'] = $this->extractEnclosures($model);
$this->items[] = $item;
}
}
public function getURI()
{
switch ($this->queriedContext) {
case 'Explore':
return self::URI . 'explore';
break;
case 'By keyword':
return self::URI . 'search/?q=' . urlencode($this->getInput('q'))
. '&sort=' . $this->getInput('sort') . '&media=' . $this->getInput('media');
break;
case 'By username':
$uri = self::URI . 'search/?user_id=' . urlencode($this->getInput('u'))
. '&sort=date-posted-desc&media=' . $this->getInput('media');
if ($this->getInput('content') === 'faves') {
return $uri . '&faves=1';
}
return $uri;
break;
default:
return parent::getURI();
}
}
public function getName()
{
switch ($this->queriedContext) {
case 'Explore':
return 'Explore - ' . self::NAME;
break;
case 'By keyword':
return $this->getInput('q') . ' - keyword - ' . self::NAME;
break;
case 'By username':
if ($this->getInput('content') === 'faves') {
return $this->username . ' - favorites - ' . self::NAME;
}
return $this->username . ' - ' . self::NAME;
break;
default:
return parent::getName();
}
return parent::getName();
}
private function extractJsonModel($html)
{
// Find SCRIPT containing JSON data
$model = $html->find('.modelExport', 0);
$model_text = $model->innertext;
// Find start and end of JSON data
$start = strpos($model_text, 'modelExport:') + strlen('modelExport:');
$end = strpos($model_text, 'auth:') - strlen('auth:');
// Extract JSON data, remove trailing comma
$model_text = trim(substr($model_text, $start, $end - $start));
$model_text = substr($model_text, 0, strlen($model_text) - 1);
return json_decode($model_text, true);
}
private function getPhotoModels($json, $filter)
{
// The JSON model contains a "legend" array, where each element contains
// the path to an element in the "main" object
$photo_models = [];
foreach ($json['legend'] as $legend) {
$photo_model = $json['main'];
foreach ($legend as $element) { // Traverse tree
$photo_model = $photo_model[$element];
}
// We are only interested in content
if ($photo_model['_flickrModelRegistry'] === $filter) {
$photo_models[] = $photo_model;
}
}
return $photo_models;
}
private function extractEnclosures($model)
{
$areas = [];
foreach ($model['sizes']['data'] as $size) {
$size = $size['data'];
$areas[$size['width'] * $size['height']] = $size['url'];
}
return [$this->fixURL(max($areas))];
}
private function extractContentImage($model)
{
$areas = [];
$limit = 320 * 240;
$sizes = $model['sizes']['data'];
foreach ($sizes as $sizeData) {
$sizeData = $sizeData['data'];
$area = $sizeData['width'] * $sizeData['height'];
if ($area >= $limit) {
$areas[$area] = $sizeData['url'];
}
}
if ($areas) {
$minKey = min(array_keys($areas));
$url = $areas[$minKey];
} else {
$array_key_first = array_key_first($sizes);
$url = $sizes[$array_key_first]['data']['url'];
}
return $this->fixURL($url);
}
private function fixURL($url)
{
// For some reason the image URLs don't include the protocol (https)
if (strpos($url, '//') === 0) {
$url = 'https:' . $url;
}
return $url;
}
}