.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
16 name = name.lower().replace('-', ' ').rstrip('s')
17 name = re.sub(' +', ' ', name)
18 return unicodedata.normalize('NFKD', name).lower()
19
20
22 name = normalize_name(name)
23 currency = CURRENCIES['names'].get(name, [name])
24 if isinstance(currency, str):
25 return currency
26 return currency[0]
27
28
29def iso4217_to_name(iso4217, language):
30 return CURRENCIES['iso4217'].get(iso4217, {}).get(language, iso4217)
31
32
34 """Processor class used by ``online_currency`` engines."""
35
36 engine_type = 'online_currency'
37
38 def get_params(self, search_query, engine_category):
39 """Returns a set of :ref:`request params <engine request online_currency>`
40 or ``None`` if search query does not match to :py:obj:`parser_re`."""
41
42 params = super().get_params(search_query, engine_category)
43 if params is None:
44 return None
45
46 m = parser_re.match(search_query.query)
47 if not m:
48 return None
49
50 amount_str, from_currency, to_currency = m.groups()
51 try:
52 amount = float(amount_str)
53 except ValueError:
54 return None
55 from_currency = name_to_iso4217(from_currency.strip())
56 to_currency = name_to_iso4217(to_currency.strip())
57
58 params['amount'] = amount
59 params['from'] = from_currency
60 params['to'] = to_currency
61 params['from_name'] = iso4217_to_name(from_currency, 'en')
62 params['to_name'] = iso4217_to_name(to_currency, 'en')
63 return params
64
66 tests = {}
67
68 tests['currency'] = {
69 'matrix': {'query': '1337 usd in rmb'},
70 'result_container': ['has_answer'],
71 }
72
73 return tests