From 7e946a369b5caae3997fb9398a8a40a2ef389193 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Fri, 16 May 2014 16:49:42 +0200 Subject: [PATCH] [mod] tpb sort refactor --- searx/engines/piratebay.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/searx/engines/piratebay.py b/searx/engines/piratebay.py index c14b70f3c..bb4886868 100644 --- a/searx/engines/piratebay.py +++ b/searx/engines/piratebay.py @@ -2,6 +2,7 @@ from urlparse import urljoin from cgi import escape from urllib import quote from lxml import html +from operator import itemgetter categories = ['videos', 'music'] @@ -24,18 +25,32 @@ def request(query, params): pageno=params['pageno'] - 1) return params + def response(resp): results = [] dom = html.fromstring(resp.text) search_res = dom.xpath('//table[@id="searchResult"]//tr') + if not search_res: return results + for result in search_res[1:]: link = result.xpath('.//div[@class="detName"]//a')[0] href = urljoin(url, link.attrib.get('href')) title = ' '.join(link.xpath('.//text()')) content = escape(' '.join(result.xpath(content_xpath))) seed, leech = result.xpath('.//td[@align="right"]/text()')[:2] + + if seed.isdigit(): + seed = int(seed) + else: + seed = 0 + + if leech.isdigit(): + leech = int(leech) + else: + leech = 0 + magnetlink = result.xpath(magnet_xpath)[0] results.append({'url': href, 'title': title, @@ -44,10 +59,5 @@ def response(resp): 'leech': leech, 'magnetlink': magnetlink.attrib['href'], 'template': 'torrent.html'}) - return sorted(results, key=lambda x: get_int('seed'), reversed=True) -def get_int(field): - try: - return int(field) - except TypeError: - return 0 + return sorted(results, key=itemgetter('seed'), reverse=True)