Merge pull request #407 from PikachuLiker/master

添加基于azure 和 百度 翻译api的机器翻译
This commit is contained in:
Yoshiko2
2021-01-12 20:45:19 +08:00
committed by GitHub
4 changed files with 116 additions and 12 deletions

View File

@@ -1,4 +1,9 @@
import requests
import hashlib
import random
import uuid
import json
import time
from lxml import etree
import re
import config
@@ -71,16 +76,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
@@ -459,13 +468,65 @@ 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)
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")
time.sleep(delay)
return trans_result
# ========================================================================是否为无码
def is_uncensored(number):