Authentication-Results: mail-b.sr.ht; dkim=pass header.d=disroot.org header.i=@disroot.org Received: from knopi.disroot.org (knopi.disroot.org [178.21.23.139]) by mail-b.sr.ht (Postfix) with ESMTPS id 3221111EF8D for <~metalune/simplytranslate-devel@lists.sr.ht>; Sun, 3 Apr 2022 14:33:36 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id A2FE9403CF; Sun, 3 Apr 2022 16:33:34 +0200 (CEST) X-Virus-Scanned: SPAM Filter at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 6ZtmZBxU3bvt; Sun, 3 Apr 2022 16:33:33 +0200 (CEST) From: fattalion DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1648996412; bh=kBkRf2PNwKqUeHd7H9BQGHHwScx9g/7yepRbh1AKtOQ=; h=From:To:Cc:Subject:Date; b=H0DN6V/zT2AihW1zYhkVwnCccMgEL/UDhryn7W1sDVIk4wpB1Q9pO9wPH9F+B6AkU 5lKI6WqDOuBj35NKWHbv5NMCe3Pw17MDARsylozXeCa5HUhBOp5hd7Jo2gi1nkubsE jngUbzIF7+3xGbESxviY6GdXJ30VoYalc8rMiM9+vIvkGHewUkFvaN0db5OdzVqajk ziGs9Y0prxZDdjxpXnkm0L/rsiTpzwgJhzuvn959DHW67LL/3K0JH/iAEuRX7f/QBL YgirGUP8IES1nziqZjpwHJhKBOBOMqZYDZedmObYXo0gZMQ1jxSkiKIWFHqF8q0ZUq YP6bn0g/QN6KA== To: ~metalune/simplytranslate-devel@lists.sr.ht Cc: fattalion Subject: [PATCH web] Fix translation API endpoint Date: Sun, 3 Apr 2022 17:35:06 +0300 Message-Id: <20220403143506.14658-1-fattalion@disroot.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit We forgot to update it to account for the changes in simplytranslate_engines. Also formatted the file with Black and removed outdated commented-out code. --- main.py | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index 4787a70..b524919 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,7 @@ from simplytranslate_engines.utils import * import requests + def read_config(): config.read(config_paths) @@ -60,7 +61,11 @@ def read_config(): config = ConfigParser() engines = [] -config_paths = ["config.conf", "/etc/simplytranslate/shared.conf", "/etc/simplytranslate/web.conf"] +config_paths = [ + "config.conf", + "/etc/simplytranslate/shared.conf", + "/etc/simplytranslate/web.conf", +] # This ain't clean, but it works. if __name__ != "__main__": @@ -105,7 +110,7 @@ def dict_to_prefs(d, **kwargs): async def translate(from_language, to_language, input_text): return engines[0].translate( input_text, from_language=from_language, to_language=to_language - ) + )["translated-text"] @app.route("/api/translate/", methods=["GET", "POST"]) @@ -131,36 +136,9 @@ async def api_translate(): from_language = to_lang_code(from_language, engine) to_language = to_lang_code(to_language, engine) - return engine.translate(text, from_language=from_language, to_language=to_language) - - -# @app.route("/api/translate_advanced/", methods=["GET", "POST"]) -# async def api_translate_advanced(): -# if request.method == "POST": -# args = await request.form -# elif request.method == "GET": -# args = request.args - -# engine_name = args.get("engine") - -# text = args.get("text") -# from_language = args.get("from") -# to_language = args.get("to") - -# if from_language == None: -# from_language = "auto" - -# if to_language == None: -# to_language = "en" - -# engine = get_engine(engine_name, engines, engines[0]) - -# from_language = to_lang_code(from_language, engine) -# to_language = to_lang_code(to_language, engine) - -# if engine_name != "google": -# return "" -# return engine.translate(text, from_language=from_language, to_language=to_language) + return engine.translate(text, from_language=from_language, to_language=to_language)[ + "translated-text" + ] @app.route("/prefs", methods=["POST", "GET"]) @@ -407,7 +385,11 @@ if __name__ == "__main__": if os.path.isfile(args.config): config_paths = [args.config] else: - print("INFO: Ignoring specified config file path '" + str(args.config) + "' because the file doesn't exist.") + print( + "INFO: Ignoring specified config file path '" + + str(args.config) + + "' because the file doesn't exist." + ) read_config() -- 2.35.1