From 80e56af55e23af177e9f7ee1dd67995ff2cb57df Mon Sep 17 00:00:00 2001 From: CLannadZSY Date: Mon, 15 Jun 2020 21:39:52 +0800 Subject: [PATCH 1/5] =?UTF-8?q?1.=20=E5=8E=BB=E9=99=A4=E5=A4=9A=E4=B8=AAif?= =?UTF-8?q?=E5=88=A4=E6=96=AD,=20=E6=94=B9=E7=94=A8=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=B9=E5=BC=8F,=20=E4=BD=BF=E7=94=A8if?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=88=86=E6=B5=81=E7=BB=9F=E4=B8=80=E8=B0=83?= =?UTF-8?q?=E7=94=A8=202.=20=E4=BF=AE=E6=94=B9=E5=AF=B9=E5=BA=94=E7=9A=84a?= =?UTF-8?q?pi=E6=8E=A5=E5=8F=A3,=20=E6=96=B9=E4=BE=BF=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Google.py | 5 +- Tencent.py | 5 +- baidufanyi.py | 2 +- translate.py | 199 ++++++++++++++++++-------------------------------- 4 files changed, 79 insertions(+), 132 deletions(-) diff --git a/Google.py b/Google.py index 2ef443cb..084b2419 100644 --- a/Google.py +++ b/Google.py @@ -9,11 +9,12 @@ class GoogleTranslate(): - def __init__(self): + def __init__(self, text): self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'} self.session = Session() self.session.keep_alive = False + self.text = text def getTk(self, text): @@ -71,7 +72,7 @@ def getHtml(self, session, url, headers): return None - def translate(self, text): + def translate(self): tk = self.getTk(text) url = self.buildUrl(text, tk) diff --git a/Tencent.py b/Tencent.py index 06794a89..88e1dafa 100644 --- a/Tencent.py +++ b/Tencent.py @@ -56,7 +56,7 @@ def getHtml(url,headers,data): class TencentTrans(object): - def __init__(self): + def __init__(self, text): self.api_url = 'https://fanyi.qq.com/api/translate' self.headers = { @@ -83,9 +83,10 @@ def __init__(self): self.headers['Cookie'] = self.headers['Cookie'].replace( 'wfMmjh3k/7Sr2xVNg/LtITgPRlnvGWBzP9a4FN0dn9PE7L5jDYiYJnW03MJLRUGHEFNCRhTfrp/V+wUj0dun1KkKNUUmS86A/wGVf6ydzhwboelTOs0hfHuF0ndtSoX+N3486tUMlm62VU4i856mqw==', self.qtk) + self.text = text - def get_trans_result(self, text): + def translate(self, ): data = { 'source': self.fromlang, diff --git a/baidufanyi.py b/baidufanyi.py index cc8e68ce..37b1e3f0 100644 --- a/baidufanyi.py +++ b/baidufanyi.py @@ -74,7 +74,7 @@ def parse_url(self,post_data): self.result = trans_json["trans_result"]["data"][0]["dst"] - def run(self): + def translate(self): try: """实现逻辑""" diff --git a/translate.py b/translate.py index 3abb09e5..897ca096 100644 --- a/translate.py +++ b/translate.py @@ -48,6 +48,54 @@ def compare_image(imageA,imageB): return score +def __call_entry(translate_name: str, translate_data) -> str: + """ + 调用入口 + :parem: translate_name 翻译名称 + :parem: translate_data + :return: + """ + __api_tradition = { + 'youdaoUse': youdao, + 'caiyunUse': caiyun, + 'jinshanUse': jinshan, + 'yeekitUse': yeekit, + 'alapiUse': ALAPI, + } + + __api_web = { + 'baiduwebUse': BaiduWeb, + 'tencentwebUse': TencentTrans, + 'googleUse': GoogleTranslate, + 'BingUse': BingTranslate, + } + + __api_private = { + 'baiduUse': baidu, + 'tencentUse': tencent, + 'caiyunPrivateUse': caiyunAPI, + } + ret = '' + # TODO 有待合并成统一的调用方式, 需要调整对应的 api 的结构 + if translate_name in __api_tradition: + + ret = __api_tradition[translate_name](translate_data['original']) + + if translate_name == 'yeekitUse': + ret = __api_tradition[translate_name](translate_data['original'], translate_data['yeekitLanguage']) + + elif translate_name in __api_web: + ret = __api_web[translate_name](translate_data['original']).translate() + + if translate_name == 'BingUse': + ret = __api_web[translate_name]().translate(translate_data["BingLanguage"], translate_data['original']) + + elif translate_name in __api_private: + ret = __api_private[translate_name](translate_data['original'], translate_data) + + return ret + + # 翻译主函数 def translate(window, data): @@ -63,154 +111,51 @@ def translate(window, data): score = compare_image(imageA, imageB) except Exception: score = 0.97 - + + result = dict() + translate_platform = { + 'youdaoUse': 'youdao', + 'caiyunUse': 'caiyun', + 'jinshanUse': 'jinshan', + 'yeekitUse': 'yeekit', + 'alapiUse': 'alapi', + 'baiduwebUse': 'baiduweb', + 'tencentwebUse': 'tencentweb', + 'googleUse': 'google', + 'BingUse': 'Bing', + 'baiduUse': 'baidu', + 'tencentUse': 'tencent', + 'caiyunPrivateUse': 'caiyunPrivate', + } + if score < 0.98: sign, original = baidu_orc(data) - + if sign and original and (original != window.original): - + # 过滤不需要加入翻译的字符 try: with open(".\\config\\filter.txt") as file: char = file.read() char.split('''&''') for ch in char: - original = original.replace(ch,'') + original = original.replace(ch, '') except Exception: print_exc() # 是否复制到剪贴板 if data["showClipboard"] == 'True': copy(original) - - youdaoUse = data["youdaoUse"] # 有道 - caiyunUse = data["caiyunUse"] # 公共彩云 - jinshanUse = data["jinshanUse"] # 金山 - yeekitUse = data["yeekitUse"] #yeekit - alapiUse = data["alapiUse"] # alapi - - baiduwebUse = data["baiduwebUse"] # 百度网页版 - tencentwebUse = data["tencentwebUse"] # 腾讯网页版 - googleUse = data["googleUse"] # google网页版 - BingUse = data["BingUse"] # Bing网页版 - - baiduUse = data["baiduUse"] # 百度私人版 - tencentUse = data["tencentUse"] # 腾讯私人版 - caiyunPrivateUse = data["caiyunPrivateUse"] # 私人彩云 - - yeekitLanguage = data["yeekitLanguage"] # yeekit翻译语种 - BingLanguage = data["BingLanguage"] # Bing翻译语种 - - # 有道 - if youdaoUse == "True": - result_youdao = youdao(original) - else: - result_youdao = '' - # 公共彩云 - if caiyunUse == "True": - result_caiyun = caiyun(original) - else: - result_caiyun = '' - # 金山 - if jinshanUse == "True": - result_jinshan = jinshan(original) - else: - result_jinshan = '' - #yeekit - if yeekitUse == "True": - result_yeekit = yeekit(original, yeekitLanguage) - else: - result_yeekit = '' - # alapi - if alapiUse == "True": - result_alapi = ALAPI(original) - else: - result_alapi = '' - # 百度网页版 - if baiduwebUse == "True": - baiduweb = BaiduWeb(original) - result_baiduweb = baiduweb.run() - else: - result_baiduweb = '' - # 腾讯网页版 - if tencentwebUse == "True": - Tencent = TencentTrans() - result_tencentweb = Tencent.get_trans_result(original) - else: - result_tencentweb = '' - # 谷歌网页版 - if googleUse == "True": - google = GoogleTranslate() - result_google = google.translate(original) - else: - result_google = '' - # Bing网页版 - if BingUse == "True": - bing = BingTranslate() - result_Bing = bing.translate(BingLanguage, original) - else: - result_Bing = '' - # 百度私人版 - if baiduUse == "True": - result_baidu = baidu(original, data) - else: - result_baidu = '' - # 腾讯私人版 - if tencentUse == "True": - result_tencent = tencent(original, data) - else: - result_tencent = '' - # 彩云私人版 - if caiyunPrivateUse == "True": - result_caiyunPrivate = caiyunAPI(original, data) - else: - result_caiyunPrivate = '' - - else: - result_youdao = '' - result_caiyun = '' - result_jinshan = '' - result_yeekit = '' - result_alapi = '' - result_baiduweb = '' - result_tencentweb = '' - result_google = '' - result_Bing = '' - result_baidu = '' - result_tencent = '' - result_caiyunPrivate = '' + + data['original'] = original + result = {y: __call_entry(x, data) if data[x] == 'True' else '' for x, y in translate_platform.items()} else: - result_youdao = '' - result_caiyun = '' - result_jinshan = '' - result_yeekit = '' - result_alapi = '' - result_baiduweb = '' - result_tencentweb = '' - result_google = '' - result_Bing = '' - result_baidu = '' - result_tencent = '' - result_caiyunPrivate = '' original = '' sign = True - result = dict() - result["youdao"] = result_youdao - result["caiyun"] = result_caiyun - result["jinshan"] = result_jinshan - result["yeekit"] = result_yeekit - result["alapi"] = result_alapi - result["baiduweb"] = result_baiduweb - result["tencentweb"] = result_tencentweb - result["google"] = result_google - result["Bing"] = result_Bing - result["baidu"] = result_baidu - result["tencent"] = result_tencent - result["caiyunPrivate"] = result_caiyunPrivate result["original"] = original result["sign"] = sign - return result \ No newline at end of file + return result From e969f43ce6bcc76e5b0671c185026e80dc8381fe Mon Sep 17 00:00:00 2001 From: CLannadZSY Date: Wed, 24 Jun 2020 22:14:45 +0800 Subject: [PATCH 2/5] =?UTF-8?q?bing=20=E7=88=AC=E8=99=AB=E7=9A=84=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=82=E6=95=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bing.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Bing.py b/Bing.py index 62b743c4..7ad8a9b4 100644 --- a/Bing.py +++ b/Bing.py @@ -23,6 +23,16 @@ def translate(self, BingLanguage, content): data['oncomplete'] = 'onComplete_3' data['onerror'] = 'onError_3' data['_'] = '1430745999189' + + data = { + 'from': f'"{BingLanguage}"', + 'to': '"zh"', + 'texts': f'"[{content}]"', + 'options': '{}', + 'oncomplete': 'onComplete_3', + 'onerror': 'onError_3', + '_': '1430745999189', + } try: data = urllib.parse.urlencode(data).encode('utf-8') From 8e45eafaad8e01958ed409ca477d7ca91c2bea70 Mon Sep 17 00:00:00 2001 From: CLannadZSY Date: Wed, 24 Jun 2020 22:15:35 +0800 Subject: [PATCH 3/5] =?UTF-8?q?bing=20=E7=88=AC=E8=99=AB=E7=9A=84=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=82=E6=95=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bing.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Bing.py b/Bing.py index 7ad8a9b4..26e5d667 100644 --- a/Bing.py +++ b/Bing.py @@ -13,16 +13,16 @@ def __init__(self): def translate(self, BingLanguage, content): - data = {} - data['from'] = '"' + BingLanguage + '"' - data['to'] = '"' + 'zh' + '"' - data['texts'] = '["' - data['texts'] += content - data['texts'] += '"]' - data['options'] = "{}" - data['oncomplete'] = 'onComplete_3' - data['onerror'] = 'onError_3' - data['_'] = '1430745999189' + # data = {} + # data['from'] = '"' + BingLanguage + '"' + # data['to'] = '"' + 'zh' + '"' + # data['texts'] = '["' + # data['texts'] += content + # data['texts'] += '"]' + # data['options'] = "{}" + # data['oncomplete'] = 'onComplete_3' + # data['onerror'] = 'onError_3' + # data['_'] = '1430745999189' data = { 'from': f'"{BingLanguage}"', From 513fc27f2f47ab039eeee40c217cee57ae0584c9 Mon Sep 17 00:00:00 2001 From: CLannadZSY Date: Wed, 24 Jun 2020 22:16:07 +0800 Subject: [PATCH 4/5] =?UTF-8?q?google=20=E7=88=AC=E8=99=AB=E7=9A=84?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Google.py | 72 ++++++++++++++++++++++++------------------------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/Google.py b/Google.py index 084b2419..154f7bc6 100644 --- a/Google.py +++ b/Google.py @@ -11,59 +11,51 @@ class GoogleTranslate(): def __init__(self, text): - self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'} + self.headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'} self.session = Session() self.session.keep_alive = False self.text = text - def getTk(self, text): - + with open('.\\config\\GoogleJS.js', encoding='utf8') as f: js_data = f.read() context = EvalJs() context.execute(js_data) tk = context.TL(text) - return tk + def buildUrl(self, text, tk): - def buildUrl(self, text ,tk): - baseUrl = 'http://translate.google.cn/translate_a/single' - baseUrl += '?client=webapp&' - baseUrl += 'sl=auto&' - baseUrl += 'tl=' + 'zh-CN' + '&' - baseUrl += 'hl=zh-CN&' - baseUrl += 'dt=at&' - baseUrl += 'dt=bd&' - baseUrl += 'dt=ex&' - baseUrl += 'dt=ld&' - baseUrl += 'dt=md&' - baseUrl += 'dt=qca&' - baseUrl += 'dt=rw&' - baseUrl += 'dt=rm&' - baseUrl += 'dt=ss&' - baseUrl += 'dt=t&' - baseUrl += 'ie=UTF-8&' - baseUrl += 'oe=UTF-8&' - baseUrl += 'clearbtn=1&' - baseUrl += 'otf=1&' - baseUrl += 'pc=1&' - baseUrl += 'srcrom=0&' - baseUrl += 'ssel=0&' - baseUrl += 'tsel=0&' - baseUrl += 'kc=2&' - baseUrl += 'tk=' + str(tk) + '&' + base_data = { + 'client': 'webapp', + 'sl': 'auto', + 'tl': 'zh-CN', + 'hl': 'zh-CN', + 'dt': ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'], + 'ie': 'UTF-8', + 'oe': 'UTF-8', + 'clearbtn': '1', + 'otf': '1', + 'pc': '1', + 'srcrom': '0', + 'ssel': '0', + 'tsel': '0', + 'kc': '2', + 'tk': f'{tk}', + } + + baseUrl = f'{baseUrl}?{"&".join([f"{x}={y}" if isinstance(y, str) else "&".join(map(lambda m : f"{x}={m}", y)) for x, y in base_data.items()])}' content = urllib.parse.quote(text) baseUrl += 'q=' + content - - return baseUrl + return baseUrl def getHtml(self, session, url, headers): - + try: html = session.get(url, headers=headers) return html.json() @@ -71,15 +63,14 @@ def getHtml(self, session, url, headers): print_exc() return None - def translate(self): - + tk = self.getTk(text) url = self.buildUrl(text, tk) - + try: result = self.getHtml(self.session, url, self.headers) - + if result != None: sentence = '' for i in result[0]: @@ -87,7 +78,7 @@ def translate(self): sentence += i[0] else: sentence = "谷歌:我抽风啦!" - + except Exception: print_exc() sentence = "谷歌:我抽风啦!" @@ -96,7 +87,6 @@ def translate(self): if __name__ == '__main__': - text = "そうすると、可笑しいことや変なこと、滑稽なことや正しくないこと、反対にやるべきことが见えてくるから。とにかく、何かにどっぷりはまっていると、周りのことが见えなくなってしまう。だから、时々一歩引くと物事が见えてくる。" - google = GoogleTranslate() - print(google.translate(text)) \ No newline at end of file + google = GoogleTranslate(text) + print(google.translate()) From 7874f16ba81e5201a81865141ea8ac3c2541598e Mon Sep 17 00:00:00 2001 From: CLannadZSY Date: Wed, 4 Nov 2020 20:36:49 +0800 Subject: [PATCH 5/5] =?UTF-8?q?&=E8=BF=9E=E6=8E=A5=E7=AC=A6=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Google.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Google.py b/Google.py index 154f7bc6..8b5187e5 100644 --- a/Google.py +++ b/Google.py @@ -50,7 +50,7 @@ def buildUrl(self, text, tk): baseUrl = f'{baseUrl}?{"&".join([f"{x}={y}" if isinstance(y, str) else "&".join(map(lambda m : f"{x}={m}", y)) for x, y in base_data.items()])}' content = urllib.parse.quote(text) - baseUrl += 'q=' + content + baseUrl += '&q=' + content return baseUrl