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] various engines: use eval_xpath* functions and searx.exceptions.*
Alexandre Flament64cccae99e62
diff --git a/searx/engines/ahmia.py b/searx/engines/ahmia.pyindex d9fcc6ca7..7a2ae0075 100644--- a/searx/engines/ahmia.py+++ b/searx/engines/ahmia.py@@ -12,7 +12,7 @@from urllib.parse import urlencode, urlparse, parse_qsfrom lxml.html import fromstring-from searx.engines.xpath import extract_url, extract_text+from searx.engines.xpath import extract_url, extract_text, eval_xpath_list, eval_xpath# engine configcategories = ['onions']@@ -50,17 +50,17 @@ def response(resp):# trim results so there's not way too many at oncefirst_result_index = page_size * (resp.search_params.get('pageno', 1) - 1)- all_results = dom.xpath(results_xpath)+ all_results = eval_xpath_list(dom, results_xpath)trimmed_results = all_results[first_result_index:first_result_index + page_size]# get resultsfor result in trimmed_results:# remove ahmia url and extract the actual url for the result- raw_url = extract_url(result.xpath(url_xpath), search_url)+ raw_url = extract_url(eval_xpath_list(result, url_xpath, min_len=1), search_url)cleaned_url = parse_qs(urlparse(raw_url).query).get('redirect_url', [''])[0]- title = extract_text(result.xpath(title_xpath))- content = extract_text(result.xpath(content_xpath))+ title = extract_text(eval_xpath(result, title_xpath))+ content = extract_text(eval_xpath(result, content_xpath))results.append({'url': cleaned_url,'title': title,@@ -68,11 +68,11 @@ def response(resp):'is_onion': True})# get spelling corrections- for correction in dom.xpath(correction_xpath):+ for correction in eval_xpath_list(dom, correction_xpath):results.append({'correction': extract_text(correction)})# get number of results- number_of_results = dom.xpath(number_of_results_xpath)+ number_of_results = eval_xpath(dom, number_of_results_xpath)if number_of_results:try:results.append({'number_of_results': int(extract_text(number_of_results))})diff --git a/searx/engines/duckduckgo_images.py b/searx/engines/duckduckgo_images.pyindex 438a8d54c..009f81cca 100644--- a/searx/engines/duckduckgo_images.py+++ b/searx/engines/duckduckgo_images.py@@ -15,6 +15,7 @@from json import loadsfrom urllib.parse import urlencode+from searx.exceptions import SearxEngineAPIExceptionfrom searx.engines.duckduckgo import get_region_codefrom searx.engines.duckduckgo import _fetch_supported_languages, supported_languages_url # NOQA # pylint: disable=unused-importfrom searx.poolrequests import get@@ -37,7 +38,7 @@ def get_vqd(query, headers):res = get(query_url, headers=headers)content = res.textif content.find('vqd=\'') == -1:- raise Exception('Request failed')+ raise SearxEngineAPIException('Request failed')vqd = content[content.find('vqd=\'') + 5:]vqd = vqd[:vqd.find('\'')]return vqd@@ -71,10 +72,7 @@ def response(resp):results = []content = resp.text- try:- res_json = loads(content)- except:- raise Exception('Cannot parse results')+ res_json = loads(content)# parse resultsfor result in res_json['results']:diff --git a/searx/engines/elasticsearch.py b/searx/engines/elasticsearch.pyindex 99e93d876..081736c1c 100644--- a/searx/engines/elasticsearch.py+++ b/searx/engines/elasticsearch.py@@ -1,5 +1,6 @@from json import loads, dumpsfrom requests.auth import HTTPBasicAuth+from searx.exceptions import SearxEngineAPIExceptionbase_url = 'http://localhost:9200'@@ -107,7 +108,7 @@ def response(resp):resp_json = loads(resp.text)if 'error' in resp_json:- raise Exception(resp_json['error'])+ raise SearxEngineAPIException(resp_json['error'])for result in resp_json['hits']['hits']:r = {key: str(value) if not key.startswith('_') else value for key, value in result['_source'].items()}diff --git a/searx/engines/google.py b/searx/engines/google.pyindex 83b18a9a0..17ab21f6a 100644--- a/searx/engines/google.py+++ b/searx/engines/google.py@@ -20,9 +20,10 @@ Definitions`_.from urllib.parse import urlencode, urlparsefrom lxml import html-from flask_babel import gettextfrom searx import logger-from searx.utils import match_language, extract_text, eval_xpath+from searx.utils import match_language, extract_text, eval_xpath, eval_xpath_list, eval_xpath_getindex+from searx.exceptions import SearxEngineCaptchaException+logger = logger.getChild('google engine')@@ -131,14 +132,6 @@ suggestion_xpath = '//div[contains(@class, "card-section")]//a'spelling_suggestion_xpath = '//div[@class="med"]/p/a'-def extract_text_from_dom(result, xpath):- """returns extract_text on the first result selected by the xpath or None"""- r = eval_xpath(result, xpath)- if len(r) > 0:- return extract_text(r[0])- return None--def get_lang_country(params, lang_list, custom_aliases):"""Returns a tuple with *langauage* on its first and *country* on its secondposition."""@@ -210,10 +203,10 @@ def response(resp):# detect google sorryresp_url = urlparse(resp.url)if resp_url.netloc == 'sorry.google.com' or resp_url.path == '/sorry/IndexRedirect':- raise RuntimeWarning('sorry.google.com')+ raise SearxEngineCaptchaException()if resp_url.path.startswith('/sorry'):- raise RuntimeWarning(gettext('CAPTCHA required'))+ raise SearxEngineCaptchaException()# which subdomain ?# subdomain = resp.search_params.get('google_subdomain')@@ -229,18 +222,17 @@ def response(resp):logger.debug("did not found 'answer'")# results --> number_of_results- try:- _txt = eval_xpath(dom, '//div[@id="result-stats"]//text()')[0]- _digit = ''.join([n for n in _txt if n.isdigit()])- number_of_results = int(_digit)- results.append({'number_of_results': number_of_results})-- except Exception as e: # pylint: disable=broad-except- logger.debug("did not 'number_of_results'")- logger.error(e, exc_info=True)+ try:+ _txt = eval_xpath_getindex(dom, '//div[@id="result-stats"]//text()', 0)+ _digit = ''.join([n for n in _txt if n.isdigit()])+ number_of_results = int(_digit)+ results.append({'number_of_results': number_of_results})+ except Exception as e: # pylint: disable=broad-except+ logger.debug("did not 'number_of_results'")+ logger.error(e, exc_info=True)# parse results- for result in eval_xpath(dom, results_xpath):+ for result in eval_xpath_list(dom, results_xpath):# google *sections*if extract_text(eval_xpath(result, g_section_with_header)):@@ -248,14 +240,14 @@ def response(resp):continuetry:- title_tag = eval_xpath(result, title_xpath)- if not title_tag:+ title_tag = eval_xpath_getindex(result, title_xpath, 0, default=None)+ if title_tag is None:# this not one of the common google results *section*logger.debug('ingoring <div class="g" ../> section: missing title')continue- title = extract_text(title_tag[0])- url = eval_xpath(result, href_xpath)[0]- content = extract_text_from_dom(result, content_xpath)+ title = extract_text(title_tag)+ url = eval_xpath_getindex(result, href_xpath, 0)+ content = extract_text(eval_xpath_getindex(result, content_xpath, 0, default=None), allow_none=True)results.append({'url': url,'title': title,@@ -270,11 +262,11 @@ def response(resp):continue# parse suggestion- for suggestion in eval_xpath(dom, suggestion_xpath):+ for suggestion in eval_xpath_list(dom, suggestion_xpath):# append suggestionresults.append({'suggestion': extract_text(suggestion)})- for correction in eval_xpath(dom, spelling_suggestion_xpath):+ for correction in eval_xpath_list(dom, spelling_suggestion_xpath):results.append({'correction': extract_text(correction)})# return results@@ -286,7 +278,7 @@ def _fetch_supported_languages(resp):ret_val = {}dom = html.fromstring(resp.text)- radio_buttons = eval_xpath(dom, '//*[@id="langSec"]//input[@name="lr"]')+ radio_buttons = eval_xpath_list(dom, '//*[@id="langSec"]//input[@name="lr"]')for x in radio_buttons:name = x.get("data-name")diff --git a/searx/engines/google_images.py b/searx/engines/google_images.pyindex a3daf6070..9ef1be753 100644--- a/searx/engines/google_images.py+++ b/searx/engines/google_images.py@@ -26,8 +26,8 @@ Definitions`_.from urllib.parse import urlencode, urlparse, unquotefrom lxml import html-from flask_babel import gettextfrom searx import logger+from searx.exceptions import SearxEngineCaptchaExceptionfrom searx.utils import extract_text, eval_xpathfrom searx.engines.google import _fetch_supported_languages, supported_languages_url # NOQA # pylint: disable=unused-import@@ -128,10 +128,10 @@ def response(resp):# detect google sorryresp_url = urlparse(resp.url)if resp_url.netloc == 'sorry.google.com' or resp_url.path == '/sorry/IndexRedirect':- raise RuntimeWarning('sorry.google.com')+ raise SearxEngineCaptchaException()if resp_url.path.startswith('/sorry'):- raise RuntimeWarning(gettext('CAPTCHA required'))+ raise SearxEngineCaptchaException()# which subdomain ?# subdomain = resp.search_params.get('google_subdomain')diff --git a/searx/engines/google_videos.py b/searx/engines/google_videos.pyindex 1e6c8b3ee..eedefbf45 100644--- a/searx/engines/google_videos.py+++ b/searx/engines/google_videos.py@@ -13,7 +13,7 @@from datetime import date, timedeltafrom urllib.parse import urlencodefrom lxml import html-from searx.utils import extract_text+from searx.utils import extract_text, eval_xpath, eval_xpath_list, eval_xpath_getindeximport re# engine dependent config@@ -66,11 +66,11 @@ def response(resp):dom = html.fromstring(resp.text)# parse results- for result in dom.xpath('//div[@class="g"]'):+ for result in eval_xpath_list(dom, '//div[@class="g"]'):- title = extract_text(result.xpath('.//h3'))- url = result.xpath('.//div[@class="r"]/a/@href')[0]- content = extract_text(result.xpath('.//span[@class="st"]'))+ title = extract_text(eval_xpath(result, './/h3'))+ url = eval_xpath_getindex(result, './/div[@class="r"]/a/@href', 0)+ content = extract_text(eval_xpath(result, './/span[@class="st"]'))# get thumbnailsscript = str(dom.xpath('//script[contains(., "_setImagesSrc")]')[0].text)diff --git a/searx/engines/youtube_api.py b/searx/engines/youtube_api.pyindex 4a205ed6c..8c12ac4d2 100644--- a/searx/engines/youtube_api.py+++ b/searx/engines/youtube_api.py@@ -11,6 +11,7 @@from json import loadsfrom dateutil import parserfrom urllib.parse import urlencode+from searx.exceptions import SearxEngineAPIException# engine dependent configcategories = ['videos', 'music']@@ -48,7 +49,7 @@ def response(resp):search_results = loads(resp.text)if 'error' in search_results and 'message' in search_results['error']:- raise Exception(search_results['error']['message'])+ raise SearxEngineAPIException(search_results['error']['message'])# return empty array if there are no resultsif 'items' not in search_results: