diff options
| author | Markus Heiser <markus.heiser@darmarIT.de> | 2025-05-25 10:40:57 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-25 10:40:57 +0200 |
| commit | 848c8d0544eb0138da4414a750f79b22883f91b2 (patch) | |
| tree | 54a1ff72ef9e6d18bb6e5b5551edf6be2871e372 /searx/search/processors | |
| parent | e46187e3ce1fd118951c0e40eb8432af75acad7c (diff) | |
[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 <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/search/processors')
| -rw-r--r-- | searx/search/processors/online_currency.py | 24 |
1 files changed, 7 insertions, 17 deletions
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): |