From 750b84410db5700402e2c66f47b3a45b2dbd1dfd Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 11 Sep 2023 10:24:57 +0200 Subject: [PATCH] [feat] odysee: implement fetch_traits for language support --- searx/engines/odysee.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/searx/engines/odysee.py b/searx/engines/odysee.py index 4b0f49375..89d11d093 100644 --- a/searx/engines/odysee.py +++ b/searx/engines/odysee.py @@ -9,6 +9,14 @@ import time from urllib.parse import urlencode from datetime import datetime +import babel + +from searx.network import get +from searx.locales import language_tag +from searx.enginelib.traits import EngineTraits + +traits: EngineTraits + # Engine metadata about = { "website": "https://odysee.com/", @@ -46,6 +54,10 @@ def request(query, params): "mediaType": "video", } + lang = traits.get_language(params['searxng_locale'], None) + if lang is not None: + query_params['language'] = lang + if params['time_range'] in time_range_dict: query_params['time_filter'] = time_range_dict[params['time_range']] @@ -99,3 +111,35 @@ def response(resp): ) return results + + +def fetch_traits(engine_traits: EngineTraits): + """ + Fetch languages from Odysee's source code. + """ + + resp = get( + 'https://raw.githubusercontent.com/OdyseeTeam/odysee-frontend/master/ui/constants/supported_browser_languages.js', # pylint: disable=line-too-long + timeout=60, + ) + + if not resp.ok: + print("ERROR: can't determine languages from Odysee") + return + + for line in resp.text.split("\n")[1:-4]: + lang_tag = line.strip().split(": ")[0].replace("'", "") + + try: + sxng_tag = language_tag(babel.Locale.parse(lang_tag, sep="-")) + except babel.UnknownLocaleError: + print("ERROR: %s is unknown by babel" % lang_tag) + continue + + conflict = engine_traits.languages.get(sxng_tag) + if conflict: + if conflict != lang_tag: + print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, lang_tag)) + continue + + engine_traits.languages[sxng_tag] = lang_tag