forked from eustasy/Phoenix
/
function.torrent.scrape.php
83 lines (72 loc) · 2.06 KB
/
function.torrent.scrape.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
<?php
function torrent_scrape() {
global $connection, $settings;
require_once __DIR__.'/once.db.connect.php';
require_once __DIR__.'/function.mysqli.fetch.once.php';
// select seeders and leechers
$query = '
SELECT
`p`.`info_hash` AS `info_hash`,
SUM(`p`.`state`=\'1\') AS `seeders`,
SUM(`p`.`state`=\'0\') AS `leechers`,
`t`.`downloads` AS `downloads`
FROM `'.$settings['db_prefix'].'peers` AS `p`
LEFT JOIN `'.$settings['db_prefix'].'torrents` AS `t`
ON `p`.`info_hash`=`t`.`info_hash`
WHERE `p`.`info_hash`=\''.$_GET['info_hash'].'\';';
$scrape = mysqli_fetch_once($query);
if ( !$scrape ) {
tracker_error('Unable to scrape for that torrent.');
} else {
$scrape['seeders'] = intval($scrape['seeders']);
$scrape['leechers'] = intval($scrape['leechers']);
$scrape['downloads'] = intval($scrape['downloads']);
$scrape['peers'] = $scrape['seeders'] + $scrape['leechers'];
// XML
if ( isset($_GET['xml']) ) {
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'.
'<torrent>'.
'<info_hash>'.$_GET['info_hash'] .'</info_hash>'.
'<seeders>' .$scrape['seeders'] .'</seeders>'.
'<leechers>' .$scrape['leechers'] .'</leechers>'.
'<peers>' .$scrape['peers'] .'</peers>'.
'<downloads>'.$scrape['downloads'].'</downloads>'.
'</torrent>';
// JSON
} else if ( isset($_GET['json']) ) {
header('Content-Type: application/json');
echo json_encode(
array(
'torrent' => array(
'info_hash' => $_GET['info_hash'],
'seeders' => $scrape['seeders'],
'leechers' => $scrape['leechers'],
'peers' => $scrape['peers'],
'downloads' => $scrape['downloads'],
),
)
);
} else {
$echo = 'd
5:files
d
20:'.hex2bin($_GET['info_hash']).'
d
8:complete
i'.$scrape['seeders'].'e
10:downloaded
i'.$scrape['downloads'].'e
10:incomplete
i'.$scrape['leechers'].'e
e
e
e';
if ( isset($_GET['verbose']) ) {
echo $echo;
} else {
echo preg_replace('/\s+/', '', $echo);
}
}
}
}