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}