forked from swetorrentking/rartracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchpotato.php
98 lines (82 loc) · 2.54 KB
/
couchpotato.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
<?php
header('Content-Type: application/json');
include('api/secrets.php');
include('api/Config.php');
include('api/Helper.php');
include('api/Torrent.php');
try {
$db = new PDO($database.':host='.$host.';dbname='.$dbname.';charset=utf8', $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
error($e->getMessage());
}
$passkey = $_GET["passkey"];
$imdbid = $_GET["imdbid"];
$search = $_GET["search"];
$categories = [
Config::$categories["DVDR_PAL"]["id"],
Config::$categories["DVDR_CUSTOM"]["id"],
Config::$categories["DVDR_TV"]["id"],
Config::$categories["MOVIE_720P"]["id"],
Config::$categories["MOVIE_1080P"]["id"],
Config::$categories["TV_720P"]["id"],
Config::$categories["TV_1080P"]["id"],
Config::$categories["BLURAY"]["id"],
Config::$categories["MOVIE_4K"]["id"]
];
if (!preg_match("/^[a-z0-9]{32}$/", $passkey) || (empty($imdbid) && empty($search))) {
error("Incorrect parameters.");
}
/* Just search for imdbid */
if (empty($search)) {
$search = $imdbid;
}
$sth = $db->prepare("SELECT id FROM users WHERE passkey = ? AND enabled = 'yes'");
$sth->bindParam(1, $passkey);
$sth->execute();
if (!$sth->fetch()) {
error("Permission denied.");
}
$torrents = new Torrent($db);
list($result, $total) = $torrents->search(array("searchText" => $search, "categories" => $categories));
$torr = array();
foreach($result as &$res) {
$torrent = array(
"release_name" => $res["name"],
"torrent_id" => $res["id"],
"details_url" => Config::SITE_URL . "/torrent/" . $res["id"] . "/" . $res["name"],
"download_url" => Config::SITE_URL . "/api/v1/torrents/download/". $res["id"]. "/ " . $passkey,
"imdb_id" => $res["imdbid2"],
"freeleech" => (bool)$res["frileech"],
"type" => typeByCategory($res["category"]),
"size" => bitsToMb($res["size"]),
"leechers" => $res["leechers"],
"seeders" => $res["seeders"]
);
array_push($torr, $torrent);
}
$response = array(
"results" => $torr,
"total_results" => $total
);
echo json_encode($response, JSON_NUMERIC_CHECK);
function bitsToMb($bits) {
return round($bits/1024/1024);
}
function typeByCategory($category) {
switch($category) {
case Config::$categories["DVDR_PAL"]["id"]:
case Config::$categories["DVDR_CUSTOM"]["id"]:
case Config::$categories["MOVIE_720P"]["id"]:
case Config::$categories["MOVIE_1080P"]["id"]:
case Config::$categories["BLURAY"]["id"]:
case Config::$categories["MOVIE_4K"]["id"]:
return "movie";
default:
return "show";
}
}
function error($err) {
echo json_encode(Array("error" => $err));
die();
}