.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
online_currency.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Processors for engine-type: ``online_currency``
3
4"""
5
6import unicodedata
7import re
8
9from searx.data import CURRENCIES
10from .online import OnlineProcessor
11
12parser_re = re.compile('.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
13
14
15def normalize_name(name: str):
16 name = name.strip()
17 name = name.lower().replace('-', ' ').rstrip('s')
18 name = re.sub(' +', ' ', name)
19 return unicodedata.normalize('NFKD', name).lower()
20
21
23 """Processor class used by ``online_currency`` engines."""
24
25 engine_type = 'online_currency'
26
27 def get_params(self, search_query, engine_category):
28 """Returns a set of :ref:`request params <engine request online_currency>`
29 or ``None`` if search query does not match to :py:obj:`parser_re`."""
30
31 params = super().get_params(search_query, engine_category)
32 if params is None:
33 return None
34
35 m = parser_re.match(search_query.query)
36 if not m:
37 return None
38
39 amount_str, from_currency, to_currency = m.groups()
40 try:
41 amount = float(amount_str)
42 except ValueError:
43 return None
44
45 from_currency = CURRENCIES.name_to_iso4217(normalize_name(from_currency))
46 to_currency = CURRENCIES.name_to_iso4217(normalize_name(to_currency))
47
48 params['amount'] = amount
49 params['from'] = from_currency
50 params['to'] = to_currency
51 params['from_name'] = CURRENCIES.iso4217_to_name(from_currency, "en")
52 params['to_name'] = CURRENCIES.iso4217_to_name(to_currency, "en")
53 return params
54
56 tests = {}
57
58 tests['currency'] = {
59 'matrix': {'query': '1337 usd in rmb'},
60 'result_container': ['has_answer'],
61 }
62
63 return tests