@@ -1,39 +1,61 @@
1+## Piratebay (Videos, Music, Files)
3+# @website https://thepiratebay.se
4+# @provide-api no (nothing found)
7+# @results HTML (using search portal)
8+# @stable yes (HTML can change)
9+# @parse url, title, content, seed, leech, magnetlink
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
17+# engine dependent config
7−categories = ['videos', 'music']
18+categories = ['videos', 'music', 'files']
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}'
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'}
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()'
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')
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)
26 return params
43 return params
46+# get response from search-request
29def response(resp):
47def response(resp):
30 results = []
48 results = []
31 dom = html.fromstring(resp.text)
50 dom = html.fromstring(resp.text)
32 search_res = dom.xpath('//table[@id="searchResult"]//tr')
52 search_res = dom.xpath('//table[@id="searchResult"]//tr')
54+ # return empty array if nothing is found
34 if not search_res:
55 if not search_res:
35− return results
56+ return []
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]
66+ # convert seed to int if possible
44 if seed.isdigit():
67 if seed.isdigit():
45 seed = int(seed)
68 seed = int(seed)
72+ # convert leech to int if possible
49 if leech.isdigit():
73 if leech.isdigit():
50 leech = int(leech)
74 leech = int(leech)
54 magnetlink = result.xpath(magnet_xpath)[0]
78 magnetlink = result.xpath(magnet_xpath)[0]
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'})
89+ # return results sorted by seeder
63 return sorted(results, key=itemgetter('seed'), reverse=True)
90 return sorted(results, key=itemgetter('seed'), reverse=True)