-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FicbookBridge.php
195 lines (168 loc) · 6 KB
/
FicbookBridge.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
<?php
class FicbookBridge extends BridgeAbstract
{
const NAME = 'Ficbook Bridge';
const URI = 'https://ficbook.net/';
const DESCRIPTION = 'No description provided';
const MAINTAINER = 'logmanoriginal';
const PARAMETERS = [
'Site News' => [],
'Fiction Updates' => [
'fiction_id' => [
'name' => 'Fanfiction ID',
'type' => 'text',
'pattern' => '[0-9]+',
'required' => true,
'title' => 'Insert fanfiction ID',
'exampleValue' => '5783919',
],
'include_contents' => [
'name' => 'Include contents',
'type' => 'checkbox',
'title' => 'Activate to include contents in the feed',
],
],
'Fiction Comments' => [
'fiction_id' => [
'name' => 'Fanfiction ID',
'type' => 'text',
'pattern' => '[0-9]+',
'required' => true,
'title' => 'Insert fanfiction ID',
'exampleValue' => '5783919',
],
],
];
protected $titleName;
public function getURI()
{
switch ($this->queriedContext) {
case 'Site News':
// For some reason this is not HTTPS
return 'http://ficbook.net/sitenews';
case 'Fiction Updates':
return self::URI
. 'readfic/'
. urlencode($this->getInput('fiction_id'));
case 'Fiction Comments':
return self::URI
. 'readfic/'
. urlencode($this->getInput('fiction_id'))
. '/comments#content';
default:
return parent::getURI();
}
}
public function getName()
{
switch ($this->queriedContext) {
case 'Site News':
return $this->queriedContext . ' | ' . self::NAME;
case 'Fiction Updates':
return $this->titleName . ' | ' . self::NAME;
case 'Fiction Comments':
return $this->titleName . ' | Comments | ' . self::NAME;
default:
return self::NAME;
}
}
public function collectData()
{
$header = ['Accept-Language: en-US'];
$html = getSimpleHTMLDOM($this->getURI(), $header);
$html = defaultLinkTo($html, self::URI);
if ($this->queriedContext == 'Fiction Updates' or $this->queriedContext == 'Fiction Comments') {
$this->titleName = $html->find('.fanfic-main-info > h1', 0)->innertext;
}
switch ($this->queriedContext) {
case 'Site News':
return $this->collectSiteNews($html);
case 'Fiction Updates':
return $this->collectUpdatesData($html);
case 'Fiction Comments':
return $this->collectCommentsData($html);
}
}
private function collectSiteNews($html)
{
foreach ($html->find('.news_view') as $news) {
$this->items[] = [
'title' => $news->find('h1.title', 0)->plaintext,
'timestamp' => strtotime($this->fixDate($news->find('span[title]', 0)->title)),
'content' => $news->find('.news_text', 0),
];
}
}
private function collectCommentsData($html)
{
foreach ($html->find('article.comment-container') as $article) {
$this->items[] = [
'uri' => $article->find('.comment_link_to_fic > a', 0)->href,
'title' => $article->find('.comment_author', 0)->plaintext,
'author' => $article->find('.comment_author', 0)->plaintext,
'timestamp' => strtotime($this->fixDate($article->find('time[datetime]', 0)->datetime)),
'content' => $article->find('.comment_message', 0),
'enclosures' => [$article->find('img', 0)->src],
];
}
}
private function collectUpdatesData($html)
{
foreach ($html->find('ul.list-of-fanfic-parts > li') as $chapter) {
$item = [
'uri' => $chapter->find('a', 0)->href,
'title' => $chapter->find('a', 0)->plaintext,
'timestamp' => strtotime($this->fixDate($chapter->find('span[title]', 0)->title)),
];
if ($this->getInput('include_contents')) {
$content = getSimpleHTMLDOMCached($item['uri'], 86400, [], [], true, true, DEFAULT_TARGET_CHARSET, false);
$item['content'] = str_replace("\n", '<br>', $content->find('#content', 0)->innertext);
}
$this->items[] = $item;
// Sort by time, descending
usort($this->items, function ($a, $b) {
return $b['timestamp'] - $a['timestamp'];
});
}
}
private function fixDate($date)
{
// FIXME: This list was generated using Google tranlator. Someone who
// actually knows russian should check this list! Please keep in mind
// that month names must match exactly the names returned by Ficbook.
$ru_month = [
'января',
'февраля',
'марта',
'апреля',
'мая',
'июня',
'июля',
'августа',
'сентября',
'октября',
'ноября',
'декабря',
];
$en_month = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
];
$fixed_date = str_replace($ru_month, $en_month, $date);
$fixed_date = str_replace(' г.', '', $fixed_date);
if ($fixed_date === $date) {
return null;
}
return $fixed_date;
}
}