1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MalScraper\Model\General; |
4
|
|
|
|
5
|
|
|
use MalScraper\Helper\Helper; |
6
|
|
|
use MalScraper\Model\MainModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* RecommendationModel class. |
10
|
|
|
*/ |
11
|
|
|
class RecommendationModel extends MainModel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Either anime or manga. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $_type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Id of the first anime/manga. |
22
|
|
|
* |
23
|
|
|
* @var int|string |
24
|
|
|
*/ |
25
|
|
|
private $_id1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Id of the first anime/manga. |
29
|
|
|
* |
30
|
|
|
* @var int|string |
31
|
|
|
*/ |
32
|
|
|
private $_id2; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Default constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $type |
38
|
|
|
* @param string|int $id1 |
39
|
|
|
* @param string|int $id2 |
40
|
|
|
* @param string $parserArea |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function __construct($type, $id1, $id2, $parserArea = '#content') |
45
|
|
|
{ |
46
|
|
|
$this->_type = $type; |
47
|
|
|
$this->_id1 = $id1; |
48
|
|
|
$this->_id2 = $id2; |
49
|
|
|
$this->_url = $this->_myAnimeListUrl.'/recommendations/'.$type.'/'.$id1.'-'.$id2; |
50
|
|
|
$this->_parserArea = $parserArea; |
51
|
|
|
|
52
|
|
|
parent::errorCheck($this); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Default call. |
57
|
|
|
* |
58
|
|
|
* @param string $method |
59
|
|
|
* @param array $arguments |
60
|
|
|
* |
61
|
|
|
* @return array|string|int |
62
|
|
|
*/ |
63
|
|
|
public function __call($method, $arguments) |
64
|
|
|
{ |
65
|
|
|
if ($this->_error) { |
66
|
|
|
return $this->_error; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return call_user_func_array([$this, $method], $arguments); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get type. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
private function getType() |
78
|
|
|
{ |
79
|
|
|
return $this->_type; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get id1. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function getId1() |
88
|
|
|
{ |
89
|
|
|
return $this->_id1; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get id2. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function getId2() |
98
|
|
|
{ |
99
|
|
|
return $this->_id2; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get source. |
104
|
|
|
* |
105
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $each_recom |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
private function getSource($each_recom) |
110
|
|
|
{ |
111
|
|
|
$source = []; |
112
|
|
|
$source_area = $each_recom->find('table tr', 0); |
113
|
|
|
$source['liked'] = $this->getSourceLiked($source_area, 0); |
114
|
|
|
$source['recommendation'] = $this->getSourceLiked($source_area, 1); |
115
|
|
|
|
116
|
|
|
return $source; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get source liked. |
121
|
|
|
* |
122
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
123
|
|
|
* @param int $key |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
private function getSourceLiked($source_area, $key) |
128
|
|
|
{ |
129
|
|
|
$liked = []; |
130
|
|
|
$source_area = $source_area->find('td', $key); |
131
|
|
|
$liked['id'] = $this->getSourceId($source_area); |
132
|
|
|
$liked['title'] = $this->getSourceTitle($source_area); |
133
|
|
|
$liked['type'] = $this->getType(); |
134
|
|
|
$liked['image'] = $this->getSourceImage($source_area); |
135
|
|
|
|
136
|
|
|
return $liked; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get source id. |
141
|
|
|
* |
142
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
private function getSourceId($source_area) |
147
|
|
|
{ |
148
|
|
|
$id = $source_area->find('a', 0)->href; |
149
|
|
|
$id = explode('/', $id); |
150
|
|
|
|
151
|
|
|
return $id[4]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get source title. |
156
|
|
|
* |
157
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
private function getSourceTitle($source_area) |
162
|
|
|
{ |
163
|
|
|
return $source_area->find('strong', 0)->plaintext; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get source image. |
168
|
|
|
* |
169
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $source_area |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
private function getSourceImage($source_area) |
174
|
|
|
{ |
175
|
|
|
$image = $source_area->find('img', 0)->src; |
176
|
|
|
|
177
|
|
|
return Helper::imageUrlCleaner($image); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get recommendation text. |
182
|
|
|
* |
183
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $recommendation_area |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
private function getRecom($recommendation_area) |
188
|
|
|
{ |
189
|
|
|
$recommendation = []; |
190
|
|
|
foreach ($recommendation_area->find('.borderClass') as $each_recom) { |
191
|
|
|
$tmp = []; |
192
|
|
|
|
193
|
|
|
$tmp['username'] = $this->getRecomUser($each_recom); |
194
|
|
|
$tmp['recommendation'] = $this->getRecomText($each_recom); |
195
|
|
|
|
196
|
|
|
$recommendation[] = $tmp; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $recommendation; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get recommendation user. |
204
|
|
|
* |
205
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $each_recom |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
private function getRecomUser($each_recom) |
210
|
|
|
{ |
211
|
|
|
return $each_recom->find('a', 1)->plaintext; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get recommendation text. |
216
|
|
|
* |
217
|
|
|
* @param \simplehtmldom_1_5\simple_html_dom $each_recom |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
private function getRecomText($each_recom) |
222
|
|
|
{ |
223
|
|
|
$text = $each_recom->find('span', 0)->plaintext; |
224
|
|
|
$text = preg_replace('/\s{2,}/', "\n", $text); |
225
|
|
|
|
226
|
|
|
return $text; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get anime/mange recommendation. |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
private function getAllInfo() |
235
|
|
|
{ |
236
|
|
|
$data = []; |
237
|
|
|
$recommendation_area = $this->_parser->find('.borderDark', 0); |
238
|
|
|
|
239
|
|
|
$data['source'] = $this->getSource($recommendation_area); |
240
|
|
|
$data['recommendation'] = $this->getRecom($recommendation_area); |
241
|
|
|
|
242
|
|
|
return $data; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|