-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.php
347 lines (331 loc) · 13.2 KB
/
search.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
* TreasureShip
* https://cndl.synology.cn/download/Document/Software/DeveloperGuide/Package/DownloadStation/All/enu/DLM_Guide.pdf
* Developer Notes:
*
* $ts_cloud_url: TPB_TreasureShip cloud api:
* >>: format: tpb@<web:url><api:url><trackers:url>
* >>: example: tpb@<web:http://tpb.com><api:http://tpb.com/api/q.php?q=><trackers:http://test.com/trackers.txt>
*/
class TreasureShip{
public $debug = false;
private $opts = ["ssl" => ["verify_peer"=>false, "verify_peer_name"=>false,]];
private $default_api_url = "https://apibay.org/q.php?q="; // default
private $default_api_host = "https://thepiratebay.org"; // default
private $default_trackers_url = 'https://ngosang.github.io/trackerslist/trackers_best.txt'; // default
private $default_trackers = '&tr=udp://tracker.openbittorrent.com:6969/announce&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://opentracker.io:6969/announce&tr=https://1337.abcvg.info:443/announce&tr=http://ipv6.rer.lol:6969/announce';
#private $ts_cloud_url = "https://raw.githubusercontent.com/malimaliao/Synology-DLM-for-TPB_TreasureShip/main/ts.css"; // cloud
private $ts_cloud_url = 'https://malimaliao.github.io/Synology-DLM-for-TPB_TreasureShip/ts.css'; // mirror form github, for cloud.
private $tpb_api = '';
private $tpb_host='';
private $trackers_url = '';
private $trackers_list = '';
public function __construct(){
}
private function DebugLog($str){
if ($this->debug == true) {
file_put_contents('TreasureShip.debug.log', $str, FILE_APPEND);
}
}
// Synology DownloadStation api function
public function VerifyAccount($username, $password){
$ret = FALSE;
$username = trim($username);
$this->DebugLog("TPB(VerifyAccount username): ".$username.PHP_EOL);
if ($username != "") {
$result = file_get_contents($username,false, stream_context_create($this->opts));
$this->DebugLog("TPB(VerifyAccount username response): ".PHP_EOL.PHP_EOL.$result.PHP_EOL.PHP_EOL);
if (strpos($result, '0000000000000000000000000000000000000000') !== false) {
$ret = TRUE;
$rem = 'ok';
$this->tpb_api = $username; //update
}else{
$rem = 'error';
}
$this->DebugLog("TPB(VerifyAccount username echo):" .$rem.PHP_EOL);
}
return $ret;
}
// Synology DownloadStation api function
public function prepare($curl, $query, $username, $password)
{
$query = trim($query);
$username = trim($username);
$password = trim($password);
# init
if($this->tpb_api == ''){
if ($username == "" or $password == "") {
$this->DebugLog("TPB(No account password mapping used): ".PHP_EOL);
// get ts cloud b64
$this->DebugLog("TPB(get start): ".$this->ts_cloud_url.PHP_EOL);
$tmp = file_get_contents($this->ts_cloud_url,false, stream_context_create($this->opts));
$this->DebugLog('@html:'.$tmp.PHP_EOL);
// ts_cloud_api format: $tpb:tpb_api_url@trackers:trackers_url_txt@from:tpb_host@
if(preg_match('/tpb@<web:(.+?)><api:(.+?)><trackers:(.+?)>/i',$tmp,$data)){
$tpb_from = $data[1];
$tpb_api = $data[2];
$trackers_url = $data[3];
$this->DebugLog("TPB(get success, tpb): ".$tpb_api.PHP_EOL);
$this->DebugLog("TPB(get success, trackers): ".$trackers_url.PHP_EOL);
}else{
$tpb_api = $this->default_api_url;
$trackers_url = $this->default_trackers_url;
$tpb_from = $this->default_api_host;
$this->DebugLog("TPB(get bad and tpb use default): ".$tpb_api.PHP_EOL);
$this->DebugLog("TPB(get bad and trackers use default): ".$trackers_url.PHP_EOL);
}
}else{
// check username_url
if(preg_match("/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is",$username)){
$tpb_api = $username;
$u = parse_url($username);
$tpb_from = $u['scheme'].'://'.$u['host'];
$this->DebugLog("TPB(tpb api replace with username_url): ".$tpb_api.PHP_EOL);
}else{
$tpb_api = $this->default_api_url;
$tpb_from = $this->default_api_host;
$this->DebugLog("TPB(username_url check bad and use default): ".$tpb_api.PHP_EOL);
}
# check password_url
if(preg_match("/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is",$password)){
$trackers_url = $password;
$this->DebugLog("TPB(trackers replace with password_url): ".$trackers_url.PHP_EOL);
}else{
$trackers_url = $this->default_trackers_url;
$this->DebugLog("TPB(password_url check bad and use default): ".$trackers_url.PHP_EOL);
}
}
$this->tpb_api = $tpb_api; // update
$this->trackers_url = $trackers_url; // update
$this->tpb_host = $tpb_from; // update
$this->DebugLog("TPB(tpb host source echo): ".$tpb_from.PHP_EOL);
}
if($this->trackers_list == ''){
$this->trackers_list = $this->format_tpb_trackers($this->trackers_url); // update
}
# start
$url = sprintf($this->tpb_api.'%s&cat=', urlencode($query));
$this->DebugLog(PHP_EOL.'@TPB start: '.$url.PHP_EOL);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.37');
}
// Synology DownloadStation api function
public function parse($plugin, $response)
{
$response = str_replace(array("\r\n", "\r", "\n"), "", $response); //抛弃所有换行便于正则匹配
$this->DebugLog('@TPB response: '.PHP_EOL.$response.PHP_EOL.PHP_EOL);
$items = json_decode($response,true);
$res = 0;
if($items != null){
$this->DebugLog(PHP_EOL.PHP_EOL.'@TPB format: '.PHP_EOL);
foreach ($items as $item){
$title = $item['name'];
$hash = $item['info_hash'];
$leechers = $item['leechers'];
$seeders = $item['seeders'];
$datetime = date('Y-m-d H:i:s',(int)$item['added']);
$size = (int)$item['size'];
$category = $this->format_tpb_category($item['category']);
$page = $this->tpb_host."/description.php?id=".$item['id'];
$download = "magnet:?xt=urn:btih:".$hash;
$download .= '&dn='.urlencode($title);
$download .= $this->trackers_list;
# push Plugin
if ($title != "") {
if ($this->debug == true) {
// out debug
$this->DebugLog($title.'|'.$download.'|'.$size.'|'.$datetime.'|'.$page.'|'.$hash.'|'.$seeders.'|'.$leechers.'|'.$category.PHP_EOL);
}else{ // out plugin
$plugin->addResult($title, $download, $size, $datetime, $page, $hash, $seeders, $leechers, $category);
}
$res++;
}
}
}
return $res;
}
private function format_tpb_category($category_code){
switch ($category_code){
case '1':
$category = 'Audio';
break;
case '2':
$category = 'Video';
break;
case '3':
$category = 'Video';
break;
case '4':
$category = 'Games';
break;
case '5':
$category = 'Porn';
break;
case '6':
$category = 'Other';
break;
case '101':
$category = 'Music';
break;
case '102':
$category = 'Audio Books';
break;
case '103':
$category = 'Sound clips';
break;
case '104':
$category = 'FLAC';
break;
case '199':
$category = 'Other';
break;
case '201':
$category = 'Movies';
break;
case '202':
$category = 'Movies DVDR';
break;
case '203':
$category = 'Music videos';
break;
case '204':
$category = 'Movie Clips';
break;
case '205':
$category = 'TV-Shows';
break;
case '206':
$category = 'Handheld';
break;
case '207':
$category = 'HD Movies';
break;
case '208':
$category = 'HD TV-Shows';
break;
case '209':
$category = '3D';
break;
case '299':
$category = 'Other';
break;
case '301':
$category = 'Windows';
break;
case '302':
$category = 'Mac/Apple';
break;
case '303':
$category = 'UNIX';
break;
case '304':
$category = 'Handheld';
break;
case '305':
$category = 'IOS(iPad/iPhone)';
break;
case '306':
$category = 'Android';
break;
case '399':
$category = 'Other OS';
break;
case '401':
$category = 'PC';
break;
case '402':
$category = 'Mac/Apple';
break;
case '403':
$category = 'PSx';
break;
case '404':
$category = 'XBOX360';
break;
case '405':
$category = 'Wii';
break;
case '406':
$category = 'Handheld';
break;
case '407':
$category = 'IOS(iPad/iPhone)';
break;
case '408':
$category = 'Android';
break;
case '499':
$category = 'Other OS';
break;
case '501':
$category = 'Movies';
break;
case '502':
$category = 'Movies DVDR';
break;
case '503':
$category = 'Pictures';
break;
case '504':
$category = 'Games';
break;
case '505':
$category = 'HD-Movies';
break;
case '506':
$category = 'Movie Clips';
break;
case '599':
$category = 'Other';
break;
case '601':
$category = 'E-books';
break;
case '602':
$category = 'Comics';
break;
case '603':
$category = 'Pictures';
break;
case '604':
$category = 'Covers';
break;
case '605':
$category = 'Physibles';
break;
case '699':
$category = 'Other';
break;
default:
$category = '';
}
return $category;
}
private function format_tpb_trackers($trackers_cloud_url){
$trackers = urlencode($this -> default_trackers);
if(preg_match("/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is",$trackers_cloud_url)){
$result = file_get_contents($trackers_cloud_url,false, stream_context_create($this->opts));
if (strpos($result, 'announce') !== false) {
$lines = preg_split('/[\s]+/',$result);
if(count($lines)>0){
$trackers = ''; // ret
foreach ($lines as $line){
if(trim($line) != ''){
$trackers .= '&tr='.urlencode(trim($line));
}
}
}
}
}
$this->DebugLog(PHP_EOL.PHP_EOL.'@trackers list print: '.PHP_EOL.$trackers.PHP_EOL.PHP_EOL);
return $trackers;
}
}