diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/Baidu.py b/Baidu.py index 89a3863..3f436a5 100644 --- a/Baidu.py +++ b/Baidu.py @@ -7,7 +7,8 @@ class Baidu(object): def __init__(self): - self.url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en' + #self.url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en' + self.url = 'https://fanyi.baidu.com/v2transapi?from={}&to={}' self.header = { 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8', 'origin': 'https://fanyi.baidu.com', @@ -106,8 +107,21 @@ def get_gtk(self): #print('gtk '+gtk) return gtk + def langdetect(self, text): + s = requests.session() + data = { 'query': text } + response = requests.post('https://fanyi.baidu.com/langdetect', headers=self.header, data=data) + res = response.json() + if res['error'] == 0: + return res['lan'] + else: + return 'en' + def get_data(self, from_lan , to_lan, text): data = {} + if from_lan == 'auto': + from_lan = self.langdetect(text) + print("detected from_lan={}".format(from_lan)) data['from'] = from_lan data['to'] = to_lan data['query'] = text @@ -120,7 +134,7 @@ def get_data(self, from_lan , to_lan, text): def translate(self, from_lan, to_lan, text): self.data = self.get_data(from_lan, to_lan, text) s = requests.session() - response = requests.post(self.url, headers=self.header, data=self.data ) + response = requests.post(self.url.format(from_lan, to_lan), headers=self.header, data=self.data ) return response.json()['trans_result']['data'][0]['dst'] diff --git a/Bing.py b/Bing.py index d939a6f..0d31518 100644 --- a/Bing.py +++ b/Bing.py @@ -7,7 +7,8 @@ def __init__(self): def translate(self, from_lan, to_lan,content,): data = {} - data['from'] = '"' + from_lan + '"' + if from_lan != 'auto': + data['from'] = '"' + from_lan + '"' data['to'] = '"' + to_lan + '"' data['texts'] = '["' data['texts'] += content @@ -20,6 +21,9 @@ def translate(self, from_lan, to_lan,content,): strUrl = self.url + data.decode() + "&appId=%223DAEE5B978BA031557E739EE1E2A68CB1FAD5909%22" response = urllib.request.urlopen(strUrl) str_data = response.read().decode('utf-8') + tmp, from_lang = str_data.split('"From":') + from_lang = from_lang[1:from_lang.find('"', 1)] + print("detected from_lang={}".format(from_lang)) tmp, str_data = str_data.split('"TranslatedText":') translate_data = str_data[1:str_data.find('"', 1)] return translate_data diff --git a/Tencent.py b/Tencent.py index 579ad67..d17fe12 100644 --- a/Tencent.py +++ b/Tencent.py @@ -16,18 +16,18 @@ def get_filter(text): return text def get_qtv_qtk(): - api_url = 'https://fanyi.qq.com/' + api_url = 'https://fanyi.qq.com/api/reauth12f' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, ' 'like Gecko) Chrome/73.0.3683.86 Safari/537.36', } - res = requests.get(api_url, headers=headers) + res = requests.post(api_url, headers=headers) data = res.text fy_guid = res.cookies.get('fy_guid') - reg = re.compile(r'var qtv = "(.*?)"') + reg = re.compile(r'"qtv":"(.*?)"') qtv = reg.search(data).group(1) - reg = re.compile(r'var qtk = "(.*?)"') + reg = re.compile(r'"qtk":"(.*?)"') qtk = reg.search(data).group(1) return fy_guid, qtv, qtk diff --git a/Youdao.py b/Youdao.py index 5c0a4c4..ad2b0a7 100644 --- a/Youdao.py +++ b/Youdao.py @@ -32,7 +32,7 @@ def translate(self,from_lan,to_lan,text): self.headers['Content-Length'] = str(233 + len(text)) ts = str(int(time.time() * 1000)) salf = ts + str(random.randint(0, 9)) - n = 'fanyideskweb' + text + salf + "n%A-rKaT5fb[Gy?;N5@Tj" + n = 'fanyideskweb' + text + salf + "Tbh5E8=q6U3EXe+&L[4c@" self.m = hashlib.md5() self.m.update(n.encode('utf-8')) sign = self.m.hexdigest() @@ -53,6 +53,7 @@ def translate(self,from_lan,to_lan,text): } try: result = self.s.post(self.url, headers=self.headers, data=data, timeout=5).json() + print(result) if result != None: ans = result['translateResult'][0][0]['tgt'] return ans