-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathjackett.py
280 lines (238 loc) · 10.8 KB
/
jackett.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
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
# VERSION: 4.1
# AUTHORS: Diego de las Heras ([email protected])
# CONTRIBUTORS: ukharley
# hannsen (github.com/hannsen)
# Alexander Georgievskiy <[email protected]>
# Ryan Meyers (github.com/sreyemnayr)
import json
import os
import xml.etree.ElementTree
from urllib.parse import urlencode, unquote
from urllib import request as urllib_request
from http.cookiejar import CookieJar
from multiprocessing.dummy import Pool
from threading import Lock
from novaprinter import prettyPrinter
from helpers import download_file
###############################################################################
# load configuration from file
CONFIG_FILE = 'jackett.json'
CONFIG_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), CONFIG_FILE)
CONFIG_DATA_DEFAULT = {
'api_key': 'YOUR_API_KEY_HERE', # jackett api
'url': 'http://127.0.0.1:9117', # jackett url
'tracker': True, # (False/True) enable tracker name
'tracker_first': False, # (False/True) add tracker name to beginning of search result
'thread_count': 20, # number of threads to use for http requests
'freeleech': False, # (False/True) enable freeleech flag
'freeleech_flag': '🆓', # string to display for freeleech torrents
'freeleech_first': True, # (False/True) add freeleech flag to beginning of search result
}
CONFIG_DATA = CONFIG_DATA_DEFAULT.copy()
PRINTER_THREAD_LOCK = Lock()
def load_configuration():
global CONFIG_PATH, CONFIG_DATA
try:
# try to load user data from file
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
CONFIG_DATA = json.load(f)
except ValueError:
# if file exists, but it's malformed we load add a flag
CONFIG_DATA['malformed'] = True
except Exception:
# if file doesn't exist, we create it
save_configuration()
# do some checks
if any(item not in CONFIG_DATA for item in ['api_key', 'tracker_first', 'url']):
CONFIG_DATA['malformed'] = True
# add missing keys
updated_config = []
for key in CONFIG_DATA_DEFAULT.keys():
if key not in CONFIG_DATA:
CONFIG_DATA[key] = CONFIG_DATA_DEFAULT[key]
updated_config.append(key)
if updated_config:
print("Updated configuration file with default values for missing keys: " + ", ".join(updated_config))
save_configuration()
def save_configuration():
global CONFIG_PATH, CONFIG_DATA
with open(CONFIG_PATH, 'w', encoding='utf-8') as f:
f.write(json.dumps(CONFIG_DATA, indent=4, sort_keys=True, ensure_ascii=False))
load_configuration()
###############################################################################
class jackett(object):
name = 'Jackett'
url = CONFIG_DATA['url'] if CONFIG_DATA['url'][-1] != '/' else CONFIG_DATA['url'][:-1]
api_key = CONFIG_DATA['api_key']
thread_count = CONFIG_DATA['thread_count']
supported_categories = {
'all': None,
'anime': ['5070'],
'books': ['8000'],
'games': ['1000', '4000'],
'movies': ['2000'],
'music': ['3000'],
'software': ['4000'],
'tv': ['5000'],
}
def download_torrent(self, download_url):
# fix for some indexers with magnet link inside .torrent file
if download_url.startswith('magnet:?'):
print(download_url + " " + download_url)
response = self.get_response(download_url)
if response is not None and response.startswith('magnet:?'):
print(response + " " + download_url)
else:
print(download_file(download_url))
def search(self, what, cat='all'):
what = unquote(what)
category = self.supported_categories[cat.lower()]
# check for malformed configuration
if 'malformed' in CONFIG_DATA:
self.handle_error("malformed configuration file", what)
return
# check api_key
if self.api_key == "YOUR_API_KEY_HERE":
self.handle_error("api key error", what)
return
# search in Jackett API
if self.thread_count > 1:
args = []
indexers = self.get_jackett_indexers(what)
for indexer in indexers:
args.append((what, category, indexer))
with Pool(min(len(indexers), self.thread_count)) as pool:
pool.starmap(self.search_jackett_indexer, args)
else:
self.search_jackett_indexer(what, category, 'all')
def get_jackett_indexers(self, what):
params = [
('apikey', self.api_key),
('t', 'indexers'),
('configured', 'true')
]
params = urlencode(params)
jacket_url = self.url + "/api/v2.0/indexers/all/results/torznab/api?%s" % params
response = self.get_response(jacket_url)
if response is None:
self.handle_error("connection error getting indexer list", what)
return
# process results
response_xml = xml.etree.ElementTree.fromstring(response)
indexers = []
for indexer in response_xml.findall('indexer'):
indexers.append(indexer.attrib['id'])
return indexers
def search_jackett_indexer(self, what, category, indexer_id):
# prepare jackett url
params = [
('apikey', self.api_key),
('q', what)
]
if category is not None:
params.append(('cat', ','.join(category)))
params = urlencode(params)
jacket_url = self.url + "/api/v2.0/indexers/" + indexer_id + "/results/torznab/api?%s" % params # noqa
response = self.get_response(jacket_url)
if response is None:
self.handle_error("connection error for indexer: " + indexer_id, what)
return
# process search results
response_xml = xml.etree.ElementTree.fromstring(response)
for result in response_xml.find('channel').findall('item'):
res = {}
title = result.find('title')
if title is not None:
title = title.text
else:
continue
tracker = result.find('jackettindexer')
tracker = '' if tracker is None else tracker.text
if CONFIG_DATA['tracker']:
if CONFIG_DATA['tracker_first']:
res['name'] = '[%s] %s' % (tracker, title)
else:
res['name'] = '%s [%s]' % (title, tracker)
else:
res['name'] = title
if CONFIG_DATA['freeleech']:
downloadVolumeFactor = 1.0
downloadVolumeFactorElement = result.find(self.generate_xpath('downloadvolumefactor'))
if downloadVolumeFactorElement is not None:
downloadVolumeFactor = float(downloadVolumeFactorElement.attrib['value'])
else:
downloadVolumeFactorElement = result.find('downloadvolumefactor')
if downloadVolumeFactorElement is not None:
downloadVolumeFactor = float(downloadVolumeFactorElement.text)
if downloadVolumeFactor <= 0:
if CONFIG_DATA['freeleech_first']:
res['name'] = '%s %s' % (CONFIG_DATA['freeleech_flag'], res['name'])
else:
res['name'] = '%s %s' % (res['name'], CONFIG_DATA['freeleech_flag'])
res['link'] = result.find(self.generate_xpath('magneturl'))
if res['link'] is not None:
res['link'] = res['link'].attrib['value']
else:
res['link'] = result.find('link')
if res['link'] is not None:
res['link'] = res['link'].text
else:
continue
res['size'] = result.find('size')
res['size'] = -1 if res['size'] is None else (res['size'].text + ' B')
res['seeds'] = result.find(self.generate_xpath('seeders'))
res['seeds'] = -1 if res['seeds'] is None else int(res['seeds'].attrib['value'])
res['leech'] = result.find(self.generate_xpath('peers'))
res['leech'] = -1 if res['leech'] is None else int(res['leech'].attrib['value'])
if res['seeds'] != -1 and res['leech'] != -1:
res['leech'] -= res['seeds']
res['desc_link'] = result.find('comments')
if res['desc_link'] is not None:
res['desc_link'] = res['desc_link'].text
else:
res['desc_link'] = result.find('guid')
res['desc_link'] = '' if res['desc_link'] is None else res['desc_link'].text
# note: engine_url can't be changed, torrent download stops working
res['engine_url'] = self.url
self.pretty_printer_thread_safe(res)
def generate_xpath(self, tag):
return './{http://torznab.com/schemas/2015/feed}attr[@name="%s"]' % tag
def get_response(self, query):
response = None
try:
# we can't use helpers.retrieve_url because of redirects
# we need the cookie processor to handle redirects
opener = urllib_request.build_opener(urllib_request.HTTPCookieProcessor(CookieJar()))
response = opener.open(query).read().decode('utf-8')
except urllib_request.HTTPError as e:
# if the page returns a magnet redirect, used in download_torrent
if e.code == 302:
response = e.url
except Exception:
pass
return response
def handle_error(self, error_msg, what):
# we need to print the search text to be displayed in qBittorrent when
# 'Torrent names only' is enabled
self.pretty_printer_thread_safe({
'seeds': -1,
'size': -1,
'leech': -1,
'engine_url': self.url,
'link': self.url,
'desc_link': 'https://github.com/qbittorrent/search-plugins/wiki/How-to-configure-Jackett-plugin', # noqa
'name': "Jackett: %s! Right-click this row and select 'Open description page' to open help. Configuration file: '%s' Search: '%s'" % (error_msg, CONFIG_PATH, what) # noqa
})
def pretty_printer_thread_safe(self, dictionary):
global PRINTER_THREAD_LOCK
with PRINTER_THREAD_LOCK:
prettyPrinter(self.escape_pipe(dictionary))
def escape_pipe(self, dictionary):
# Safety measure until it's fixed in prettyPrinter
for key in dictionary.keys():
if isinstance(dictionary[key], str):
dictionary[key] = dictionary[key].replace('|', '%7C')
return dictionary
if __name__ == "__main__":
jackett_se = jackett()
jackett_se.search("ubuntu server", 'software')