1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MalScraper\Model\Search; |
4
|
|
|
|
5
|
|
|
use MalScraper\Helper\Helper; |
6
|
|
|
use MalScraper\Model\MainModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* SearchAnimeMangaModel class. |
10
|
|
|
*/ |
11
|
|
|
class SearchAnimeMangaModel extends MainModel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Either anime or manga. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $_type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Search query. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $_query; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Page number. |
29
|
|
|
* |
30
|
|
|
* @var int|string |
31
|
|
|
*/ |
32
|
|
|
private $_page; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Default constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $type |
38
|
|
|
* @param string $query |
39
|
|
|
* @param int|string $page |
40
|
|
|
* @param string $parserArea |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function __construct($type, $query, $page, $parserArea = 'div[class^=js-categories-seasonal]') |
45
|
|
|
{ |
46
|
|
|
$this->_type = $type; |
47
|
|
|
$this->_query = $query; |
48
|
|
|
$this->_page = 50 * ($page - 1); |
49
|
|
|
|
50
|
|
|
if ($type == 'anime') { |
51
|
|
|
$this->_url = $this->_myAnimeListUrl.'/anime.php?q='.$query.'&show='.$this->_page; |
52
|
|
|
} else { |
53
|
|
|
$this->_url = $this->_myAnimeListUrl.'/manga.php?q='.$query.'&show='.$this->_page; |
54
|
|
|
} |
55
|
|
|
$this->_parserArea = $parserArea; |
56
|
|
|
|
57
|
|
|
parent::errorCheck($this); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Default call. |
62
|
|
|
* |
63
|
|
|
* @param string $method |
64
|
|
|
* @param array $arguments |
65
|
|
|
* |
66
|
|
|
* @return array|string|int |
67
|
|
|
*/ |
68
|
|
|
public function __call($method, $arguments) |
69
|
|
|
{ |
70
|
|
|
if ($this->_error) { |
71
|
|
|
return $this->_error; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return call_user_func_array([$this, $method], $arguments); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get image. |
79
|
|
|
* |
80
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $result_area |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function getImage($result_area) |
85
|
|
|
{ |
86
|
|
|
$image = $result_area->find('td', 0)->find('a img', 0)->getAttribute('data-src'); |
87
|
|
|
|
88
|
|
|
return Helper::imageUrlCleaner($image); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get id. |
93
|
|
|
* |
94
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $name_area |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function getId($name_area) |
99
|
|
|
{ |
100
|
|
|
$id = $name_area->find('div[id^=sarea]', 0)->id; |
101
|
|
|
|
102
|
|
|
return str_replace('sarea', '', $id); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get title. |
107
|
|
|
* |
108
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $name_area |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
private function getTitle($name_area) |
113
|
|
|
{ |
114
|
|
|
return $name_area->find('strong', 0)->plaintext; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get summary. |
119
|
|
|
* |
120
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $name_area |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function getSummary($name_area) |
125
|
|
|
{ |
126
|
|
|
$summary = $name_area->find('.pt4', 0)->plaintext; |
127
|
|
|
|
128
|
|
|
return str_replace('read more.', '', $summary); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get type. |
133
|
|
|
* |
134
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $result_area |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
private function getType($result_area) |
139
|
|
|
{ |
140
|
|
|
$type = $result_area->find('td', 2)->plaintext; |
141
|
|
|
|
142
|
|
|
return trim($type); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get episode. |
147
|
|
|
* |
148
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $result_area |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
private function getEpisode($result_area) |
153
|
|
|
{ |
154
|
|
|
$episode = $result_area->find('td', 3)->plaintext; |
155
|
|
|
$episode = trim($episode); |
156
|
|
|
|
157
|
|
|
return $episode == '-' ? '' : $episode; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get score. |
162
|
|
|
* |
163
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $result_area |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
private function getScore($result_area) |
168
|
|
|
{ |
169
|
|
|
$score = $result_area->find('td', 4)->plaintext; |
170
|
|
|
$score = trim($score); |
171
|
|
|
|
172
|
|
|
return $score == 'N/A' ? '' : $score; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get result list. |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
private function getAllInfo() |
181
|
|
|
{ |
182
|
|
|
$data = []; |
183
|
|
|
$result_table = $this->_parser->find('table', 0); |
184
|
|
|
$result_area = $result_table->find('tr', 0)->next_sibling(); |
185
|
|
|
while (true) { |
186
|
|
|
$result = []; |
187
|
|
|
|
188
|
|
|
$name_area = $result_area->find('td', 1); |
189
|
|
|
|
190
|
|
|
$result['image'] = $this->getImage($result_area); |
191
|
|
|
$result['id'] = $this->getId($name_area); |
192
|
|
|
$result['title'] = $this->getTitle($name_area); |
193
|
|
|
$result['summary'] = $this->getSummary($name_area); |
194
|
|
|
$result['type'] = $this->getType($result_area); |
195
|
|
|
|
196
|
|
|
if ($this->_type == 'anime') { |
197
|
|
|
$result['episode'] = $this->getEpisode($result_area); |
198
|
|
|
} else { |
199
|
|
|
$result['volume'] = $this->getEpisode($result_area); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$result['score'] = $this->getScore($result_area); |
203
|
|
|
|
204
|
|
|
$data[] = $result; |
205
|
|
|
|
206
|
|
|
$result_area = $result_area->next_sibling(); |
207
|
|
|
if (!$result_area) { |
208
|
|
|
break; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $data; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|