/
rarbg.py
189 lines (151 loc) · 6.97 KB
/
rarbg.py
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
# coding=utf-8
# Author: Dustyn Gibson <miigotu@gmail.com>
#
#
# This file is part of SickRage.
#
# SickRage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SickRage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import datetime
import time
import sickbeard
from sickbeard import logger, tvcache
from sickbeard.common import cpu_presets
from sickbeard.indexers.indexer_config import INDEXER_TVDB
from sickrage.helper.common import convert_size, try_int
from sickrage.providers.torrent.TorrentProvider import TorrentProvider
class RarbgProvider(TorrentProvider): # pylint: disable=too-many-instance-attributes
def __init__(self):
TorrentProvider.__init__(self, "Rarbg")
self.public = True
self.ratio = None
self.minseed = None
self.ranked = None
self.sorting = None
self.minleech = None
self.token = None
self.token_expires = None
# Spec: https://torrentapi.org/apidocs_v2.txt
self.url = "https://rarbg.com"
self.urls = {"api": "http://torrentapi.org/pubapi_v2.php"}
self.proper_strings = ["{{PROPER|REPACK}}"]
self.cache = tvcache.TVCache(self, min_time=10) # only poll RARBG every 10 minutes max
def login(self):
if self.token and self.token_expires and datetime.datetime.now() < self.token_expires:
return True
login_params = {
"get_token": "get_token",
"format": "json",
"app_id": "sickrage2"
}
# Maximum requests allowed are 1req/2sec
time.sleep(max(2, cpu_presets[sickbeard.CPU_PRESET]))
response = self.get_url(self.urls["api"], params=login_params, timeout=30, returns="json")
if not response:
logger.log("Unable to connect to provider", logger.WARNING)
return False
self.token = response.get("token", None)
self.token_expires = datetime.datetime.now() + datetime.timedelta(minutes=14) if self.token else None
return self.token is not None
def search(self, search_strings, age=0, ep_obj=None): # pylint: disable=too-many-branches, too-many-locals, too-many-statements
results = []
if not self.login():
return results
search_params = {
"app_id": "sickrage2",
"category": "tv",
"min_seeders": try_int(self.minseed),
"min_leechers": try_int(self.minleech),
"limit": 100,
"format": "json_extended",
"ranked": try_int(self.ranked),
"token": self.token,
}
if ep_obj is not None:
ep_indexerid = ep_obj.show.indexerid
ep_indexer = ep_obj.show.indexer
else:
ep_indexerid = None
ep_indexer = None
for mode in search_strings:
items = []
logger.log("Search Mode: {}".format(mode), logger.DEBUG)
if mode == "RSS":
search_params["sort"] = "last"
search_params["mode"] = "list"
search_params.pop("search_string", None)
search_params.pop("search_tvdb", None)
else:
search_params["sort"] = self.sorting if self.sorting else "seeders"
search_params["mode"] = "search"
if ep_indexer == INDEXER_TVDB and ep_indexerid:
search_params["search_tvdb"] = ep_indexerid
else:
search_params.pop("search_tvdb", None)
for search_string in search_strings[mode]:
if mode != "RSS":
search_params["search_string"] = search_string
logger.log("Search string: {}".format(search_string.decode("utf-8")),
logger.DEBUG)
# Check if token is still valid before search
if not self.login():
continue
# Maximum requests allowed are 1req/2sec
time.sleep(max(2, cpu_presets[CPU_PRESET]))
data = self.get_url(self.urls["api"], params=search_params, returns="json")
if not isinstance(data, dict):
logger.log("No data returned from provider", logger.DEBUG)
continue
error = data.get("error")
error_code = data.get("error_code")
# Don't log when {"error":"No results found","error_code":20}
# List of errors: https://github.com/rarbg/torrentapi/issues/1#issuecomment-114763312
if error:
if try_int(error_code) != 20:
logger.log(error)
continue
torrent_results = data.get("torrent_results")
if not torrent_results:
logger.log("Data returned from provider does not contain any torrents", logger.DEBUG)
continue
for item in torrent_results:
try:
title = item.pop("title")
download_url = item.pop("download")
if not all([title, download_url]):
continue
seeders = item.pop("seeders")
leechers = item.pop("leechers")
if seeders < self.minseed or leechers < self.minleech:
if mode != "RSS":
logger.log("Discarding torrent because it doesn't meet the"
" minimum seeders or leechers: {} (S:{} L:{})".format
(title, seeders, leechers), logger.DEBUG)
continue
torrent_size = item.pop("size", -1)
size = convert_size(torrent_size) or -1
item = title, download_url, size, seeders, leechers
if mode != "RSS":
logger.log("Found result: {} with {} seeders and {} leechers".format
(title, seeders, leechers), logger.DEBUG)
items.append(item)
except StandardError:
continue
# For each search mode sort all the items by seeders
items.sort(key=lambda tup: tup[3], reverse=True)
results += items
return results
def seed_ratio(self):
return self.ratio
provider = RarbgProvider()