Skip to content

Commit

Permalink
Implement google/brave switch in Mullvad Leta
Browse files Browse the repository at this point in the history
cleanup

Import annontations
  • Loading branch information
glanham-jr authored and return42 committed Jul 7, 2024
1 parent c835f92 commit ef103ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
34 changes: 31 additions & 3 deletions searx/engines/mullvad_leta.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
update of SearXNG!
"""

from __future__ import annotations

from typing import TYPE_CHECKING
from httpx import Response
from lxml import html
Expand All @@ -37,6 +39,8 @@

use_cache: bool = True # non-cache use only has 100 searches per day!

leta_engine: str = 'google'

search_url = "https://leta.mullvad.net"

# about
Expand All @@ -61,6 +65,11 @@
"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"""
Expand All @@ -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:
Expand All @@ -107,15 +127,23 @@ 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),
'content': extract_text(p_elem),
}


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"""
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions searx/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ef103ba

Please sign in to comment.