summaryrefslogtreecommitdiff
path: root/searx/search/processors/online_currency.py
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2020-12-16 13:41:32 +0100
committerAlexandre Flament <alex@al-f.net>2020-12-17 11:39:36 +0100
commit7ec8bc3ea76516e33318c67165161df5c1efdd36 (patch)
tree6c9dff310882db816cada8662ef5ed2b8a8158e8 /searx/search/processors/online_currency.py
parentc0cc01e936593ff3df828fa3bb834507c45cd7ac (diff)
[mod] split searx.search into different processors
see searx.search.processors.abstract.EngineProcessor First the method searx call the get_params method. If the return value is not None, then the searx call the method search.
Diffstat (limited to 'searx/search/processors/online_currency.py')
-rw-r--r--searx/search/processors/online_currency.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/searx/search/processors/online_currency.py b/searx/search/processors/online_currency.py
new file mode 100644
index 000000000..f0e919c03
--- /dev/null
+++ b/searx/search/processors/online_currency.py
@@ -0,0 +1,57 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+
+import unicodedata
+import re
+
+from searx.data import CURRENCIES
+from .online import OnlineProcessor
+
+
+parser_re = re.compile('.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
+
+
+def normalize_name(name):
+ name = name.lower().replace('-', ' ').rstrip('s')
+ name = re.sub(' +', ' ', name)
+ return unicodedata.normalize('NFKD', name).lower()
+
+
+def name_to_iso4217(name):
+ global CURRENCIES
+ name = normalize_name(name)
+ currency = CURRENCIES['names'].get(name, [name])
+ return currency[0]
+
+
+def iso4217_to_name(iso4217, language):
+ global CURRENCIES
+ return CURRENCIES['iso4217'].get(iso4217, {}).get(language, iso4217)
+
+
+class OnlineCurrencyProcessor(OnlineProcessor):
+
+ engine_type = 'online_currency'
+
+ def get_params(self, search_query, engine_category):
+ params = super().get_params(search_query, engine_category)
+ if params is None:
+ return None
+
+ m = parser_re.match(search_query.query)
+ if not m:
+ return None
+
+ amount_str, from_currency, to_currency = m.groups()
+ try:
+ amount = float(amount_str)
+ except ValueError:
+ return None
+ from_currency = name_to_iso4217(from_currency.strip())
+ to_currency = name_to_iso4217(to_currency.strip())
+
+ params['amount'] = amount
+ params['from'] = from_currency
+ params['to'] = to_currency
+ params['from_name'] = iso4217_to_name(from_currency, 'en')
+ params['to_name'] = iso4217_to_name(to_currency, 'en')
+ return params