on master help sign in
[mod] tpb sort refactor
commit7e946a369b5caae3997fb9398a8a40a2ef389193 authorAdam Tauber <asciimoo@gmail.com> dateFri 16 May 2014, 16:49 (12 years ago) parent6d84ef1
1 file changed+16−6
searx/engines/piratebay.py+16−6
searx/engines/piratebay.py+16−6
@@ -2,6 +2,7 @@ from urlparse import urljoin
22from cgi import escape
33from urllib import quote
44from lxml import html
5+from operator import itemgetter
56
67categories = ['videos', 'music']
78
@@ -24,18 +25,32 @@ def request(query, params):
2425 pageno=params['pageno'] - 1)
2526 return params
2627
28+
2729def response(resp):
2830 results = []
2931 dom = html.fromstring(resp.text)
3032 search_res = dom.xpath('//table[@id="searchResult"]//tr')
33+
3134 if not search_res:
3235 return results
36+
3337 for result in search_res[1:]:
3438 link = result.xpath('.//div[@class="detName"]//a')[0]
3539 href = urljoin(url, link.attrib.get('href'))
3640 title = ' '.join(link.xpath('.//text()'))
3741 content = escape(' '.join(result.xpath(content_xpath)))
3842 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+
3954 magnetlink = result.xpath(magnet_xpath)[0]
4055 results.append({'url': href,
4156 'title': title,
@@ -44,10 +59,5 @@ def response(resp):
4459 'leech': leech,
4560 'magnetlink': magnetlink.attrib['href'],
4661 'template': 'torrent.html'})
47− return sorted(results, key=lambda x: get_int('seed'), reversed=True)
4862
49−def get_int(field):
50− try:
51− return int(field)
52− except TypeError:
53− return 0
63+ return sorted(results, key=itemgetter('seed'), reverse=True)