-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArchiveofOurOwnBridge.php
342 lines (278 loc) · 9.21 KB
/
ArchiveofOurOwnBridge.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
class ArchiveofOurOwnBridge extends BridgeAbstract {
const NAME = 'Archive of Our Own (AO3) Bridge';
const URI = 'https://archiveofourown.org';
const DESCRIPTION = 'Returns fanfiction by user, series, tag, or chapters.';
const MAINTAINER = 'VerifiedJoseph';
const PARAMETERS = array(
'User' => array(
'u' => array(
'name' => 'username',
'type' => 'text',
'required' => true,
),
'c' => array(
'name' => 'content',
'type' => 'list',
'required' => true,
'values' => array(
'Works' => 'works',
'Series' => 'series',
'Bookmarks' => 'bookmarks',
'Gifts' => 'gifts'
)
),
'l' => array(
'title' => 'Limits number of tags to 15',
'name' => 'Limit tags',
'type' => 'checkbox'
)
),
'Series' => array(
's' => array(
'name' => 'Series ID',
'type' => 'text',
'required' => true,
),
'l' => array(
'title' => 'Limits number of tags to 15',
'name' => 'Limit tags',
'type' => 'checkbox'
)
),
'Tag' => array(
't' => array(
'name' => 'Tag',
'type' => 'text',
'required' => true,
),
'l' => array(
'title' => 'Limits number of tags to 15',
'name' => 'Limit tags',
'type' => 'checkbox'
)
),
'Chapters' => array(
'w' => array(
'name' => 'Work ID',
'type' => 'text',
'required' => true,
),
),
);
const CACHE_TIMEOUT = 3600;
private $feedName = '';
private $userProfile = array(
'works' => array(
'url' => '/works',
'liClass' => 'li.work.blurb.group',
'regex' => '/Works? by ([A-Za-z0-9_]+)$/'
),
'series' => array(
'url' => '/series',
'liClass' => 'li.series.blurb.group',
'regex' => '/^([A-Za-z0-9_]+)\'s Series$/'
),
'bookmarks' => array(
'url' => '/bookmarks?bookmark_search[sort_column]=bookmarkable_date',
'liClass' => 'li.bookmark.blurb.group',
'regex' => '/Bookmarks? by ([A-Za-z0-9_]+)$/'
),
'collections' => array(
'url' => '/collections',
'liClass' => 'li.collections.blurb.group',
'regex' => '/^([A-Za-z0-9_]+)\'s Collections$/'
),
'gifts' => array(
'url' => '/gifts',
'liClass' => 'li.work.blurb.group',
'regex' => '/^Gifts for ([A-Za-z0-9_]+)$/'
),
);
public function collectData() {
$item = array();
$html = getSimpleHTMLDOM($this->getURI())
or returnServerError('Could not request: ' . $this->getURI());
// Feed for works, series, bookmarks or gifts from a user's profile.
if ($this->queriedContext === 'User') {
$content_type = $this->getInput('c');
// Feed name
if(preg_match($this->userProfile[$content_type]['regex'], trim($html->find('h2.heading', 0)->plaintext), $matches)) {
$this->feedName = $matches[1] . ' - ' . ucfirst($this->getInput('c'));
}
foreach($html->find($this->userProfile[$content_type]['liClass']) as $work) {
$item['title'] = $this->processWorkTitle($work);
$item['author'] = $this->processWorkAuthor($work);
$item['timestamp'] = $this->processWorkTimestamp($work);
$item['uri'] = $this->processWorkUri($work);
$item['content'] = $this->processContent($work);
$item['content'] .= $this->processStats($work);
$item['categories'] = $this->processTags($work);
$this->items[] = $item;
}
}
// Feed for works of a specific series.
if ($this->queriedContext === 'Series') {
$seriesTitle = $html->find('h2.heading', 0)->plaintext;
$SeriesCreator = $html->find('dl.series.meta.group', 0)->children(1)->plaintext;
$SeriesCreatorPath = self::URI . $html->find('dl.series.meta.group', 0)->children(1)->children(0)->href;
$this->feedName = $seriesTitle . ' (Series By ' . $SeriesCreator . ')';
foreach($html->find('li.work.blurb.group') as $work) {
$item['title'] = $this->processWorkTitle($work);
$item['author'] = $this->processWorkAuthor($work);
$item['timestamp'] = $this->processWorkTimestamp($work);
$item['uri'] = $this->processWorkUri($work);
$item['content'] = $this->processContent($work);
$item['content'] .= $this->processStats($work);
$item['categories'] = $this->processTags($work);
$this->items[] = $item;
}
$this->fixDateOrder();
}
// Feed for works of a specific tag.
if ($this->queriedContext === 'Tag') {
$TagTitle = htmlspecialchars_decode($html->find('h2.heading', 0)->children(0)->plaintext, ENT_QUOTES);
$this->feedName = $TagTitle . ' - Tag';
foreach($html->find('li.work.blurb.group') as $work) {
$item['title'] = $this->processWorkTitle($work);
$item['author'] = $this->processWorkAuthor($work);
$item['timestamp'] = $this->processWorkTimestamp($work);
$item['uri'] = $this->processWorkUri($work);
$item['content'] = $this->processContent($work);
$item['categories'] = $this->processTags($work);
$this->items[] = $item;
}
}
// Feed for chapters of a specific work.
if ($this->queriedContext === 'Chapters') {
$heading = $html->find('h2.heading', 0);
$this->feedName = $heading->children(0)->plaintext;
$authors = array();
foreach($heading->find('a[rel=author]') as $a) {
$authors[] = htmlspecialchars_decode($a->plaintext, ENT_QUOTES);
}
$workCreator = implode(', ', $authors);
foreach($html->find('ol.chapter.index.group li') as $chapter) {
$date = str_replace(array('(', ')'), array(''), $chapter->children(1)->plaintext);
$item['title'] = htmlspecialchars_decode($chapter->children(0)->plaintext, ENT_QUOTES);
$item['author'] = $workCreator;
$item['timestamp'] = strtotime($date);
$item['uri'] = self::URI . $chapter->children(0)->href;
$this->items[] = $item;
}
$this->items = array_reverse($this->items);
}
}
public function getName() {
if ($this->feedName) {
return $this->feedName . ' - AO3';
}
return parent::getName();
}
public function getURI() {
switch($this->queriedContext) {
case 'User':
return self::URI . '/users/' . $this->getInput('u')
. $this->userProfile[$this->getInput('c')]['url'];
case 'Series':
return self::URI . '/series/'
. $this->getInput('s');
case 'Tag':
return self::URI . '/tags/'
. rawurlencode($this->getInput('t')) . '/works';
case 'Chapters':
return self::URI . '/works/'
. $this->getInput('w') . '/navigate';
default: return parent::getURI();
}
}
private function processWorkTitle($work) {
$heading = $work->find('h4.heading', 0);
return htmlspecialchars_decode($heading->find('a', 0)->plaintext, ENT_QUOTES);
}
private function processWorkAuthor($work) {
$heading = $work->find('h4.heading', 0);
$authors = array();
foreach($heading->find('a[rel=author]') as $author) {
$authors[] = htmlspecialchars_decode($author->plaintext, ENT_QUOTES);
}
return implode(', ', $authors);
}
private function processWorkTimestamp($work) {
if ($this->getInput('c') === 'bookmarks') {
return strtotime($work->find('div.user.module.group > p.datetime', 0)->plaintext);
}
return strtotime($work->find('p.datetime', 0)->plaintext);
}
private function processWorkUri($work) {
$heading = $work->find('h4.heading', 0);
return self::URI . $heading->find('a', 0)->href;
}
private function processContent($work) {
$content = '';
$authors = '';
$fandoms = '';
// Description
if ($work->find('blockquote', 0)) {
$content .= trim($work->find('blockquote', 0)->innertext);
}
// Series
if ($work->find('ul.series', 0)) {
$series = '';
foreach($work->find('ul.series li') as $s) {
$part = $s->children(0)->plaintext;
$name = $s->children(1)->plaintext;
$link = self::URI . $s->children(1)->href;
$series .= '<br>Part ' . $part . ' of <a target="_blank" href="' . $link . '">' . $name . '</a>';
}
$content .= '<p>Series:' . $series . '</p>';
}
// Authors
foreach($work->find('h4.heading a[rel=author]') as $a) {
$authors .= '<br><a href="' . self::URI . $a->href . '">' . htmlspecialchars_decode($a->plaintext, ENT_QUOTES) . '</a>';
}
$content .= '<p>Authors:' . $authors . '</p>';
// Fandoms
foreach($work->find('h5.fandoms.heading a') as $fandom) {
$fandoms .= '<br><a target="_blank" href="' . self::URI . $fandom->href . '">' . $fandom->innertext . '</a>';
}
$content .= '<p>Fandoms:' . $fandoms . '</p>';
return $content;
}
private function processTags($work) {
$limit = 15;
$tags = array();
foreach($work->find('ul.tags li') as $tag) {
if ($tag->class === 'warnings') {
continue;
}
$tags[] = htmlspecialchars_decode($tag->find('a', 0)->innertext, ENT_NOQUOTES);
if ($this->getInput('l') && count($tags) >= $limit) {
break;
}
}
return $tags;
}
private function processStats($work) {
$noNumberFormatting = array('language','words', 'chapters');
$stats = '';
if (($this->queriedContext === 'User' && $this->getInput('c') != 'series') || $this->queriedContext === 'Series') {
$dl = $work->find('dl', 0);
foreach($dl->find('dd') as $stat) {
$value = $stat->plaintext;
if (in_array($stat->class, $noNumberFormatting) === false) {
$value = number_format($stat->plaintext);
}
$stats .= '<p>' . ucfirst($stat->class) . ': <br>' . $value . '</p>';
}
}
return $stats;
}
private function fixDateOrder() {
$sort = array();
foreach ($this->items as $key => $item) {
$sort[$key] = $item['timestamp'];
}
array_multisort($sort, SORT_DESC, $this->items);
}
}