on master help sign in
[fix] piratebay engine
commit52d78d8418677d2ce37afbca7c27a767b04852aa authorEmilien Devos <contact@emiliendevos.be> dateMon 10 Aug 2020, 20:26 (6 years ago) parent77103c7
2 files changed+64−64
searx/engines/piratebay.py+62−63 searx/settings.yml+2−1
searx/engines/piratebay.py+62−63
@@ -1,44 +1,50 @@
11# Piratebay (Videos, Music, Files)
22#
3−# @website https://thepiratebay.se
4−# @provide-api no (nothing found)
3+# @website https://thepiratebay.org
4+# @provide-api yes (https://apibay.org/)
55#
6−# @using-api no
7−# @results HTML (using search portal)
8−# @stable yes (HTML can change)
9−# @parse url, title, content, seed, leech, magnetlink
6+# @using-api yes
7+# @results JSON
8+# @stable no (the API is not documented nor versioned)
9+# @parse url, title, seed, leech, magnetlink, filesize, publishedDate
1010
11−from lxml import html
11+from json import loads
12+from datetime import datetime
1213from 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
1516
1617# engine dependent config
17−categories = ['videos', 'music', 'files']
18−paging = True
18+categories = ["videos", "music", "files"]
1919
2020# search-url
21−url = 'https://thepiratebay.org/'
22−search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
21+url = "https://thepiratebay.org/"
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+]
2335
2436# piratebay specific type-definitions
25−search_types = {'files': '0',
26− 'music': '100',
27− '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"]'
37+search_types = {"files": "0",
38+ "music": "100",
39+ "videos": "200"}
3340
3441
3542# do search-request
3643def request(query, params):
37− search_type = search_types.get(params['category'], '0')
44+ search_type = search_types.get(params["category"], "0")
3845
39− params['url'] = search_url.format(search_term=quote(query),
40− search_type=search_type,
41− pageno=params['pageno'] - 1)
46+ params["url"] = search_url.format(search_term=quote(query),
47+ search_type=search_type)
4248
4349 return params
4450
@@ -47,50 +53,43 @@ def request(query, params):
4753def response(resp):
4854 results = []
4955
50− dom = html.fromstring(resp.text)
51−
52− search_res = dom.xpath('//table[@id="searchResult"]//tr')
56+ search_res = loads(resp.text)
5357
5458 # return empty array if nothing is found
55− if not search_res:
59+ if search_res[0]["name"] == "No results returned":
5660 return []
5761
5862 # parse results
59− for result in search_res[1:]:
60− link = result.xpath('.//div[@class="detName"]//a')[0]
61− href = urljoin(url, link.attrib.get('href'))
62− title = extract_text(link)
63− content = extract_text(result.xpath(content_xpath))
64− seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
65−
66− # convert seed to int if possible
67− if seed.isdigit():
68− seed = int(seed)
69− else:
70− seed = 0
71−
72− # convert leech to int if possible
73− if leech.isdigit():
74− leech = int(leech)
75− else:
76− leech = 0
77−
78− magnetlink = result.xpath(magnet_xpath)[0]
79− torrentfile_links = result.xpath(torrent_xpath)
80− if torrentfile_links:
81− torrentfile_link = torrentfile_links[0].attrib.get('href')
82− else:
83− torrentfile_link = None
63+ for result in search_res:
64+ link = url + "description.php?id=" + result["id"]
65+ magnetlink = "magnet:?xt=urn:btih:" + result["info_hash"] + "&dn=" + result["name"]
66+ + "&tr=" + "&tr=".join(trackers)
67+
68+ params = {
69+ "url": link,
70+ "title": result["name"],
71+ "seed": result["seeders"],
72+ "leech": result["leechers"],
73+ "magnetlink": magnetlink,
74+ "template": "torrent.html"
75+ }
76+
77+ # extract and convert creation date
78+ try:
79+ date = datetime.fromtimestamp(result.added)
80+ params['publishedDate'] = date
81+ except:
82+ pass
83+
84+ # let's try to calculate the torrent size
85+ try:
86+ filesize = get_torrent_size(result["size"], "B")
87+ params['filesize'] = filesize
88+ except:
89+ pass
8490
8591 # append result
86− results.append({'url': href,
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'})
92+ results.append(params)
9493
9594 # 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:
544544 - name : piratebay
545545 engine : piratebay
546546 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/
548549 timeout : 3.0
549550
550551 - name : pubmed