[mod] tpb sort refactor

This commit is contained in:
Adam Tauber 2014-05-16 16:49:42 +02:00
parent 6d84ef13f2
commit 7e946a369b

View File

@ -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)