From 4c5e8850aa138b32b0c51fed2d1caffcd123d63a Mon Sep 17 00:00:00 2001 From: lanvent Date: Fri, 9 Jun 2023 14:46:43 +0800 Subject: [PATCH] fix: env vars type error (#1127) --- translate/baidu/baidu_translate.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/translate/baidu/baidu_translate.py b/translate/baidu/baidu_translate.py index a871766..6f99e34 100644 --- a/translate/baidu/baidu_translate.py +++ b/translate/baidu/baidu_translate.py @@ -17,13 +17,15 @@ class BaiduTranslator(Translator): self.url = endpoint + path self.appid = conf().get("baidu_translate_app_id") self.appkey = conf().get("baidu_translate_app_key") + if not self.appid or not self.appkey: + raise Exception("baidu translate appid or appkey not set") # For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`, need to convert to ISO 639-1 codes def translate(self, query: str, from_lang: str = "", to_lang: str = "en") -> str: if not from_lang: from_lang = "auto" # baidu suppport auto detect salt = random.randint(32768, 65536) - sign = self.make_md5(self.appid + query + str(salt) + self.appkey) + sign = self.make_md5("{}{}{}{}".format(self.appid, query, salt, self.appkey)) headers = {"Content-Type": "application/x-www-form-urlencoded"} payload = {"appid": self.appid, "q": query, "from": from_lang, "to": to_lang, "salt": salt, "sign": sign}