@@ -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+
111from urlparse import urljoin
212from cgi import escape
313from urllib import quote
414from lxml import html
515from operator import itemgetter
616
7−categories = ['videos', 'music']
17+# engine dependent config
18+categories = ['videos', 'music', 'files']
19+paging = True
820
21+# search-url
922url = 'https://thepiratebay.se/'
1023search_url = url + 'search/{search_term}/{pageno}/99/{search_type}'
11−search_types = {'videos': '200',
24+
25+# piratebay specific type-definitions
26+search_types = {'files': '0',
1227 'music': '100',
13− 'files': '0'}
28+ 'videos': '200'}
1429
30+# specific xpath variables
1531magnet_xpath = './/a[@title="Download this torrent using magnet"]'
1632content_xpath = './/font[@class="detDesc"]//text()'
1733
18−paging = True
19−
2034
35+# do search-request
2136def request(query, params):
22− search_type = search_types.get(params['category'], '200')
37+ search_type = search_types.get(params['category'], '0')
38+
2339 params['url'] = search_url.format(search_term=quote(query),
2440 search_type=search_type,
2541 pageno=params['pageno'] - 1)
42+
2643 return params
2744
2845
46+# get response from search-request
2947def response(resp):
3048 results = []
49+
3150 dom = html.fromstring(resp.text)
51+
3252 search_res = dom.xpath('//table[@id="searchResult"]//tr')
3353
54+ # return empty array if nothing is found
3455 if not search_res:
35− return results
56+ return []
3657
58+ # parse results
3759 for result in search_res[1:]:
3860 link = result.xpath('.//div[@class="detName"]//a')[0]
3961 href = urljoin(url, link.attrib.get('href'))
@@ -41,17 +63,21 @@ def response(resp):
4163 content = escape(' '.join(result.xpath(content_xpath)))
4264 seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
4365
66+ # convert seed to int if possible
4467 if seed.isdigit():
4568 seed = int(seed)
4669 else:
4770 seed = 0
4871
72+ # convert leech to int if possible
4973 if leech.isdigit():
5074 leech = int(leech)
5175 else:
5276 leech = 0
5377
5478 magnetlink = result.xpath(magnet_xpath)[0]
79+
80+ # append result
5581 results.append({'url': href,
5682 'title': title,
5783 'content': content,
@@ -60,4 +86,5 @@ def response(resp):
6086 'magnetlink': magnetlink.attrib['href'],
6187 'template': 'torrent.html'})
6288
89+ # return results sorted by seeder
6390 return sorted(results, key=itemgetter('seed'), reverse=True)