SearXNG is a free internet metasearch engine which aggregates results from various search services and databases. Users are neither tracked nor profiled.
Python bingbravedegoogleduckduckgogooglemetasearchprivacypythonqwantsearchsearch-enginesearxsearxngstartpageyahoo
[mod] int_or_zero refactored to searx_utils
misnyo33fd938016de
diff --git a/searx/engines/nyaa.py b/searx/engines/nyaa.pyindex 0d5bfc917..6a8e598c4 100644--- a/searx/engines/nyaa.py+++ b/searx/engines/nyaa.py@@ -12,7 +12,7 @@from lxml import htmlfrom searx.engines.xpath import extract_textfrom searx.url_utils import urlencode-from searx.utils import get_torrent_size+from searx.utils import get_torrent_size, int_or_zero# engine dependent configcategories = ['files', 'images', 'videos', 'music']@@ -49,14 +49,14 @@ def response(resp):for result in dom.xpath(xpath_results):# defaultsfilesize = 0- seed = 0- leech = 0- downloads = 0magnet_link = ""torrent_link = ""# category in which our torrent belongs- category = result.xpath(xpath_category)[0].attrib.get('title')+ try:+ category = result.xpath(xpath_category)[0].attrib.get('title')+ except:+ pass# torrent titlepage_a = result.xpath(xpath_title)[0]@@ -74,12 +74,14 @@ def response(resp):# link to the torrent filetorrent_link = url- # get seeders and leechers- try:- seed = int(result.xpath(xpath_seeds)[0])- leech = int(result.xpath(xpath_leeches)[0])- except:- pass+ # seed count+ seed = int_or_zero(result.xpath(xpath_seeds))++ # leech count+ leech = int_or_zero(result.xpath(xpath_leeches))++ # torrent downloads count+ downloads = int_or_zero(result.xpath(xpath_downloads))# let's try to calculate the torrent sizetry:@@ -89,12 +91,6 @@ def response(resp):except:pass- # torrent downloads count- try:- downloads = result.xpath(xpath_downloads)[0]- except:- pass-# content string contains all information not included into templatecontent = 'Category: "{category}". Downloaded {downloads} times.'content = content.format(category=category, downloads=downloads)diff --git a/searx/engines/tokyotoshokan.py b/searx/engines/tokyotoshokan.pyindex dfd2e7790..773212043 100644--- a/searx/engines/tokyotoshokan.py+++ b/searx/engines/tokyotoshokan.py@@ -15,7 +15,7 @@ from lxml import htmlfrom searx.engines.xpath import extract_textfrom datetime import datetimefrom searx.url_utils import urlencode-from searx.utils import get_torrent_size+from searx.utils import get_torrent_size, int_or_zero# engine dependent configcategories = ['files', 'videos', 'music']@@ -26,17 +26,6 @@ base_url = 'https://www.tokyotosho.info/'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-requestdef request(query, params):query = urlencode({'page': params['pageno'], 'terms': query})diff --git a/searx/utils.py b/searx/utils.pyindex 3df571160..35b20ad83 100644--- a/searx/utils.py+++ b/searx/utils.py@@ -290,6 +290,15 @@ def convert_str_to_int(number_str):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):is_abbr = (len(lang) == 2)if is_abbr: