on master help sign in
[mod] tpb sort refactor
7e946a3Adam Tauber
searx/engines/piratebay.py+16−6
@@ -2,6 +2,7 @@ from urlparse import urljoin
2from cgi import escape
2from cgi import escape
3from urllib import quote
3from urllib import quote
4from lxml import html
4from lxml import html
5+from operator import itemgetter
5
6
6categories = ['videos', 'music']
7categories = ['videos', 'music']
7
8
@@ -24,18 +25,32 @@ def request(query, params):
24 pageno=params['pageno'] - 1)
25 pageno=params['pageno'] - 1)
25 return params
26 return params
26
27
28+
27def response(resp):
29def response(resp):
28 results = []
30 results = []
29 dom = html.fromstring(resp.text)
31 dom = html.fromstring(resp.text)
30 search_res = dom.xpath('//table[@id="searchResult"]//tr')
32 search_res = dom.xpath('//table[@id="searchResult"]//tr')
33+
31 if not search_res:
34 if not search_res:
32 return results
35 return results
36+
33 for result in search_res[1:]:
37 for result in search_res[1:]:
34 link = result.xpath('.//div[@class="detName"]//a')[0]
38 link = result.xpath('.//div[@class="detName"]//a')[0]
35 href = urljoin(url, link.attrib.get('href'))
39 href = urljoin(url, link.attrib.get('href'))
36 title = ' '.join(link.xpath('.//text()'))
40 title = ' '.join(link.xpath('.//text()'))
37 content = escape(' '.join(result.xpath(content_xpath)))
41 content = escape(' '.join(result.xpath(content_xpath)))
38 seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
42 seed, leech = result.xpath('.//td[@align="right"]/text()')[:2]
43+
44+ if seed.isdigit():
45+ seed = int(seed)
46+ else:
47+ seed = 0
48+
49+ if leech.isdigit():
50+ leech = int(leech)
51+ else:
52+ leech = 0
53+
39 magnetlink = result.xpath(magnet_xpath)[0]
54 magnetlink = result.xpath(magnet_xpath)[0]
40 results.append({'url': href,
55 results.append({'url': href,
41 'title': title,
56 'title': title,
@@ -44,10 +59,5 @@ def response(resp):
44 'leech': leech,
59 'leech': leech,
45 'magnetlink': magnetlink.attrib['href'],
60 'magnetlink': magnetlink.attrib['href'],
46 'template': 'torrent.html'})
61 'template': 'torrent.html'})
47− return sorted(results, key=lambda x: get_int('seed'), reversed=True)
48
62
49−def get_int(field):
63+ return sorted(results, key=itemgetter('seed'), reverse=True)
50− try:
51− return int(field)
52− except TypeError:
53− return 0