Implement google/brave switch in Mullvad Leta

cleanup

Import annontations
This commit is contained in:
Grant Lanham 2024-06-22 14:02:16 -04:00 committed by Markus Heiser
parent c835f920ed
commit ef103ba80a
2 changed files with 32 additions and 3 deletions

View File

@ -20,6 +20,8 @@ Otherwise, follow instructions provided by Mullvad for enabling the VPN on Linux
update of SearXNG!
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from httpx import Response
from lxml import html
@ -37,6 +39,8 @@ traits: EngineTraits
use_cache: bool = True # non-cache use only has 100 searches per day!
leta_engine: str = 'google'
search_url = "https://leta.mullvad.net"
# about
@ -61,6 +65,11 @@ time_range_dict = {
"year": "y1",
}
available_leta_engines = [
'google', # first will be default if provided engine is invalid
'brave',
]
def is_vpn_connected(dom: html.HtmlElement) -> bool:
"""Returns true if the VPN is connected, False otherwise"""
@ -80,11 +89,22 @@ def assign_headers(headers: dict) -> dict:
def request(query: str, params: dict):
country = traits.get_region(params.get('searxng_locale', 'all'), traits.all_locale) # type: ignore
result_engine = leta_engine
if leta_engine not in available_leta_engines:
result_engine = available_leta_engines[0]
logger.warning(
'Configured engine "%s" not one of the available engines %s, defaulting to "%s"',
leta_engine,
available_leta_engines,
result_engine,
)
params['url'] = search_url
params['method'] = 'POST'
params['data'] = {
"q": query,
"gl": country if country is str else '',
'engine': result_engine,
}
# pylint: disable=undefined-variable
if use_cache:
@ -107,8 +127,8 @@ def request(query: str, params: dict):
return params
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: list[html.HtmlElement]):
[a_elem, h3_elem, p_elem] = dom_result
return {
'url': extract_text(a_elem.text),
'title': extract_text(h3_elem),
@ -116,6 +136,14 @@ def extract_result(dom_result: html.HtmlElement):
}
def extract_results(search_results: html.HtmlElement):
for search_result in search_results:
dom_result = eval_xpath_list(search_result, 'div/div/*')
# sometimes an info box pops up, will need to filter that out
if len(dom_result) == 3:
yield extract_result(dom_result)
def response(resp: Response):
"""Checks if connected to Mullvad VPN, then extracts the search results from
the DOM resp: requests response object"""
@ -124,7 +152,7 @@ def response(resp: Response):
if not is_vpn_connected(dom):
raise SearxEngineResponseException('Not connected to Mullvad VPN')
search_results = eval_xpath(dom.body, '//main/div[2]/div')
return [extract_result(sr) for sr in search_results]
return list(extract_results(search_results))
def fetch_traits(engine_traits: EngineTraits):

View File

@ -1234,6 +1234,7 @@ engines:
# read https://docs.searxng.org/dev/engines/online/mullvad_leta.html
# - name: mullvadleta
# engine: mullvad_leta
# leta_engine: google # choose one of the following: google, brave
# use_cache: true # Only 100 non-cache searches per day, suggested only for private instances
# search_url: https://leta.mullvad.net
# categories: [general, web]