~metalune/simplytranslate-devel

web: Fix translation API endpoint v1 REJECTED

fattalion: 1
 Fix translation API endpoint

 1 files changed, 15 insertions(+), 33 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~metalune/simplytranslate-devel/patches/30774/mbox | git am -3
Learn more about email & git

[PATCH web] Fix translation API endpoint Export this patch

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
Ignore this patch; the fact that the API now returns JSON is actually intentional, to give access to all the extra information in the dictionary.