on master help sign in
[fix] piratebay engine
52d78d8Emilien Devos
searx/engines/piratebay.py+62−63
@@ -1,44 +1,50 @@
1# Piratebay (Videos, Music, Files)
1# Piratebay (Videos, Music, Files)
2#
2#
3−# @website https://thepiratebay.se
3+# @website https://thepiratebay.org
4−# @provide-api no (nothing found)
4+# @provide-api yes (https://apibay.org/)
5#
5#
6−# @using-api no
6+# @using-api yes
7−# @results HTML (using search portal)
7+# @results JSON
8−# @stable yes (HTML can change)
8+# @stable no (the API is not documented nor versioned)
9−# @parse url, title, content, seed, leech, magnetlink
9+# @parse url, title, seed, leech, magnetlink, filesize, publishedDate
10
10
11−from lxml import html
11+from json import loads
12+from datetime import datetime
12from operator import itemgetter
13from operator import itemgetter
13−from searx.engines.xpath import extract_text
14−from searx.url_utils import quote, urljoin
14+from searx.url_utils import quote
15+from searx.utils import get_torrent_size
15
16
16# engine dependent config
17# engine dependent config
17−categories = ['videos', 'music', 'files']
18+categories = ["videos", "music", "files"]
18−paging = True
19
19
20# search-url
20# search-url
21−url = 'https://thepiratebay.org/'
21+url = "https://thepiratebay.org/"
22−search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
22+search_url = "https://apibay.org/q.php?q={search_term}&cat={search_type}"
23+
24+# default trackers provided by thepiratebay
25+trackers = [
26+ "udp://tracker.coppersurfer.tk:6969/announce",
27+ "udp://9.rarbg.to:2920/announce",
28+ "udp://tracker.opentrackr.org:1337",
29+ "udp://tracker.internetwarriors.net:1337/announce",
30+ "udp://tracker.leechers-paradise.org:6969/announce",
31+ "udp://tracker.coppersurfer.tk:6969/announce",
32+ "udp://tracker.pirateparty.gr:6969/announce",
33+ "udp://tracker.cyberia.is:6969/announce",
34+]
23
35
24# piratebay specific type-definitions
36# piratebay specific type-definitions
25−search_types = {'files': '0',
37+search_types = {"files": "0",
26− 'music': '100',
38+ "music": "100",
27− 'videos': '200'}
39+ "videos": "200"}
28−
29−# specific xpath variables
30−magnet_xpath = './/a[@title="Download this torrent using magnet"]'
31−torrent_xpath = './/a[@title="Download this torrent"]'
32−content_xpath = './/font[@class="detDesc"]'
33
40
34
41
35# do search-request
42# do search-request
36def request(query, params):
43def request(query, params):
37− search_type = search_types.get(params['category'], '0')
44+ search_type = search_types.get(params["category"], "0")
38
45
39− params['url'] = search_url.format(search_term=quote(query),
46+ params["url"] = search_url.format(search_term=quote(query),
40− search_type=search_type,
47+ search_type=search_type)
41− pageno=params['pageno'] - 1)
42
48
43 return params
49 return params
44
50
@@ -47,50 +53,43 @@ def request(query, params):
47def response(resp):
53def response(resp):
48 results = []
54 results = []
49
55
50− dom = html.fromstring(resp.text)
56+ search_res = loads(resp.text)
51−
52− search_res = dom.xpath('//table[@id="searchResult"]//tr')
53
57
54 # return empty array if nothing is found
58 # return empty array if nothing is found
55− if not search_res:
59+ if search_res[0]["name"] == "No results returned":
56 return []
60 return []
57
61
58 # parse results
62 # parse results
59− for result in search_res[1:]:
63+ for result in search_res:
60− link = result.xpath('.//div[@class="detName"]//a')[0]
64+ link = url + "description.php?id=" + result["id"]
61− href = urljoin(url, link.attrib.get('href'))
65+ magnetlink = "magnet:?xt=urn:btih:" + result["info_hash"] + "&dn=" + result["name"]
62− title = extract_text(link)
66+ + "&tr=" + "&tr=".join(trackers)
63− content = extract_text(result.xpath(content_xpath))
67+
64− seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
68+ params = {
65−
69+ "url": link,
66− # convert seed to int if possible
70+ "title": result["name"],
67− if seed.isdigit():
71+ "seed": result["seeders"],
68− seed = int(seed)
72+ "leech": result["leechers"],
69− else:
73+ "magnetlink": magnetlink,
70− seed = 0
74+ "template": "torrent.html"
71−
75+ }
72− # convert leech to int if possible
76+
73− if leech.isdigit():
77+ # extract and convert creation date
74− leech = int(leech)
78+ try:
75− else:
79+ date = datetime.fromtimestamp(result.added)
76− leech = 0
80+ params['publishedDate'] = date
77−
81+ except:
78− magnetlink = result.xpath(magnet_xpath)[0]
82+ pass
79− torrentfile_links = result.xpath(torrent_xpath)
83+
80− if torrentfile_links:
84+ # let's try to calculate the torrent size
81− torrentfile_link = torrentfile_links[0].attrib.get('href')
85+ try:
82− else:
86+ filesize = get_torrent_size(result["size"], "B")
83− torrentfile_link = None
87+ params['filesize'] = filesize
88+ except:
89+ pass
84
90
85 # append result
91 # append result
86− results.append({'url': href,
92+ results.append(params)
87− 'title': title,
88− 'content': content,
89− 'seed': seed,
90− 'leech': leech,
91− 'magnetlink': magnetlink.attrib.get('href'),
92− 'torrentfile': torrentfile_link,
93− 'template': 'torrent.html'})
94
93
95 # return results sorted by seeder
94 # return results sorted by seeder
96− return sorted(results, key=itemgetter('seed'), reverse=True)
95+ return sorted(results, key=itemgetter("seed"), reverse=True)
searx/settings.yml+2−1
@@ -544,7 +544,8 @@ engines:
544 - name : piratebay
544 - name : piratebay
545 engine : piratebay
545 engine : piratebay
546 shortcut : tpb
546 shortcut : tpb
547− url: https://pirateproxy.red/
547+ # You may need to change this URL to a proxy if piratebay is blocked in your country
548+ url: https://thepiratebay.org/
548 timeout : 3.0
549 timeout : 3.0
549
550
550 - name : pubmed
551 - name : pubmed