.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
piratebay.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3 Piratebay (Videos, Music, Files)
4"""
5
6from json import loads
7from datetime import datetime
8from operator import itemgetter
9
10from urllib.parse import quote
11from searx.utils import humanize_bytes
12
13# about
14about = {
15 "website": 'https://thepiratebay.org',
16 "wikidata_id": 'Q22663',
17 "official_api_documentation": 'https://apibay.org/',
18 "use_official_api": True,
19 "require_api_key": False,
20 "results": 'JSON',
21}
22
23# engine dependent config
24categories = ["files"]
25
26# search-url
27url = "https://thepiratebay.org/"
28search_url = "https://apibay.org/q.php?q={search_term}&cat={search_type}"
29
30# default trackers provided by thepiratebay
31trackers = [
32 "udp://tracker.coppersurfer.tk:6969/announce",
33 "udp://9.rarbg.to:2920/announce",
34 "udp://tracker.opentrackr.org:1337",
35 "udp://tracker.internetwarriors.net:1337/announce",
36 "udp://tracker.leechers-paradise.org:6969/announce",
37 "udp://tracker.coppersurfer.tk:6969/announce",
38 "udp://tracker.pirateparty.gr:6969/announce",
39 "udp://tracker.cyberia.is:6969/announce",
40]
41
42# piratebay specific type-definitions
43search_types = {"files": "0", "music": "100", "videos": "200"}
44
45
46# do search-request
47def request(query, params):
48 search_type = search_types.get(params["category"], "0")
49
50 params["url"] = search_url.format(search_term=quote(query), search_type=search_type)
51
52 return params
53
54
55# get response from search-request
56def response(resp):
57 results = []
58
59 search_res = loads(resp.text)
60
61 # return empty array if nothing is found
62 if search_res[0]["name"] == "No results returned":
63 return []
64
65 # parse results
66 for result in search_res:
67 link = url + "description.php?id=" + result["id"]
68 magnetlink = (
69 "magnet:?xt=urn:btih:" + result["info_hash"] + "&dn=" + result["name"] + "&tr=" + "&tr=".join(trackers)
70 )
71
72 params = {
73 "url": link,
74 "title": result["name"],
75 "seed": result["seeders"],
76 "leech": result["leechers"],
77 "magnetlink": magnetlink,
78 "template": "torrent.html",
79 }
80
81 # extract and convert creation date
82 try:
83 params['publishedDate'] = datetime.fromtimestamp(float(result["added"]))
84 except: # pylint: disable=bare-except
85 pass
86
87 # let's try to calculate the torrent size
88 params['filesize'] = humanize_bytes(int(result["size"]))
89
90 # append result
91 results.append(params)
92
93 # return results sorted by seeder
94 return sorted(results, key=itemgetter("seed"), reverse=True)
request(query, params)
Definition piratebay.py:47