添加基于百度api的机器翻译
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
|
import hashlib
|
||||||
|
import random
|
||||||
|
import time
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
import re
|
import re
|
||||||
import config
|
import config
|
||||||
@@ -458,13 +461,52 @@ def translateTag_to_sc(tag):
|
|||||||
else:
|
else:
|
||||||
return tag
|
return tag
|
||||||
|
|
||||||
def translate(src:str,target_language:str="zh_cn"):
|
def translate(
|
||||||
url = "https://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl=auto&tl=" + target_language + "&q=" + src
|
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")
|
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):
|
def is_uncensored(number):
|
||||||
|
|||||||
@@ -33,8 +33,17 @@ folders=failed,JAV_output
|
|||||||
[debug_mode]
|
[debug_mode]
|
||||||
switch=0
|
switch=0
|
||||||
|
|
||||||
|
; 机器翻译
|
||||||
[transalte]
|
[transalte]
|
||||||
switch=0
|
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
|
values=title,outline
|
||||||
|
|
||||||
; 预告片
|
; 预告片
|
||||||
|
|||||||
19
config.py
19
config.py
@@ -73,8 +73,15 @@ class Config:
|
|||||||
return extrafanart_download
|
return extrafanart_download
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self._exit("extrafanart_folder")
|
self._exit("extrafanart_folder")
|
||||||
|
def get_transalte_engine(self) -> str:
|
||||||
def transalte_values(self) -> bool:
|
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")
|
return self.conf.get("transalte", "values")
|
||||||
def proxy(self) -> [str, int, int, str]:
|
def proxy(self) -> [str, int, int, str]:
|
||||||
try:
|
try:
|
||||||
@@ -180,6 +187,10 @@ class Config:
|
|||||||
sec8 = "transalte"
|
sec8 = "transalte"
|
||||||
conf.add_section(sec8)
|
conf.add_section(sec8)
|
||||||
conf.set(sec8, "switch", "0")
|
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")
|
conf.set(sec8, "values", "title,outline")
|
||||||
|
|
||||||
sec9 = "trailer"
|
sec9 = "trailer"
|
||||||
@@ -225,4 +236,8 @@ if __name__ == "__main__":
|
|||||||
print(config.escape_folder())
|
print(config.escape_folder())
|
||||||
print(config.debug())
|
print(config.debug())
|
||||||
print(config.is_transalte())
|
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())
|
print(config.transalte_values())
|
||||||
|
|||||||
12
core.py
12
core.py
@@ -229,6 +229,18 @@ def get_data_from_json(file_number, filepath, conf: config.Config): # 从JSON
|
|||||||
if conf.is_transalte():
|
if conf.is_transalte():
|
||||||
translate_values = conf.transalte_values().split(",")
|
translate_values = conf.transalte_values().split(",")
|
||||||
for translate_value in translate_values:
|
for translate_value in translate_values:
|
||||||
|
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])
|
json_data[translate_value] = translate(json_data[translate_value])
|
||||||
|
|
||||||
if conf.is_trailer():
|
if conf.is_trailer():
|
||||||
|
|||||||
Reference in New Issue
Block a user