.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 initialize(self):
28 CURRENCIES.init()
29 super().initialize()
30
31 def get_params(self, search_query, engine_category):
32 """Returns a set of :ref:`request params <engine request online_currency>`
33 or ``None`` if search query does not match to :py:obj:`parser_re`."""
34
35 params = super().get_params(search_query, engine_category)
36 if params is None:
37 return None
38
39 m = parser_re.match(search_query.query)
40 if not m:
41 return None
42
43 amount_str, from_currency, to_currency = m.groups()
44 try:
45 amount = float(amount_str)
46 except ValueError:
47 return None
48
49 from_currency = CURRENCIES.name_to_iso4217(normalize_name(from_currency))
50 to_currency = CURRENCIES.name_to_iso4217(normalize_name(to_currency))
51
52 params['amount'] = amount
53 params['from'] = from_currency
54 params['to'] = to_currency
55 params['from_name'] = CURRENCIES.iso4217_to_name(from_currency, "en")
56 params['to_name'] = CURRENCIES.iso4217_to_name(to_currency, "en")
57 return params
58
60 tests = {}
61
62 tests['currency'] = {
63 'matrix': {'query': '1337 usd in rmb'},
64 'result_container': ['has_answer'],
65 }
66
67 return tests