.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.mullvad_leta Namespace Reference

Functions

bool is_vpn_connected (html.HtmlElement dom)
 
dict assign_headers (dict headers)
 
 request (str query, dict params)
 
 extract_result (html.HtmlElement dom_result)
 
 response (Response resp)
 
 fetch_traits (EngineTraits engine_traits)
 

Variables

 logger = logging.getLogger()
 
EngineTraits traits
 
bool use_cache = True
 
str search_url = "https://leta.mullvad.net"
 
dict about
 
list categories = ['general', 'web']
 
bool paging = True
 
int max_page = 50
 
bool time_range_support = True
 
dict time_range_dict
 

Detailed Description

This is the implementation of the Mullvad-Leta meta-search engine.

This engine **REQUIRES** that searxng operate within a Mullvad VPN

If using docker, consider using gluetun for easily connecting to the Mullvad

- https://github.com/qdm12/gluetun

Otherwise, follow instructions provided by Mullvad for enabling the VPN on Linux

- https://mullvad.net/en/help/install-mullvad-app-linux

.. hint::

   The :py:obj:`EngineTraits` is empty by default.  Maintainers have to run
   ``make data.traits`` (in the Mullvad VPN / :py:obj:`fetch_traits`) and rebase
   the modified JSON file ``searx/data/engine_traits.json`` on every single
   update of SearXNG!

Function Documentation

◆ assign_headers()

dict searx.engines.mullvad_leta.assign_headers ( dict headers)
Assigns the headers to make a request to Mullvad Leta

Definition at line 71 of file mullvad_leta.py.

71def assign_headers(headers: dict) -> dict:
72 """Assigns the headers to make a request to Mullvad Leta"""
73 headers['Accept'] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8"
74 headers['Content-Type'] = "application/x-www-form-urlencoded"
75 headers['Host'] = "leta.mullvad.net"
76 headers['Origin'] = "https://leta.mullvad.net"
77 return headers
78
79

Referenced by searx.engines.mullvad_leta.fetch_traits(), and searx.engines.mullvad_leta.request().

+ Here is the caller graph for this function:

◆ extract_result()

searx.engines.mullvad_leta.extract_result ( html.HtmlElement dom_result)

Definition at line 110 of file mullvad_leta.py.

110def extract_result(dom_result: html.HtmlElement):
111 [a_elem, h3_elem, p_elem] = eval_xpath_list(dom_result, 'div/div/*')
112 return {
113 'url': extract_text(a_elem.text),
114 'title': extract_text(h3_elem),
115 'content': extract_text(p_elem),
116 }
117
118

Referenced by searx.engines.mullvad_leta.response().

+ Here is the caller graph for this function:

◆ fetch_traits()

searx.engines.mullvad_leta.fetch_traits ( EngineTraits engine_traits)
Fetch languages and regions from Mullvad-Leta

.. warning::

    Fetching the engine traits also requires a Mullvad VPN connection. If
    not connected, then an error message will print and no traits will be
    updated.

Definition at line 130 of file mullvad_leta.py.

130def fetch_traits(engine_traits: EngineTraits):
131 """Fetch languages and regions from Mullvad-Leta
132
133 .. warning::
134
135 Fetching the engine traits also requires a Mullvad VPN connection. If
136 not connected, then an error message will print and no traits will be
137 updated.
138 """
139 # pylint: disable=import-outside-toplevel
140 # see https://github.com/searxng/searxng/issues/762
141 from searx.network import post as http_post
142
143 # pylint: enable=import-outside-toplevel
144 resp = http_post(search_url, headers=assign_headers({}))
145 if not isinstance(resp, Response):
146 print("ERROR: failed to get response from mullvad-leta. Are you connected to the VPN?")
147 return
148 if not resp.ok:
149 print("ERROR: response from mullvad-leta is not OK. Are you connected to the VPN?")
150 return
151 dom = html.fromstring(resp.text)
152 if not is_vpn_connected(dom):
153 print('ERROR: Not connected to Mullvad VPN')
154 return
155 # supported region codes
156 options = eval_xpath_list(dom.body, '//main/div/form/div[2]/div/select[1]/option')
157 if options is None or len(options) <= 0:
158 print('ERROR: could not find any results. Are you connected to the VPN?')
159 for x in options:
160 eng_country = x.get("value")
161
162 sxng_locales = get_official_locales(eng_country, engine_traits.languages.keys(), regional=True)
163
164 if not sxng_locales:
165 print(
166 "ERROR: can't map from Mullvad-Leta country %s (%s) to a babel region."
167 % (x.get('data-name'), eng_country)
168 )
169 continue
170
171 for sxng_locale in sxng_locales:
172 engine_traits.regions[region_tag(sxng_locale)] = eng_country

References searx.engines.mullvad_leta.assign_headers(), and searx.engines.mullvad_leta.is_vpn_connected().

+ Here is the call graph for this function:

◆ is_vpn_connected()

bool searx.engines.mullvad_leta.is_vpn_connected ( html.HtmlElement dom)
Returns true if the VPN is connected, False otherwise

Definition at line 65 of file mullvad_leta.py.

65def is_vpn_connected(dom: html.HtmlElement) -> bool:
66 """Returns true if the VPN is connected, False otherwise"""
67 connected_text = extract_text(eval_xpath(dom, '//main/div/p[1]'))
68 return connected_text != 'You are not connected to Mullvad VPN.'
69
70

Referenced by searx.engines.mullvad_leta.fetch_traits(), and searx.engines.mullvad_leta.response().

+ Here is the caller graph for this function:

◆ request()

searx.engines.mullvad_leta.request ( str query,
dict params )

Definition at line 80 of file mullvad_leta.py.

80def request(query: str, params: dict):
81 country = traits.get_region(params.get('searxng_locale', 'all'), traits.all_locale) # type: ignore
82
83 params['url'] = search_url
84 params['method'] = 'POST'
85 params['data'] = {
86 "q": query,
87 "gl": country if country is str else '',
88 }
89 # pylint: disable=undefined-variable
90 if use_cache:
91 params['data']['oc'] = "on"
92 # pylint: enable=undefined-variable
93
94 if params['time_range'] in time_range_dict:
95 params['dateRestrict'] = time_range_dict[params['time_range']]
96 else:
97 params['dateRestrict'] = ''
98
99 if params['pageno'] > 1:
100 # Page 1 is n/a, Page 2 is 11, page 3 is 21, ...
101 params['data']['start'] = ''.join([str(params['pageno'] - 1), "1"])
102
103 if params['headers'] is None:
104 params['headers'] = {}
105
106 assign_headers(params['headers'])
107 return params
108
109

References searx.engines.mullvad_leta.assign_headers().

+ Here is the call graph for this function:

◆ response()

searx.engines.mullvad_leta.response ( Response resp)
Checks if connected to Mullvad VPN, then extracts the search results from
the DOM resp: requests response object

Definition at line 119 of file mullvad_leta.py.

119def response(resp: Response):
120 """Checks if connected to Mullvad VPN, then extracts the search results from
121 the DOM resp: requests response object"""
122
123 dom = html.fromstring(resp.text)
124 if not is_vpn_connected(dom):
125 raise SearxEngineResponseException('Not connected to Mullvad VPN')
126 search_results = eval_xpath(dom.body, '//main/div[2]/div')
127 return [extract_result(sr) for sr in search_results]
128
129

References searx.engines.mullvad_leta.extract_result(), and searx.engines.mullvad_leta.is_vpn_connected().

+ Here is the call graph for this function:

Variable Documentation

◆ about

dict searx.engines.mullvad_leta.about
Initial value:
1= {
2 "website": search_url,
3 "wikidata_id": 'Q47008412', # the Mullvad id - not leta, but related
4 "official_api_documentation": 'https://leta.mullvad.net/faq',
5 "use_official_api": False,
6 "require_api_key": False,
7 "results": 'HTML',
8}

Definition at line 43 of file mullvad_leta.py.

◆ categories

list searx.engines.mullvad_leta.categories = ['general', 'web']

Definition at line 53 of file mullvad_leta.py.

◆ logger

searx.engines.mullvad_leta.logger = logging.getLogger()

Definition at line 34 of file mullvad_leta.py.

◆ max_page

int searx.engines.mullvad_leta.max_page = 50

Definition at line 55 of file mullvad_leta.py.

◆ paging

bool searx.engines.mullvad_leta.paging = True

Definition at line 54 of file mullvad_leta.py.

◆ search_url

str searx.engines.mullvad_leta.search_url = "https://leta.mullvad.net"

Definition at line 40 of file mullvad_leta.py.

◆ time_range_dict

dict searx.engines.mullvad_leta.time_range_dict
Initial value:
1= {
2 "day": "d1",
3 "week": "w1",
4 "month": "m1",
5 "year": "y1",
6}

Definition at line 57 of file mullvad_leta.py.

◆ time_range_support

bool searx.engines.mullvad_leta.time_range_support = True

Definition at line 56 of file mullvad_leta.py.

◆ traits

EngineTraits searx.engines.mullvad_leta.traits

Definition at line 36 of file mullvad_leta.py.

◆ use_cache

bool searx.engines.mullvad_leta.use_cache = True

Definition at line 38 of file mullvad_leta.py.