添加基于azure api的机器翻译

This commit is contained in:
PikachuLiker
2021-01-11 15:28:12 +08:00
parent cca6ed937e
commit e40623ce1a
3 changed files with 31 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
import requests import requests
import hashlib import hashlib
import random import random
import uuid
import json
import time import time
from lxml import etree from lxml import etree
import re 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!') 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() switch, proxy, timeout, retry_count, proxytype = config.Config().proxy()
proxies = get_proxy(proxy, proxytype) 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"} "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): for i in range(retry_count):
try: try:
if switch == 1 or switch == '1': 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: else:
result = requests.post(url, data=query, headers=headers, timeout=timeout) result = requests.post(url, data=query, headers=headers, timeout=timeout)
return result return result
@@ -502,6 +508,19 @@ def translate(
translate_list = [i["dst"] for i in result.json()["trans_result"]] translate_list = [i["dst"] for i in result.json()["trans_result"]]
trans_result = trans_result.join(translate_list) 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: else:
raise ValueError("Non-existent translation engine") raise ValueError("Non-existent translation engine")

View File

@@ -36,11 +36,11 @@ switch=0
; 机器翻译 ; 机器翻译
[transalte] [transalte]
switch=0 switch=0
;可选项 google-free,baidu ;可选项 google-free,baidu,azure
engine=google-free engine=google-free
; 百度翻译app id ; 百度翻译app id
appid= appid=
; 百度翻译密钥 ; 百度翻译或azure翻译密钥
key= key=
; 翻译延迟用于满足百度不同定价api的QPS要求单位秒http://api.fanyi.baidu.com/product/112 ; 翻译延迟用于满足百度不同定价api的QPS要求单位秒http://api.fanyi.baidu.com/product/112
delay=1 delay=1

View File

@@ -240,6 +240,13 @@ def get_data_from_json(file_number, filepath, conf: config.Config): # 从JSON
key=conf.get_transalte_key(), key=conf.get_transalte_key(),
delay=conf.get_transalte_delay(), 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: else:
json_data[translate_value] = translate(json_data[translate_value]) json_data[translate_value] = translate(json_data[translate_value])