-
Notifications
You must be signed in to change notification settings - Fork 269
/
rarbg.py
222 lines (176 loc) · 7.3 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
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
# coding=utf-8
"""Provider code for RARBG."""
from __future__ import unicode_literals
import datetime
import logging
import time
from medusa import (
app,
tv,
)
from medusa.helper.common import convert_size, try_int
from medusa.logger.adapters.style import BraceAdapter
from medusa.providers.torrent.torrent_provider import TorrentProvider
log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())
class RarbgProvider(TorrentProvider):
"""RARBG Torrent provider."""
def __init__(self):
"""Initialize the class."""
super(RarbgProvider, self).__init__('Rarbg')
# Credentials
self.public = True
self.token = None
self.token_expires = None
# URLs
self.url = 'https://rarbg.com' # Spec: https://torrentapi.org/apidocs_v2.txt
self.urls = {
'api': 'http://torrentapi.org/pubapi_v2.php',
}
# Proper Strings
self.proper_strings = ['{{PROPER|REPACK|REAL|RERIP}}']
# Miscellaneous Options
self.ranked = None
self.sorting = None
# Cache
self.cache = tv.Cache(self, min_time=15)
def search(self, search_strings, age=0, ep_obj=None, **kwargs):
"""
Search a provider and parse the results.
:param search_strings: A dict with mode (key) and the search value (value)
:param age: Not used
:param ep_obj: Not used
:returns: A list of search results (structure)
"""
results = []
if not self.login():
return results
# Search Params
search_params = {
'app_id': app.RARBG_APPID,
'category': 'tv',
'min_seeders': self.minseed,
'min_leechers': self.minleech,
'limit': 100,
'format': 'json_extended',
'ranked': try_int(self.ranked),
'token': self.token,
'sort': 'last',
'mode': 'list',
}
for mode in search_strings:
log.debug('Search mode: {0}', mode)
if mode == 'RSS':
search_params['search_string'] = None
search_params['search_tvdb'] = None
else:
search_params['sort'] = self.sorting if self.sorting else 'seeders'
search_params['mode'] = 'search'
search_params['search_tvdb'] = self._get_tvdb_id()
for search_string in search_strings[mode]:
if mode != 'RSS':
log.debug('Search string: {search}',
{'search': search_string})
if self.ranked:
log.debug('Searching only ranked torrents')
search_params['search_string'] = search_string
# Check if token is still valid before search
if not self.login():
continue
# Maximum requests allowed are 1req/2sec
# Changing to 5 because of server clock desync
time.sleep(5)
search_url = self.urls['api']
response = self.session.get(search_url, params=search_params)
if not response or not response.content:
log.debug('No data returned from provider')
continue
try:
jdata = response.json()
except ValueError:
log.debug('No data returned from provider')
continue
error = jdata.get('error')
error_code = jdata.get('error_code')
if error:
# List of errors: https://github.com/rarbg/torrentapi/issues/1#issuecomment-114763312
if error_code == 5:
# 5 = Too many requests per second
log_level = logging.INFO
elif error_code not in (4, 8, 10, 12, 14, 20):
# 4 = Invalid token. Use get_token for a new one!
# 8, 10, 12, 14 = Cant find * in database. Are you sure this * exists?
# 20 = No results found
log_level = logging.WARNING
else:
log_level = logging.DEBUG
log.log(log_level, '{msg} Code: {code}', {'msg': error, 'code': error_code})
continue
results += self.parse(jdata, mode)
return results
def parse(self, data, mode):
"""
Parse search results for items.
:param data: The raw response from a search
:param mode: The current mode used to search, e.g. RSS
:return: A list of items found
"""
items = []
torrent_rows = data.get('torrent_results', {})
if not torrent_rows:
log.debug('Data returned from provider does not contain any torrents')
return items
for row in torrent_rows:
try:
title = row.pop('title')
download_url = row.pop('download') + self._custom_trackers
if not all([title, download_url]):
continue
seeders = row.pop('seeders', 0)
leechers = row.pop('leechers', 0)
# Filter unseeded torrent
if seeders < self.minseed:
if mode != 'RSS':
log.debug("Discarding torrent because it doesn't meet the"
' minimum seeders: {0}. Seeders: {1}',
title, seeders)
continue
torrent_size = row.pop('size', None)
size = convert_size(torrent_size, default=-1)
pubdate_raw = row.pop('pubdate', None)
pubdate = self.parse_pubdate(pubdate_raw)
item = {
'title': title,
'link': download_url,
'size': size,
'seeders': seeders,
'leechers': leechers,
'pubdate': pubdate,
}
if mode != 'RSS':
log.debug('Found result: {0} with {1} seeders and {2} leechers',
title, seeders, leechers)
items.append(item)
except (AttributeError, TypeError, KeyError, ValueError, IndexError):
log.exception('Failed parsing provider.')
return items
def login(self):
"""Login method used for logging in before doing search and torrent downloads."""
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': app.RARBG_APPID,
}
response = self.session.get(self.urls['api'], params=login_params)
if not response:
log.warning('Unable to connect to provider')
return False
try:
self.token = response.json().get('token')
except ValueError:
self.token = None
self.token_expires = datetime.datetime.now() + datetime.timedelta(minutes=14) if self.token else None
return self.token is not None
provider = RarbgProvider()