@@ -0,0 +1,80 @@
1+# SPDX-License-Identifier: AGPL-3.0-or-later
2+"""
3+
4+ Prowlarr search (Files)
5+
6+"""
7+
8+from json import loads
9+from urllib.parse import urlencode
10+from searx.exceptions import SearxEngineAPIException
11+
12+categories = ''
13+
14+paging = False
15+api_key = ''
16+indexer_ids = ''
17+search_type = 'search'
18+search_categories = ''
19+base_url = ''
20+
21+
22+def request(query, params):
23+ if not base_url:
24+ raise SearxEngineAPIException('missing prowlarr base url')
25+
26+ if not api_key:
27+ raise SearxEngineAPIException('missing prowlarr API key')
28+
29+ query_args = {
30+ 'query': query,
31+ 'apikey': api_key,
32+ 'type': search_type
33+ }
34+
35+ if indexer_ids:
36+ query_args['indexerIds'] = indexer_ids
37+
38+ if search_categories:
39+ query_args['categories'] = search_categories
40+
41+ params['url'] = base_url + urlencode(query_args)
42+
43+ return params
44+
45+
46+def response(resp):
47+ results = []
48+
49+ json_data = loads(resp.text)
50+
51+ for result in json_data:
52+
53+ new_result = {
54+ 'title': result['title'],
55+ 'url': result['infoUrl'],
56+ 'template': 'torrent.html'
57+ }
58+
59+ if 'files' in result:
60+ new_result['files'] = result['files']
61+
62+ if 'size' in result:
63+ new_result['filesize'] = result['size']
64+
65+ if 'seeders' in result:
66+ new_result['seed'] = result['seeders']
67+
68+ if 'leechers' in result:
69+ new_result['leech'] = result['leechers']
70+
71+ if 'downloadUrl' in result:
72+ new_result['torrentfile'] = result['downloadUrl']
73+
74+ # magnet link *may* be in guid, but it may be also idential to infoUrl
75+ if 'guid' in result and isinstance(result['guid'], str) and result['guid'].startswith('magnet'):
76+ new_result['magnetlink'] = result['guid']
77+
78+ results.append(new_result)
79+
80+ return results