[mod] tpb sort refactor

This commit is contained in:
Adam Tauber 2014-05-16 16:49:42 +02:00
parent 6d84ef13f2
commit 7e946a369b
1 changed files with 16 additions and 6 deletions

View File

@ -2,6 +2,7 @@ from urlparse import urljoin
from cgi import escape from cgi import escape
from urllib import quote from urllib import quote
from lxml import html from lxml import html
from operator import itemgetter
categories = ['videos', 'music'] categories = ['videos', 'music']
@ -24,18 +25,32 @@ def request(query, params):
pageno=params['pageno'] - 1) pageno=params['pageno'] - 1)
return params return params
def response(resp): def response(resp):
results = [] results = []
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
search_res = dom.xpath('//table[@id="searchResult"]//tr') search_res = dom.xpath('//table[@id="searchResult"]//tr')
if not search_res: if not search_res:
return results return results
for result in search_res[1:]: for result in search_res[1:]:
link = result.xpath('.//div[@class="detName"]//a')[0] link = result.xpath('.//div[@class="detName"]//a')[0]
href = urljoin(url, link.attrib.get('href')) href = urljoin(url, link.attrib.get('href'))
title = ' '.join(link.xpath('.//text()')) title = ' '.join(link.xpath('.//text()'))
content = escape(' '.join(result.xpath(content_xpath))) content = escape(' '.join(result.xpath(content_xpath)))
seed, leech = result.xpath('.//td[@align="right"]/text()')[:2] 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] magnetlink = result.xpath(magnet_xpath)[0]
results.append({'url': href, results.append({'url': href,
'title': title, 'title': title,
@ -44,10 +59,5 @@ def response(resp):
'leech': leech, 'leech': leech,
'magnetlink': magnetlink.attrib['href'], 'magnetlink': magnetlink.attrib['href'],
'template': 'torrent.html'}) 'template': 'torrent.html'})
return sorted(results, key=lambda x: get_int('seed'), reversed=True)
def get_int(field): return sorted(results, key=itemgetter('seed'), reverse=True)
try:
return int(field)
except TypeError:
return 0