From cca6ed937e4963911de8d70f457c7c0e3f78d3ed Mon Sep 17 00:00:00 2001 From: PikachuLiker Date: Sun, 10 Jan 2021 22:44:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=E7=99=BE=E5=BA=A6api=E7=9A=84=E6=9C=BA=E5=99=A8=E7=BF=BB?= =?UTF-8?q?=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ADC_function.py | 52 ++++++++++++++++++++++++++++++++++++++++++++----- config.ini | 9 +++++++++ config.py | 19 ++++++++++++++++-- core.py | 16 +++++++++++++-- 4 files changed, 87 insertions(+), 9 deletions(-) diff --git a/ADC_function.py b/ADC_function.py index e65ae32..2662a63 100755 --- a/ADC_function.py +++ b/ADC_function.py @@ -1,4 +1,7 @@ import requests +import hashlib +import random +import time from lxml import etree import re import config @@ -458,13 +461,52 @@ def translateTag_to_sc(tag): else: return tag -def translate(src:str,target_language:str="zh_cn"): - url = "https://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=" + target_language + "&q=" + src - result = get_html(url=url,return_type="object") +def translate( + src: str, + target_language: str = "zh_cn", + engine: str = "google-free", + app_id: str = "", + key: str = "", + delay: int = 0, +): + trans_result = "" + if engine == "google-free": + url = ( + "https://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=" + + target_language + + "&q=" + + src + ) + result = get_html(url=url, return_type="object") - translate_list = [i["trans"] for i in result.json()["sentences"]] + translate_list = [i["trans"] for i in result.json()["sentences"]] + trans_result = trans_result.join(translate_list) + elif engine == "baidu": + url = "https://fanyi-api.baidu.com/api/trans/vip/translate" + salt = random.randint(1, 1435660288) + sign = app_id + src + str(salt) + key + sign = hashlib.md5(sign.encode()).hexdigest() + url += ( + "?appid=" + + app_id + + "&q=" + + src + + "&from=auto&to=" + + target_language + + "&salt=" + + str(salt) + + "&sign=" + + sign + ) + result = get_html(url=url, return_type="object") - return "".join(translate_list) + translate_list = [i["dst"] for i in result.json()["trans_result"]] + trans_result = trans_result.join(translate_list) + else: + raise ValueError("Non-existent translation engine") + + time.sleep(delay) + return trans_result # ========================================================================是否为无码 def is_uncensored(number): diff --git a/config.ini b/config.ini index ceb30a9..afa5f5f 100644 --- a/config.ini +++ b/config.ini @@ -33,8 +33,17 @@ folders=failed,JAV_output [debug_mode] switch=0 +; 机器翻译 [transalte] switch=0 +;可选项 google-free,baidu +engine=google-free +; 百度翻译app id +appid= +; 百度翻译密钥 +key= +; 翻译延迟,用于满足百度不同定价api的QPS要求,单位秒,http://api.fanyi.baidu.com/product/112 +delay=1 values=title,outline ; 预告片 diff --git a/config.py b/config.py index b798ab6..79da1c8 100644 --- a/config.py +++ b/config.py @@ -73,8 +73,15 @@ class Config: return extrafanart_download except ValueError: self._exit("extrafanart_folder") - - def transalte_values(self) -> bool: + def get_transalte_engine(self) -> str: + return self.conf.get("transalte","engine") + def get_transalte_appId(self) ->str: + return self.conf.get("transalte","appid") + def get_transalte_key(self) -> str: + return self.conf.get("transalte","key") + def get_transalte_delay(self) -> int: + return self.conf.getint("transalte","delay") + def transalte_values(self) -> str: return self.conf.get("transalte", "values") def proxy(self) -> [str, int, int, str]: try: @@ -180,6 +187,10 @@ class Config: sec8 = "transalte" conf.add_section(sec8) conf.set(sec8, "switch", "0") + conf.set(sec8, "engine", "google-free") + conf.set(sec8, "appid", "") + conf.set(sec8, "key", "") + conf.set(sec8, "delay", "1") conf.set(sec8, "values", "title,outline") sec9 = "trailer" @@ -225,4 +236,8 @@ if __name__ == "__main__": print(config.escape_folder()) print(config.debug()) print(config.is_transalte()) + print(config.get_transalte_engine()) + print(config.get_transalte_appId()) + print(config.get_transalte_key()) + print(config.get_transalte_delay()) print(config.transalte_values()) diff --git a/core.py b/core.py index 1372c33..a4f4c9f 100755 --- a/core.py +++ b/core.py @@ -229,8 +229,20 @@ def get_data_from_json(file_number, filepath, conf: config.Config): # 从JSON if conf.is_transalte(): translate_values = conf.transalte_values().split(",") for translate_value in translate_values: - json_data[translate_value] = translate(json_data[translate_value]) - + if json_data[translate_value] == "": + continue + if conf.get_transalte_engine() == "baidu": + json_data[translate_value] = translate( + json_data[translate_value], + target_language="zh", + engine=conf.get_transalte_engine(), + app_id=conf.get_transalte_appId(), + key=conf.get_transalte_key(), + delay=conf.get_transalte_delay(), + ) + else: + json_data[translate_value] = translate(json_data[translate_value]) + if conf.is_trailer(): if trailer: json_data['trailer'] = trailer From e40623ce1a355f649a2fbf92f899dd0f56aa46a4 Mon Sep 17 00:00:00 2001 From: PikachuLiker Date: Mon, 11 Jan 2021 15:28:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9F=BA=E4=BA=8Eazure?= =?UTF-8?q?=20api=E7=9A=84=E6=9C=BA=E5=99=A8=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ADC_function.py | 25 ++++++++++++++++++++++--- config.ini | 4 ++-- core.py | 7 +++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ADC_function.py b/ADC_function.py index 2662a63..a37673f 100755 --- a/ADC_function.py +++ b/ADC_function.py @@ -1,6 +1,8 @@ import requests import hashlib import random +import uuid +import json import time from lxml import etree import re @@ -73,16 +75,20 @@ def get_html(url, cookies: dict = None, ua: str = None, return_type: str = None) print('[-]Connect Failed! Please check your Proxy or Network!') -def post_html(url: str, query: dict) -> requests.Response: +def post_html(url: str, query: dict, headers: dict = None) -> requests.Response: switch, proxy, timeout, retry_count, proxytype = config.Config().proxy() proxies = get_proxy(proxy, proxytype) - headers = { + headers_ua = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36"} + if headers is None: + headers = headers_ua + else: + headers.update(headers_ua) for i in range(retry_count): try: if switch == 1 or switch == '1': - result = requests.post(url, data=query, proxies=proxies,headers=headers, timeout=timeout) + result = requests.post(url, data=query, proxies=proxies, headers=headers, timeout=timeout) else: result = requests.post(url, data=query, headers=headers, timeout=timeout) return result @@ -502,6 +508,19 @@ def translate( translate_list = [i["dst"] for i in result.json()["trans_result"]] trans_result = trans_result.join(translate_list) + elif engine == "azure": + url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=" + target_language + headers = { + 'Ocp-Apim-Subscription-Key': key, + 'Ocp-Apim-Subscription-Region': "global", + 'Content-type': 'application/json', + 'X-ClientTraceId': str(uuid.uuid4()) + } + body = json.dumps([{'text': src}]) + result = post_html(url=url,query=body,headers=headers) + translate_list = [i["text"] for i in result.json()[0]["translations"]] + trans_result = trans_result.join(translate_list) + else: raise ValueError("Non-existent translation engine") diff --git a/config.ini b/config.ini index afa5f5f..a8f1ad1 100644 --- a/config.ini +++ b/config.ini @@ -36,11 +36,11 @@ switch=0 ; 机器翻译 [transalte] switch=0 -;可选项 google-free,baidu +;可选项 google-free,baidu,azure engine=google-free ; 百度翻译app id appid= -; 百度翻译密钥 +; 百度翻译或azure翻译密钥 key= ; 翻译延迟,用于满足百度不同定价api的QPS要求,单位秒,http://api.fanyi.baidu.com/product/112 delay=1 diff --git a/core.py b/core.py index a4f4c9f..c985f91 100755 --- a/core.py +++ b/core.py @@ -240,6 +240,13 @@ def get_data_from_json(file_number, filepath, conf: config.Config): # 从JSON key=conf.get_transalte_key(), delay=conf.get_transalte_delay(), ) + elif conf.get_transalte_engine() == "azure": + json_data[translate_value] = translate( + json_data[translate_value], + target_language="zh-Hans", + engine=conf.get_transalte_engine(), + key=conf.get_transalte_key(), + ) else: json_data[translate_value] = translate(json_data[translate_value])