Repository navigation
Expand file tree
/
Copy pathYgg.php
More file actions
executable file
·491 lines (437 loc) · 13.4 KB
/
Copy pathYgg.php
File metadata and controls
executable file
·491 lines (437 loc) · 13.4 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* POC permettant de parcourir le site www.yggtorrent.com et d'en extraire les torrents recherchés
* User: Anthony Saugrain
* Date: 26/07/2016
* Time: 18:08
* Last update : 14/09/2017
*/
require_once('lib/simple_html_dom.php');
class Ygg
{
const BASE_URL = "https://ww1.yggtorrent.is/";
const CATEGORY_MOVIES = 2145;
const CATEGORY_AUDIO = 2139;
const CATEGORY_APPS = 2144;
const CATEGORY_VIDEOGAMES = 2142;
const CATEGORY_EBOOKS = 2140;
const CATEGORY_EMULATION = 2141;
const CATEGORY_GPS = 2143;
const CATEGORY_ADULT = 2188;
private $up;
private $down;
private $torrents;
private $html;
private $search;
private $pagination;
private $login;
private $password;
private $order;
private $url;
private $checkWithoutPagination;
/**
* Ygg constructor.
* @param string $search
* @param int $pagination
*/
public function __construct($search = '', $pagination = 1)
{
$this->up = '';
$this->down = '';
$this->torrents = array();
$this->search = $search;
$this->pagination = $pagination;
$this->login = '****';
$this->password = '****';
$this->order = 'seeds';
$this->checkWithoutPagination = false;
// Create needed directories if needed
$this->checkForDirectories('tmp');
$this->checkForDirectories('dl');
}
public function getLogin(){
return $this->login;
}
/**
* Get category id by category name
* @param $category
* @return bool|int
*/
public static function getCategoryId($category)
{
switch ($category) {
case 'movies':
$category_id = self::CATEGORY_MOVIES;
break;
case 'audio':
$category_id = self::CATEGORY_AUDIO;
break;
case 'apps':
$category_id = self::CATEGORY_APPS;
break;
case 'videogames':
$category_id = self::CATEGORY_VIDEOGAMES;
break;
case 'ebooks':
$category_id = self::CATEGORY_EBOOKS;
break;
case 'emulation':
$category_id = self::CATEGORY_EMULATION;
break;
case 'gps':
$category_id = self::CATEGORY_GPS;
break;
case 'adult':
$category_id = self::CATEGORY_ADULT;
break;
default:
$category_id = false;
break;
}
return $category_id;
}
/**
* @return string ratio up
*/
public function getUp()
{
return $this->up;
}
/**
* @return string ratio down
*/
public function getDown()
{
return $this->down;
}
/**
* @return array torrents
*/
public function getTorrents()
{
return $this->torrents;
}
/**
* Login and store cookie
*/
public function login()
{
try {
if ($this->call('login', '/user/login') == "") {
if (($page = $this->call('basic', '')) !== false) {
$this->html = $this->open($page);
if ($this->findLink('/user/account')) {
if (!$this->findRatio()) {
throw new Exception('Unable to find ratio');
}
return true;
} else {
throw new Exception('Unable to login');
}
}
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Generic cURL call
* @param $type
* @param $path
* @return mixed
* @throws Exception
*/
private function call($type, $path)
{
try {
// create curl resource
$ch = curl_init();
// Check if we already have FQDN
$url = ((substr(self::BASE_URL, 0, 4) == 'http') ? self::BASE_URL : '') . $path;
//var_dump($url);die();
if ($type == 'login') {
$datas = "id=" . urlencode($this->login) . "&pass=" . urlencode($this->password);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
curl_setopt($ch, CURLOPT_POST, true);
}
// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$cookie_file_path = "./tmp/cookies.txt";
//return the transfer as a string
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// $output contains the output string
$output = curl_exec($ch);
// echo $output;
if ($type == 'download') {
preg_match('/(([0-9]){3,8})/', $path, $matches);
if(isset($matches[1]))
$idygg=$matches[1];
else
$idygg=null;
$destination = "./dl/".$idygg.".torrent";
$file = fopen($destination, "w+");
fputs($file, $output);
fclose($file);
}
// close curl resource to free up system resources
curl_close($ch);
return $output;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Open a html file with simple html dom
* @param $file
* @return simple_html_dom
* @throws Exception
*/
private function open($file)
{
try {
$html = new simple_html_dom();
$html->load($file);
return $html;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Find a particular link
* @param $term
* @return bool
* @throws Exception
*/
private function findLink($term)
{
// echo $this->html;
try {
$links = $this->html->find('a');
foreach ($links as $link) {
if (strpos($link->href, $term) !== false) {
return true;
}
}
return false;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Extract ratios Up / Down
* @return bool
* @throws Exception
*/
private function findRatio()
{
try {
$links = $this->html->find('#top_panel .ct ul li strong');
foreach ($links as $link) {
if (strpos($link->innertext, 'GB') !== false || strpos($link->innertext, 'TB') !== false) {
if (strpos($link->children(0)->getAttribute('class'), 'ico_upload') !== false) {
$this->up = str_replace("GB", '', trim($link->innertext));
} else {
$this->down = str_replace("GB", '', trim($link->innertext));
}
}
}
if ($this->up != '' && $this->down != '') {
return true;
}
return false;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Search a torrent
* @throws Exception
*/
public function searchTorrent()
{
try {
$term = urlencode($this->search);
$this->url = '/engine/search?name='. $term.'&description=&file=&uploader=&category=2145&sub_category=all&do=search';
$this->loopForTorrent();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Loop and find torrent in given url
* @param $url
*/
private function loopForTorrent()
{
for ($i = 1; $i <= $this->pagination; $i++) {
$page = $this->call('basic', $this->url . '&page=' . $i);
$this->html = $this->open($page);
$this->extractTorrents();
}
}
/**
* Extract founded torrents
* @return bool
* @throws Exception
*/
private function extractTorrents()
{
try {
$lines = $this->html->find('.results table tbody tr');
foreach ($lines as $key => $line) {
// Do not handle first line (header)
if ($key > 0) {
// Extract torrent link & name
$links = $line->children(1)->find('a');
foreach ($links as $key => $link) {
if ($key == 0) {
$name = $link->innertext;
}
preg_match('/\/(([0-9]){3,8})/', $link->href, $matches);
if(isset($matches[1]))
$idygg=$matches[1];
else
$idygg=null;
$href="/engine/download_torrent?id=".$idygg;
}
if (is_null($href) || is_null($name)) {
continue;
}
// Extract size
$age = $line->children(4)->innertext;
$size = $line->children(5)->innertext;
// Extract seeds
$completed = $line->children(6)->innertext;
$seeds = $line->children(7)->innertext;
// Extract leechs
$leechs = $line->children(8)->innertext;
$this->torrents[] = array(
'name' => $name,
'href' => $href,
'size' => $size,
'age' => $age,
'completed' => $completed,
'seeds' => $seeds,
'leechs' => $leechs
);
}
}
if (count($this->torrents) > 0) {
if ($this->orderBy($this->order)) {
return true;
}
} else {
// If no result with pagination, check witout pagination (just one time to avoid loop..)
if (!$this->checkWithoutPagination) {
$this->checkWithoutPagination = true;
$page = $this->call('basic', $this->url);
$this->html = $this->open($page);
$this->extractTorrents();
}
}
return false;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Sort torrents
* @return bool
* @throws Exception
*/
public function orderBy()
{
try {
$order = $this->order;
usort($this->torrents, function ($a, $b) use ($order) {
return $b[$order] - $a[$order];
});
return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Search torrent of the moment in given category
* @param $category
* @throws Exception
*/
public function searchMoment($category)
{
try {
$this->loopForTorrent('/torrents/popular?category=' . $category);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Search torrent of the last day in given category
* @param $category
* @throws Exception
*/
public function searchYesterday($category)
{
try {
$this->loopForTorrent('/torrents/yesterday?category=' . $category);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Search torrent of the day in given category
* @param $category
* @throws Exception
*/
public function searchToday($category)
{
try {
$this->loopForTorrent('/torrents/today?category=' . $category);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Download given torrent
* @param $url
* @return bool
* @throws Exception
*/
public function download($url)
{
try {
if ($this->call('download', $url) !== false) {
return true;
}
return false;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
/**
* Check and create given directory
* @param $dirname
* @throws Exception
**/
public function checkForDirectories($dirname)
{
try{
if(!is_dir($dirname)) {
mkdir($dirname, 0755);
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}