on master help sign in
update piratebay engine and add comments
dae88d8Thomas Pointhuber
searx/engines/piratebay.py+34−7
@@ -1,39 +1,61 @@
1+## Piratebay (Videos, Music, Files)
2+#
3+# @website https://thepiratebay.se
4+# @provide-api no (nothing found)
5+#
6+# @using-api no
7+# @results HTML (using search portal)
8+# @stable yes (HTML can change)
9+# @parse url, title, content, seed, leech, magnetlink
10+
1from urlparse import urljoin
11from urlparse import urljoin
2from cgi import escape
12from cgi import escape
3from urllib import quote
13from urllib import quote
4from lxml import html
14from lxml import html
5from operator import itemgetter
15from operator import itemgetter
6
16
17+# engine dependent config
7−categories = ['videos', 'music']
18+categories = ['videos', 'music', 'files']
19+paging = True
8
20
21+# search-url
9url = 'https://thepiratebay.se/'
22url = 'https://thepiratebay.se/'
10search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
23search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
24+
25+# piratebay specific type-definitions
11−search_types = {'videos': '200',
26+search_types = {'files': '0',
12 'music': '100',
27 'music': '100',
13− 'files': '0'}
28+ 'videos': '200'}
14
29
30+# specific xpath variables
15magnet_xpath = './/a[@title="Download this torrent using magnet"]'
31magnet_xpath = './/a[@title="Download this torrent using magnet"]'
16content_xpath = './/font[@class="detDesc"]//text()'
32content_xpath = './/font[@class="detDesc"]//text()'
17
33
18−paging = True
19−
20
34
35+# do search-request
21def request(query, params):
36def request(query, params):
22− search_type = search_types.get(params['category'], '200')
37+ search_type = search_types.get(params['category'], '0')
38+
23 params['url'] = search_url.format(search_term=quote(query),
39 params['url'] = search_url.format(search_term=quote(query),
24 search_type=search_type,
40 search_type=search_type,
25 pageno=params['pageno'] - 1)
41 pageno=params['pageno'] - 1)
42+
26 return params
43 return params
27
44
28
45
46+# get response from search-request
29def response(resp):
47def response(resp):
30 results = []
48 results = []
49+
31 dom = html.fromstring(resp.text)
50 dom = html.fromstring(resp.text)
51+
32 search_res = dom.xpath('//table[@id="searchResult"]//tr')
52 search_res = dom.xpath('//table[@id="searchResult"]//tr')
33
53
54+ # return empty array if nothing is found
34 if not search_res:
55 if not search_res:
35− return results
56+ return []
36
57
58+ # parse results
37 for result in search_res[1:]:
59 for result in search_res[1:]:
38 link = result.xpath('.//div[@class="detName"]//a')[0]
60 link = result.xpath('.//div[@class="detName"]//a')[0]
39 href = urljoin(url, link.attrib.get('href'))
61 href = urljoin(url, link.attrib.get('href'))
@@ -41,17 +63,21 @@ def response(resp):
41 content = escape(' '.join(result.xpath(content_xpath)))
63 content = escape(' '.join(result.xpath(content_xpath)))
42 seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
64 seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
43
65
66+ # convert seed to int if possible
44 if seed.isdigit():
67 if seed.isdigit():
45 seed = int(seed)
68 seed = int(seed)
46 else:
69 else:
47 seed = 0
70 seed = 0
48
71
72+ # convert leech to int if possible
49 if leech.isdigit():
73 if leech.isdigit():
50 leech = int(leech)
74 leech = int(leech)
51 else:
75 else:
52 leech = 0
76 leech = 0
53
77
54 magnetlink = result.xpath(magnet_xpath)[0]
78 magnetlink = result.xpath(magnet_xpath)[0]
79+
80+ # append result
55 results.append({'url': href,
81 results.append({'url': href,
56 'title': title,
82 'title': title,
57 'content': content,
83 'content': content,
@@ -60,4 +86,5 @@ def response(resp):
60 'magnetlink': magnetlink.attrib['href'],
86 'magnetlink': magnetlink.attrib['href'],
61 'template': 'torrent.html'})
87 'template': 'torrent.html'})
62
88
89+ # return results sorted by seeder
63 return sorted(results, key=itemgetter('seed'), reverse=True)
90 return sorted(results, key=itemgetter('seed'), reverse=True)
searx/settings.yml−1
@@ -82,7 +82,6 @@ engines:
82
82
83 - name : piratebay
83 - name : piratebay
84 engine : piratebay
84 engine : piratebay
85− categories : videos, music, files
86 shortcut : tpb
85 shortcut : tpb
87
86
88 - name : soundcloud
87 - name : soundcloud