on master help sign in
[enh] add yggtorrent engine (#2135)
commit27d74826f1e00302336c8720e014cd4cda871981 authorÉmilien Devos <contact@emiliendevos.be> dateTue 18 Aug 2020, 19:02 (6 years ago) parent093dd42
2 files changed+131
searx/engines/yggtorrent.py+124 searx/settings.yml+7
searx/engines/yggtorrent.pynew file+124
@@ -0,0 +1,124 @@
1+# Yggtorrent (Videos, Music, Files)
2+#
3+# @website https://www2.yggtorrent.si
4+# @provide-api no (nothing found)
5+#
6+# @using-api no
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, seed, leech, publishedDate, filesize
10+
11+from lxml import html
12+from operator import itemgetter
13+from datetime import datetime
14+from searx.engines.xpath import extract_text
15+from searx.url_utils import quote
16+from searx.utils import get_torrent_size
17+from searx.poolrequests import get as http_get
18+
19+# engine dependent config
20+categories = ['videos', 'music', 'files']
21+paging = True
22+
23+# search-url
24+url = 'https://www2.yggtorrent.si/'
25+search_url = url + 'engine/search?name={search_term}&do=search&page={pageno}&category={search_type}'
26+
27+# yggtorrent specific type-definitions
28+search_types = {'files': 'all',
29+ 'music': '2139',
30+ 'videos': '2145'}
31+
32+cookies = dict()
33+
34+
35+def init(engine_settings=None):
36+ global cookies
37+ # initial cookies
38+ resp = http_get(url, allow_redirects=False)
39+ if resp.ok:
40+ for r in resp.history:
41+ cookies.update(r.cookies)
42+ cookies.update(resp.cookies)
43+
44+
45+# do search-request
46+def request(query, params):
47+ search_type = search_types.get(params['category'], 'all')
48+ pageno = (params['pageno'] - 1) * 50
49+
50+ params['url'] = search_url.format(search_term=quote(query),
51+ search_type=search_type,
52+ pageno=pageno)
53+
54+ params['cookies'] = cookies
55+
56+ return params
57+
58+
59+# get response from search-request
60+def response(resp):
61+ results = []
62+ dom = html.fromstring(resp.text)
63+
64+ search_res = dom.xpath('//section[@id="#torrents"]/div/table/tbody/tr')
65+
66+ # return empty array if nothing is found
67+ if not search_res:
68+ return []
69+
70+ # parse results
71+ for result in search_res:
72+ link = result.xpath('.//a[@id="torrent_name"]')[0]
73+ href = link.attrib.get('href')
74+ title = extract_text(link)
75+ seed = result.xpath('.//td[8]/text()')[0]
76+ leech = result.xpath('.//td[9]/text()')[0]
77+
78+ # convert seed to int if possible
79+ if seed.isdigit():
80+ seed = int(seed)
81+ else:
82+ seed = 0
83+
84+ # convert leech to int if possible
85+ if leech.isdigit():
86+ leech = int(leech)
87+ else:
88+ leech = 0
89+
90+ params = {'url': href,
91+ 'title': title,
92+ 'seed': seed,
93+ 'leech': leech,
94+ 'template': 'torrent.html'}
95+
96+ # let's try to calculate the torrent size
97+ try:
98+ filesize_info = result.xpath('.//td[6]/text()')[0]
99+ filesize = filesize_info[:-2]
100+ filesize_multiplier = filesize_info[-2:].lower()
101+ multiplier_french_to_english = {
102+ 'to': 'TiB',
103+ 'go': 'GiB',
104+ 'mo': 'MiB',
105+ 'ko': 'KiB'
106+ }
107+ filesize = get_torrent_size(filesize, multiplier_french_to_english[filesize_multiplier])
108+ params['filesize'] = filesize
109+ except:
110+ pass
111+
112+ # extract and convert creation date
113+ try:
114+ date_ts = result.xpath('.//td[5]/div/text()')[0]
115+ date = datetime.fromtimestamp(float(date_ts))
116+ params['publishedDate'] = date
117+ except:
118+ pass
119+
120+ # append result
121+ results.append(params)
122+
123+ # return results sorted by seeder
124+ return sorted(results, key=itemgetter('seed'), reverse=True)
searx/settings.yml+7
@@ -702,6 +702,13 @@ engines:
702702 # Or you can use the html non-stable engine, activated by default
703703 engine : youtube_noapi
704704
705+ - name : yggtorrent
706+ engine : yggtorrent
707+ shortcut : ygg
708+ url: https://www2.yggtorrent.si/
709+ disabled : True
710+ timeout : 4.0
711+
705712 - name : dailymotion
706713 engine : dailymotion
707714 shortcut : dm