From e40623ce1a355f649a2fbf92f899dd0f56aa46a4 Mon Sep 17 00:00:00 2001 From: PikachuLiker Date: Mon, 11 Jan 2021 15:28:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9F=BA=E4=BA=8Eazure=20api?= =?UTF-8?q?=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])