From b23d073f26485d7b114e6589e660c5524c15272b Mon Sep 17 00:00:00 2001 From: Jason Lyu Date: Fri, 15 Dec 2023 05:16:47 +0800 Subject: [PATCH] Feature: support retry translation (#24) --- Contents/Code/translator.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Contents/Code/translator.py b/Contents/Code/translator.py index 86b48ca..c9ad50f 100644 --- a/Contents/Code/translator.py +++ b/Contents/Code/translator.py @@ -41,17 +41,30 @@ def translate_text(text, lang, fallback=None): forced_lang = params.pop('to', None) if forced_lang: - Log.Warn('Force translation language from {lang} to {forced_lang}' + Log.Info('Force setting translation language from {lang} to {forced_lang}' .format(lang=lang, forced_lang=forced_lang)) lang = forced_lang + Log.Info('Translate text to {lang}: {text}'.format(lang=lang, text=text)) + # limit translate request rate to 1 rps. time.sleep(1.0) + def translate(): + return api.translate(q=text, to=lang, engine=engine, + **params).translated_text + + return retry(func=translate, fallback=translated_text) + + +def retry(func, fallback=None, count=3): + i = 1 + while i <= count: try: - translated_text = api.translate(q=text, to=lang, engine=engine, - **params).translated_text + return func() except Exception as e: - Log.Warn('Translate error: {error}'.format(error=e)) - finally: - return translated_text + Log.Warn('Retry function {func} ({count}): {error}' + .format(func=getattr(func, '__name__', func), + count=i, error=e)) + i = i + 1 + return fallback