1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MalScraper\Model\Lists; |
4
|
|
|
|
5
|
|
|
use MalScraper\Helper\Helper; |
6
|
|
|
use MalScraper\Model\MainModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* AllReviewModel class. |
10
|
|
|
*/ |
11
|
|
|
class AllReviewModel extends MainModel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Either anime, manga or bestvoted. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $_type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Page number. |
22
|
|
|
* |
23
|
|
|
* @var int|string |
24
|
|
|
*/ |
25
|
|
|
private $_page; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Default constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $type |
31
|
|
|
* @param string|int $page |
32
|
|
|
* @param string $parserArea |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function __construct($type, $page, $parserArea = '#content') |
37
|
|
|
{ |
38
|
|
|
$this->_type = $type; |
39
|
|
|
$this->_page = $page; |
40
|
|
|
if ($type != 'bestvoted') { |
41
|
|
|
$this->_url = $this->_myAnimeListUrl.'/reviews.php?t='.$type.'&p='.$page; |
42
|
|
|
} else { |
43
|
|
|
$this->_url = $this->_myAnimeListUrl.'/reviews.php?st='.$type.'&p='.$page; |
44
|
|
|
} |
45
|
|
|
$this->_parserArea = $parserArea; |
46
|
|
|
|
47
|
|
|
parent::errorCheck($this); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Default call. |
52
|
|
|
* |
53
|
|
|
* @param string $method |
54
|
|
|
* @param array $arguments |
55
|
|
|
* |
56
|
|
|
* @return array|string|int |
57
|
|
|
*/ |
58
|
|
|
public function __call($method, $arguments) |
59
|
|
|
{ |
60
|
|
|
if ($this->_error) { |
61
|
|
|
return $this->_error; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return call_user_func_array([$this, $method], $arguments); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get type. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function getType() |
73
|
|
|
{ |
74
|
|
|
return $this->_type; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get page. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function getPage() |
83
|
|
|
{ |
84
|
|
|
return $this->_page; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get review user. |
89
|
|
|
* |
90
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $very_bottom_area |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
private function getReviewId($very_bottom_area) |
95
|
|
|
{ |
96
|
|
|
$id = $very_bottom_area->find('a', 0)->href; |
97
|
|
|
$id = explode('?id=', $id); |
98
|
|
|
|
99
|
|
|
return $id[1]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get review source. |
104
|
|
|
* |
105
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $top_area |
106
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $bottom_area |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
private function getReviewSource($top_area, $bottom_area) |
111
|
|
|
{ |
112
|
|
|
$source_area = $top_area->find('.mb8', 1); |
113
|
|
|
|
114
|
|
|
return [ |
115
|
|
|
'type' => $this->getSourceType($source_area), |
116
|
|
|
'id' => $this->getSourceId($source_area), |
117
|
|
|
'title'=> $this->getSourceTitle($source_area), |
118
|
|
|
'image'=> $this->getSourceImage($bottom_area), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get source type. |
124
|
|
|
* |
125
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
private function getSourceType($source_area) |
130
|
|
|
{ |
131
|
|
|
$type = $source_area->find('small', 0)->plaintext; |
132
|
|
|
$type = str_replace(['(', ')'], '', $type); |
133
|
|
|
$this->_type = strtolower($type); |
134
|
|
|
|
135
|
|
|
return strtolower($type); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get source id. |
140
|
|
|
* |
141
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
private function getSourceId($source_area) |
146
|
|
|
{ |
147
|
|
|
$id = $source_area->find('strong a', 0)->href; |
148
|
|
|
$id = explode('/', $id); |
149
|
|
|
|
150
|
|
|
return $id[4]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get source title. |
155
|
|
|
* |
156
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private function getSourceTitle($source_area) |
161
|
|
|
{ |
162
|
|
|
$title = $source_area->find('strong', 0)->plaintext; |
163
|
|
|
|
164
|
|
|
return trim($title); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get source image. |
169
|
|
|
* |
170
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $bottom_area |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
private function getSourceImage($bottom_area) |
175
|
|
|
{ |
176
|
|
|
$image = $bottom_area->find('.picSurround img', 0)->getAttribute('data-src'); |
177
|
|
|
|
178
|
|
|
return Helper::imageUrlCleaner($image); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get review id. |
183
|
|
|
* |
184
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $top_area |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
private function getReviewUser($top_area) |
189
|
|
|
{ |
190
|
|
|
$user = $top_area->find('table', 0); |
191
|
|
|
|
192
|
|
|
return $user->find('td', 1)->find('a', 0)->plaintext; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get review image. |
197
|
|
|
* |
198
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $top_area |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
private function getReviewImage($top_area) |
203
|
|
|
{ |
204
|
|
|
$image = $top_area->find('table', 0); |
205
|
|
|
$image = $image->find('td', 0)->find('img', 0)->src; |
206
|
|
|
|
207
|
|
|
return Helper::imageUrlCleaner($image); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get review helful. |
212
|
|
|
* |
213
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $top_area |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
private function getReviewHelpful($top_area) |
218
|
|
|
{ |
219
|
|
|
$helpful = $top_area->find('table', 0); |
220
|
|
|
$helpful = $helpful->find('td', 1)->find('strong', 0)->plaintext; |
221
|
|
|
|
222
|
|
|
return trim($helpful); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get review date. |
227
|
|
|
* |
228
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $top_area |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
private function getReviewDate($top_area) |
233
|
|
|
{ |
234
|
|
|
$date = $top_area->find('div div', 0); |
235
|
|
|
|
236
|
|
|
return [ |
237
|
|
|
'date' => $date->plaintext, |
238
|
|
|
'time' => $date->title, |
239
|
|
|
]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Get review episode seen. |
244
|
|
|
* |
245
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $top_area |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
private function getReviewEpisode($top_area) |
250
|
|
|
{ |
251
|
|
|
$episode = $top_area->find('div div', 1)->plaintext; |
252
|
|
|
$episode = str_replace(['episodes seen', 'chapters read'], '', $episode); |
253
|
|
|
|
254
|
|
|
return trim($episode); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Get review score. |
259
|
|
|
* |
260
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $bottom_area |
261
|
|
|
* |
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
|
|
private function getReviewScore($bottom_area) |
265
|
|
|
{ |
266
|
|
|
$score = []; |
267
|
|
|
$score_area = $bottom_area->find('table', 0); |
268
|
|
|
if ($score_area) { |
269
|
|
|
foreach ($score_area->find('tr') as $each_score) { |
270
|
|
|
$score_type = strtolower($each_score->find('td', 0)->plaintext); |
271
|
|
|
$score_value = $each_score->find('td', 1)->plaintext; |
272
|
|
|
$score[$score_type] = $score_value; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $score; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get review text. |
281
|
|
|
* |
282
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $bottom_area |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
private function getReviewText($bottom_area) |
287
|
|
|
{ |
288
|
|
|
$useless_area = $bottom_area->find('div', 0); |
289
|
|
|
$useless_area_1 = $useless_area->plaintext; |
290
|
|
|
$useless_area_2 = $useless_area->next_sibling()->plaintext; |
291
|
|
|
$useless_area_3 = $bottom_area->find('div[id^=revhelp_output]', 0)->plaintext; |
292
|
|
|
$useless_area_4 = $bottom_area->find('a[id^=reviewToggle]', 0) ? $bottom_area->find('a[id^=reviewToggle]', 0)->plaintext : null; |
293
|
|
|
$text = str_replace([$useless_area_1, $useless_area_2, $useless_area_3, $useless_area_4], '', $bottom_area->plaintext); |
294
|
|
|
$text = str_replace('<', '<', $text); |
295
|
|
|
|
296
|
|
|
return trim(preg_replace('/\h+/', ' ', $text)); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get anime/mange review. |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
|
|
private function getAllInfo() |
305
|
|
|
{ |
306
|
|
|
$data = []; |
307
|
|
|
$review_area = $this->_parser->find('.borderDark'); |
308
|
|
|
foreach ($review_area as $each_review) { |
309
|
|
|
$tmp = []; |
310
|
|
|
|
311
|
|
|
$top_area = $each_review->find('.spaceit', 0); |
312
|
|
|
$bottom_area = $top_area->next_sibling(); |
313
|
|
|
$very_bottom_area = $bottom_area->next_sibling(); |
314
|
|
|
|
315
|
|
|
$tmp['id'] = $this->getReviewId($very_bottom_area); |
316
|
|
|
$tmp['source'] = $this->getReviewSource($top_area, $bottom_area); |
317
|
|
|
$tmp['username'] = $this->getReviewUser($top_area); |
318
|
|
|
$tmp['image'] = $this->getReviewImage($top_area); |
319
|
|
|
$tmp['helpful'] = $this->getReviewHelpful($top_area); |
320
|
|
|
$tmp['date'] = $this->getReviewDate($top_area); |
321
|
|
|
if ($this->_type == 'anime') { |
322
|
|
|
$tmp['episode'] = $this->getReviewEpisode($top_area); |
323
|
|
|
} else { |
324
|
|
|
$tmp['chapter'] = $this->getReviewEpisode($top_area); |
325
|
|
|
} |
326
|
|
|
$tmp['score'] = $this->getReviewScore($bottom_area); |
327
|
|
|
$tmp['review'] = $this->getReviewText($bottom_area); |
328
|
|
|
|
329
|
|
|
$data[] = $tmp; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return $data; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|