1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
7
|
|
|
|
8
|
|
|
class WeburgClient |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ClientInterface |
12
|
|
|
*/ |
13
|
|
|
private $httpClient; |
14
|
|
|
|
15
|
|
|
public function __construct(ClientInterface $httpClient) |
16
|
|
|
{ |
17
|
|
|
$this->httpClient = $httpClient; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getMovieInfoById($movieId) |
21
|
|
|
{ |
22
|
|
|
$movieUrl = $this->getMovieUrl($movieId); |
23
|
|
|
$body = $this->getUrlBody($movieUrl); |
24
|
|
|
$body = iconv('WINDOWS-1251', 'UTF-8', $body); |
25
|
|
|
$info = $this->getMovieInfo($body); |
26
|
|
|
return $info; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $movieId |
31
|
|
|
* @return array urls of movie (not series) |
32
|
|
|
*/ |
33
|
|
|
public function getMovieTorrentUrlsById($movieId) |
34
|
|
|
{ |
35
|
|
|
$torrentUrl = $this->getMovieTorrentUrl($movieId); |
36
|
|
|
$body = $this->getUrlBody($torrentUrl); |
37
|
|
|
$torrentsUrls = $this->getTorrentsUrls($body); |
38
|
|
|
return $torrentsUrls; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $movieId |
43
|
|
|
* @param $hashes array hashes of torrents from movieInfo |
44
|
|
|
* @param int $daysMax torrents older last days will not matched |
45
|
|
|
* @return array urls of matched torrent files |
46
|
|
|
*/ |
47
|
|
|
public function getSeriesTorrents($movieId, $hashes, $daysMax = 1) |
48
|
|
|
{ |
49
|
|
|
$torrentsUrls = []; |
50
|
|
|
$timestampFrom = strtotime('-' . $daysMax . 'days'); |
51
|
|
|
|
52
|
|
|
$hashes = array_reverse($hashes); |
53
|
|
|
foreach ($hashes as $hash) { |
54
|
|
|
$torrentUrl = $this->getMovieTorrentUrl($movieId, $hash); |
55
|
|
|
$body = $this->getUrlBody($torrentUrl); |
56
|
|
|
|
57
|
|
|
if (!$this->checkTorrentDate($body, $timestampFrom)) { |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
$torrentsUrls = array_merge($torrentsUrls, $this->getTorrentsUrls($body)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $torrentsUrls; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getMoviesIds() |
67
|
|
|
{ |
68
|
|
|
$moviesUrl = 'http://weburg.net/movies/new/?clever_title=1&template=0&last=0'; |
69
|
|
|
|
70
|
|
|
$jsonRaw = $this->getUrlBody($moviesUrl, [ |
71
|
|
|
'Content-Type' => 'text/html; charset=utf-8', |
72
|
|
|
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0', |
73
|
|
|
'X-Requested-With' => 'XMLHttpRequest', |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$moviesJson = json_decode($jsonRaw); |
77
|
|
|
|
78
|
|
|
$moviesIds = $this->getInfoUrls($moviesJson->items); |
79
|
|
|
|
80
|
|
|
return $moviesIds; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getMovieUrl($movieId) |
84
|
|
|
{ |
85
|
|
|
return 'http://weburg.net/movies/info/' . $movieId; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $url |
90
|
|
|
* @param array $headers |
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
92
|
|
|
* @throws \RuntimeException |
93
|
|
|
*/ |
94
|
|
|
private function getUrl($url, $headers = []) |
95
|
|
|
{ |
96
|
|
|
$jar = new CookieJar(); |
97
|
|
|
|
98
|
|
|
$res = $this->httpClient->request('GET', $url, [ |
99
|
|
|
'headers' => $headers, |
100
|
|
|
'cookies' => $jar, |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
if ($res->getStatusCode() != 200) { |
104
|
|
|
throw new \RuntimeException('Error ' . $res->getStatusCode() . 'while get url ' . $url); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $res; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $url |
112
|
|
|
* @param array $headers |
113
|
|
|
* @return \Psr\Http\Message\StreamInterface |
114
|
|
|
* @throws \RuntimeException |
115
|
|
|
*/ |
116
|
|
|
public function getUrlBody($url, $headers = []) |
117
|
|
|
{ |
118
|
|
|
$res = $this->getUrl($url, $headers); |
119
|
|
|
$body = $res->getBody(); |
120
|
|
|
return $body; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $url |
125
|
|
|
* @param $torrentsDir |
126
|
|
|
* @throws \RuntimeException |
127
|
|
|
*/ |
128
|
|
|
public function downloadTorrent($url, $torrentsDir) |
129
|
|
|
{ |
130
|
|
|
$res = $this->getUrl($url); |
131
|
|
|
$torrentBody = $res->getBody(); |
132
|
|
|
|
133
|
|
|
$disposition = $res->getHeader('content-disposition'); |
134
|
|
|
preg_match('/filename="(.*?)"/', $disposition[0], $res); |
135
|
|
|
$filename = $res[1]; |
136
|
|
|
|
137
|
|
|
$filePath = $torrentsDir . '/' . $filename; |
138
|
|
|
file_put_contents($filePath, $torrentBody); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function isTorrentPopular($movieInfo, $commentsMin, $imdbMin, $kinopoiskMin, $votesMin) |
142
|
|
|
{ |
143
|
|
|
return $movieInfo['comments'] >= $commentsMin |
144
|
|
|
|| $movieInfo['rating_imdb'] >= $imdbMin |
145
|
|
|
|| $movieInfo['rating_kinopoisk'] >= $kinopoiskMin |
146
|
|
|
|| $movieInfo['rating_votes'] >= $votesMin; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function cleanMovieId($idOrUrl) |
150
|
|
|
{ |
151
|
|
|
if (preg_match('/^\d+$/', $idOrUrl)) { |
152
|
|
|
return $idOrUrl; |
153
|
|
|
} |
154
|
|
|
preg_match('/^http:\/\/weburg\.net\/(series|movies)\/info\/(\d+)$/', $idOrUrl, $res); |
155
|
|
|
$movieId = count($res) ? $res[2] : null; |
156
|
|
|
return $movieId; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function getMovieTorrentUrl($movieId, $hash = '') |
160
|
|
|
{ |
161
|
|
|
return 'http://weburg.net/ajax/download/movie?' |
162
|
|
|
. ($hash ? 'hash=' . $hash . '&' : '') |
163
|
|
|
. 'obj_id=' . $movieId; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function getMovieInfo($body) |
167
|
|
|
{ |
168
|
|
|
$info = []; |
169
|
|
|
|
170
|
|
|
$checks = [ |
171
|
|
|
'title' => '<title>(.*?) — Weburg<\/title>', |
172
|
|
|
'comments' => 'Комментариев: (\d+)', |
173
|
|
|
'rating_kinopoisk' => 'external-ratings-link_type_kinopoisk.*?>(\d+\.\d+)', |
174
|
|
|
'rating_imdb' => 'external-ratings-link_type_imdb.*?>(\d+\.\d+)', |
175
|
|
|
'rating_votes' => 'count-votes" value="([0-9]+)"', |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
foreach ($checks as $name => $regexp) { |
179
|
|
|
preg_match('/' . $regexp . '/mis', $body, $res); |
180
|
|
|
$info[$name] = count($res) ? $res[1] : null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
preg_match_all('/js-search-button.*?hash="(.*?)"/mis', $body, $res); |
184
|
|
|
$info['hashes'] = count($res) ? $res[1] : null; |
185
|
|
|
|
186
|
|
|
return $info; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param $body |
191
|
|
|
* @param $fromTimestamp |
192
|
|
|
* @return bool|string date if matched, false if not |
193
|
|
|
*/ |
194
|
|
|
private function checkTorrentDate($body, $fromTimestamp) |
195
|
|
|
{ |
196
|
|
|
preg_match('/(\d{2})\.(\d{2})\.(\d{4})/mis', $body, $res); |
197
|
|
|
if (empty($res)) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$torrentTimestamp = mktime(0, 0, 0, $res[2], $res[1], $res[3]); |
202
|
|
|
return $torrentTimestamp >= $fromTimestamp ? $res[0] : false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private function getInfoUrls($body) |
206
|
|
|
{ |
207
|
|
|
preg_match_all('/\/movies\/info\/([0-9]+)/', $body, $res); |
208
|
|
|
$moviesIds = array_unique($res[1]); |
209
|
|
|
return $moviesIds; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param $body |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
private function getTorrentsUrls($body) |
217
|
|
|
{ |
218
|
|
|
preg_match_all('/(http:\/\/.*?gettorrent.*?)"/', $body, $res); |
219
|
|
|
$torrentsUrls = $res[1]; |
220
|
|
|
$torrentsUrls = array_unique($torrentsUrls); |
221
|
|
|
return $torrentsUrls; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|