on master help sign in
[mod] int_or_zero refactored to searx_utils
commit33fd938016def4496876d89d0ccaa53f47705005 authormisnyo <misnyo@misnyo.eu> dateMon 4 Sep 2017, 20:05 (9 years ago) parentc3232b0
3 files changed+23−29
searx/engines/nyaa.py+13−17 searx/engines/tokyotoshokan.py+1−12 searx/utils.py+9
searx/engines/nyaa.py+13−17
@@ -12,7 +12,7 @@
1212from lxml import html
1313from searx.engines.xpath import extract_text
1414from searx.url_utils import urlencode
15−from searx.utils import get_torrent_size
15+from searx.utils import get_torrent_size, int_or_zero
1616
1717# engine dependent config
1818categories = ['files', 'images', 'videos', 'music']
@@ -49,14 +49,14 @@ def response(resp):
4949 for result in dom.xpath(xpath_results):
5050 # defaults
5151 filesize = 0
52− seed = 0
53− leech = 0
54− downloads = 0
5552 magnet_link = ""
5653 torrent_link = ""
5754
5855 # category in which our torrent belongs
59− category = result.xpath(xpath_category)[0].attrib.get('title')
56+ try:
57+ category = result.xpath(xpath_category)[0].attrib.get('title')
58+ except:
59+ pass
6060
6161 # torrent title
6262 page_a = result.xpath(xpath_title)[0]
@@ -74,12 +74,14 @@ def response(resp):
7474 # link to the torrent file
7575 torrent_link = url
7676
77− # get seeders and leechers
78− try:
79− seed = int(result.xpath(xpath_seeds)[0])
80− leech = int(result.xpath(xpath_leeches)[0])
81− except:
82− pass
77+ # seed count
78+ seed = int_or_zero(result.xpath(xpath_seeds))
79+
80+ # leech count
81+ leech = int_or_zero(result.xpath(xpath_leeches))
82+
83+ # torrent downloads count
84+ downloads = int_or_zero(result.xpath(xpath_downloads))
8385
8486 # let's try to calculate the torrent size
8587 try:
@@ -89,12 +91,6 @@ def response(resp):
8991 except:
9092 pass
9193
92− # torrent downloads count
93− try:
94− downloads = result.xpath(xpath_downloads)[0]
95− except:
96− pass
97−
9894 # content string contains all information not included into template
9995 content = 'Category: "{category}". Downloaded {downloads} times.'
10096 content = content.format(category=category, downloads=downloads)
searx/engines/tokyotoshokan.py+1−12
@@ -15,7 +15,7 @@ from lxml import html
1515from searx.engines.xpath import extract_text
1616from datetime import datetime
1717from searx.url_utils import urlencode
18−from searx.utils import get_torrent_size
18+from searx.utils import get_torrent_size, int_or_zero
1919
2020# engine dependent config
2121categories = ['files', 'videos', 'music']
@@ -26,17 +26,6 @@ base_url = 'https://www.tokyotosho.info/'
2626search_url = base_url + 'search.php?{query}'
2727
2828
29−# convert a variable to integer or return 0 if it's not a number
30−def int_or_zero(num):
31− if isinstance(num, list):
32− if len(num) < 1:
33− return 0
34− num = num[0]
35− if num.isdigit():
36− return int(num)
37− return 0
38−
39−
4029# do search-request
4130def request(query, params):
4231 query = urlencode({'page': params['pageno'], 'terms': query})
searx/utils.py+9
@@ -290,6 +290,15 @@ def convert_str_to_int(number_str):
290290 return 0
291291
292292
293+# convert a variable to integer or return 0 if it's not a number
294+def int_or_zero(num):
295+ if isinstance(num, list):
296+ if len(num) < 1:
297+ return 0
298+ num = num[0]
299+ return convert_str_to_int(num)
300+
301+
293302def is_valid_lang(lang):
294303 is_abbr = (len(lang) == 2)
295304 if is_abbr: