[mod] int_or_zero refactored to searx_utils

This commit is contained in:
misnyo 2017-09-04 20:05:04 +02:00
parent c3232b0e1a
commit 33fd938016
3 changed files with 23 additions and 29 deletions

View File

@ -12,7 +12,7 @@
from lxml import html from lxml import html
from searx.engines.xpath import extract_text from searx.engines.xpath import extract_text
from searx.url_utils import urlencode from searx.url_utils import urlencode
from searx.utils import get_torrent_size from searx.utils import get_torrent_size, int_or_zero
# engine dependent config # engine dependent config
categories = ['files', 'images', 'videos', 'music'] categories = ['files', 'images', 'videos', 'music']
@ -49,14 +49,14 @@ def response(resp):
for result in dom.xpath(xpath_results): for result in dom.xpath(xpath_results):
# defaults # defaults
filesize = 0 filesize = 0
seed = 0
leech = 0
downloads = 0
magnet_link = "" magnet_link = ""
torrent_link = "" torrent_link = ""
# category in which our torrent belongs # category in which our torrent belongs
try:
category = result.xpath(xpath_category)[0].attrib.get('title') category = result.xpath(xpath_category)[0].attrib.get('title')
except:
pass
# torrent title # torrent title
page_a = result.xpath(xpath_title)[0] page_a = result.xpath(xpath_title)[0]
@ -74,12 +74,14 @@ def response(resp):
# link to the torrent file # link to the torrent file
torrent_link = url torrent_link = url
# get seeders and leechers # seed count
try: seed = int_or_zero(result.xpath(xpath_seeds))
seed = int(result.xpath(xpath_seeds)[0])
leech = int(result.xpath(xpath_leeches)[0]) # leech count
except: leech = int_or_zero(result.xpath(xpath_leeches))
pass
# torrent downloads count
downloads = int_or_zero(result.xpath(xpath_downloads))
# let's try to calculate the torrent size # let's try to calculate the torrent size
try: try:
@ -89,12 +91,6 @@ def response(resp):
except: except:
pass pass
# torrent downloads count
try:
downloads = result.xpath(xpath_downloads)[0]
except:
pass
# content string contains all information not included into template # content string contains all information not included into template
content = 'Category: "{category}". Downloaded {downloads} times.' content = 'Category: "{category}". Downloaded {downloads} times.'
content = content.format(category=category, downloads=downloads) content = content.format(category=category, downloads=downloads)

View File

@ -15,7 +15,7 @@ from lxml import html
from searx.engines.xpath import extract_text from searx.engines.xpath import extract_text
from datetime import datetime from datetime import datetime
from searx.url_utils import urlencode from searx.url_utils import urlencode
from searx.utils import get_torrent_size from searx.utils import get_torrent_size, int_or_zero
# engine dependent config # engine dependent config
categories = ['files', 'videos', 'music'] categories = ['files', 'videos', 'music']
@ -26,17 +26,6 @@ base_url = 'https://www.tokyotosho.info/'
search_url = base_url + 'search.php?{query}' search_url = base_url + 'search.php?{query}'
# convert a variable to integer or return 0 if it's not a number
def int_or_zero(num):
if isinstance(num, list):
if len(num) < 1:
return 0
num = num[0]
if num.isdigit():
return int(num)
return 0
# do search-request # do search-request
def request(query, params): def request(query, params):
query = urlencode({'page': params['pageno'], 'terms': query}) query = urlencode({'page': params['pageno'], 'terms': query})

View File

@ -290,6 +290,15 @@ def convert_str_to_int(number_str):
return 0 return 0
# convert a variable to integer or return 0 if it's not a number
def int_or_zero(num):
if isinstance(num, list):
if len(num) < 1:
return 0
num = num[0]
return convert_str_to_int(num)
def is_valid_lang(lang): def is_valid_lang(lang):
is_abbr = (len(lang) == 2) is_abbr = (len(lang) == 2)
if is_abbr: if is_abbr: