From 241e18854ebd29975e2a4eec5fa0f5f617fd5942 Mon Sep 17 00:00:00 2001 From: Grant Lanham Date: Fri, 12 Jan 2024 12:26:38 -0500 Subject: [PATCH] fix yammlint and pylint --- searx/engines/mullvad_leta.py | 39 +++++++++++++++++++---------------- searx/settings.yml | 26 +++++++++++------------ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/searx/engines/mullvad_leta.py b/searx/engines/mullvad_leta.py index ed15204d7..c06b7ccea 100644 --- a/searx/engines/mullvad_leta.py +++ b/searx/engines/mullvad_leta.py @@ -15,9 +15,9 @@ Otherwise, follow instructions provided by Mullvad for enabling the VPN on Linux """ +from typing import TYPE_CHECKING, List from httpx import Response from lxml import html -from typing import TYPE_CHECKING, Tuple, List from searx.enginelib.traits import EngineTraits from searx.locales import region_tag, get_official_locales from searx.utils import eval_xpath, extract_text, eval_xpath_list @@ -25,6 +25,7 @@ from searx.exceptions import SearxEngineResponseException if TYPE_CHECKING: import logging + logger = logging.getLogger() traits: EngineTraits @@ -57,13 +58,13 @@ time_range_dict = { def is_vpn_connected(dom: html.HtmlElement) -> bool: - """ Returns true if the VPN is connected, False otherwise """ + """Returns true if the VPN is connected, False otherwise""" connected_text = extract_text(eval_xpath(dom, '//main/div/p[1]')) return connected_text != 'You are not connected to Mullvad VPN.' def assign_headers(headers: dict) -> dict: - """ Assigns the headers to make a request to Mullvad Leta """ + """Assigns the headers to make a request to Mullvad Leta""" headers['Accept'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" headers['Accept-Encoding'] = "gzip, deflate, br" # headers['Accept-Language'] = "en-US,en;q=0.5" @@ -92,10 +93,12 @@ def request(query: str, params: dict): category: 'search category' pageno : 1 # number of the requested page """ + def get_country(eng_traits: EngineTraits) -> str: sxng_locale = params.get('searxng_locale', 'all') country = eng_traits.get_region(sxng_locale, eng_traits.all_locale) return country if country is str else '' + country = get_country(traits) params['url'] = search_url params['method'] = 'POST' @@ -128,18 +131,19 @@ def response(resp: Response): """post-response callback resp: requests response object """ - def extract_result(dom_result: html.HtmlElement): - [a_elem, h3_elem, p_elem] = eval_xpath_list(dom_result, - 'div/div/*') + + def extract_result(dom_result: html.HtmlElement): + [a_elem, h3_elem, p_elem] = eval_xpath_list(dom_result, 'div/div/*') return { 'url': extract_text(a_elem.text), 'title': extract_text(h3_elem), 'content': extract_text(p_elem), } - #def are_more_pages(dom: html.HtmlElement) -> bool: + + # def are_more_pages(dom: html.HtmlElement) -> bool: # previous and next buttons. If only 1, then just previous # return len(eval_xpath_list(dom, '//main/div[3]/form')) == 2 - #def get_page_x_of_y(dom: html.HtmlElement) -> Tuple[int, int]: + # def get_page_x_of_y(dom: html.HtmlElement) -> Tuple[int, int]: # page_x_of_y = extract_text(eval_xpath(dom.body, '//main/div[3]/span')) # if page_x_of_y is None or page_x_of_y == '': # return (0, 0) @@ -163,11 +167,11 @@ def fetch_traits(engine_traits: EngineTraits): # pylint: disable=import-outside-toplevel # see https://github.com/searxng/searxng/issues/762 from searx.network import post as http_post + # pylint: enable=import-outside-toplevel resp = http_post(search_url, headers=assign_headers({})) if not isinstance(resp, Response): - print( - "ERROR: failed to get response from mullvad-leta. Are you connected to the VPN?") + print("ERROR: failed to get response from mullvad-leta. Are you connected to the VPN?") return if not resp.ok: print("ERROR: response from mullvad-leta is not OK. Are you connected to the VPN?") @@ -176,20 +180,19 @@ def fetch_traits(engine_traits: EngineTraits): if not is_vpn_connected(dom): print('ERROR: Not connected to Mullvad VPN') # supported region codes - options = eval_xpath_list( - dom.body, '//main/div/form/div[2]/div/select[1]/option') + options = eval_xpath_list(dom.body, '//main/div/form/div[2]/div/select[1]/option') if options is None or len(options) <= 0: - print( - 'ERROR: could not find any results. Are you connected to the VPN?') + print('ERROR: could not find any results. Are you connected to the VPN?') for x in options: eng_country = x.get("value") - sxng_locales = get_official_locales( - eng_country, engine_traits.languages.keys(), regional=True) + sxng_locales = get_official_locales(eng_country, engine_traits.languages.keys(), regional=True) if not sxng_locales: - print("ERROR: can't map from Mullvad-Leta country %s (%s) to a babel region." % - (x.get('data-name'), eng_country)) + print( + "ERROR: can't map from Mullvad-Leta country %s (%s) to a babel region." + % (x.get('data-name'), eng_country) + ) continue for sxng_locale in sxng_locales: diff --git a/searx/settings.yml b/searx/settings.yml index 3e8f278d0..153574bbd 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -1192,19 +1192,19 @@ engines: require_api_key: false results: JSON - #- name: mullvadleta - # engine: mullvad_leta - # use_cache: true # Only 100 non-cache searches per day, suggested only for private instances - # search_url: https://leta.mullvad.net - # categories: [general, web] - # shortcut: ml - # about: - # website: https://leta.mullvad.net/ - # wikidata_id: Q47008412 - # official_api_documentation: https://leta.mullvad.net/faq - # use_official_api: false - # require_api_key: false - # results: HTML + # - name: mullvadleta + # engine: mullvad_leta + # use_cache: true # Only 100 non-cache searches per day, suggested only for private instances + # search_url: https://leta.mullvad.net + # categories: [general, web] + # shortcut: ml + # about: + # website: https://leta.mullvad.net/ + # wikidata_id: Q47008412 + # official_api_documentation: https://leta.mullvad.net/faq + # use_official_api: false + # require_api_key: false + # results: HTML - name: odysee engine: odysee