From 848c8d0544eb0138da4414a750f79b22883f91b2 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sun, 25 May 2025 10:40:57 +0200 Subject: [mod] data: implement a simple currencies (SQL) database (#4836) To reduce the memory footprint, this patch no longer loads the JSON data completely into memory. Instead, there is an SQL database based on `ExpireCacheSQLite`. The class CurrenciesDB is a simple DB application that encapsulates the DB (queries and initialization) and provides convenient methods like `name_to_iso4217` and `iso4217_to_name`. Related: - https://github.com/searxng/searxng/discussions/1892 - https://github.com/searxng/searxng/pull/3458#issuecomment-2900807671 - https://github.com/searxng/searxng/pull/4650 Signed-off-by: Markus Heiser --- searx/search/processors/online_currency.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'searx/search') diff --git a/searx/search/processors/online_currency.py b/searx/search/processors/online_currency.py index 197e0e061..0d7900616 100644 --- a/searx/search/processors/online_currency.py +++ b/searx/search/processors/online_currency.py @@ -12,24 +12,13 @@ from .online import OnlineProcessor parser_re = re.compile('.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I) -def normalize_name(name): +def normalize_name(name: str): + name = name.strip() name = name.lower().replace('-', ' ').rstrip('s') name = re.sub(' +', ' ', name) return unicodedata.normalize('NFKD', name).lower() -def name_to_iso4217(name): - name = normalize_name(name) - currency = CURRENCIES['names'].get(name, [name]) - if isinstance(currency, str): - return currency - return currency[-1] - - -def iso4217_to_name(iso4217, language): - return CURRENCIES['iso4217'].get(iso4217, {}).get(language, iso4217) - - class OnlineCurrencyProcessor(OnlineProcessor): """Processor class used by ``online_currency`` engines.""" @@ -52,14 +41,15 @@ class OnlineCurrencyProcessor(OnlineProcessor): amount = float(amount_str) except ValueError: return None - from_currency = name_to_iso4217(from_currency.strip()) - to_currency = name_to_iso4217(to_currency.strip()) + + from_currency = CURRENCIES.name_to_iso4217(normalize_name(from_currency)) + to_currency = CURRENCIES.name_to_iso4217(normalize_name(to_currency)) 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') + params['from_name'] = CURRENCIES.iso4217_to_name(from_currency, "en") + params['to_name'] = CURRENCIES.iso4217_to_name(to_currency, "en") return params def get_default_tests(self): -- cgit v1.2.3