zhenxun_bot/plugins/translate/data_source.py

62 lines
1.9 KiB
Python
Raw Normal View History

2021-05-20 19:25:51 +08:00
import aiohttp
2021-06-30 19:50:55 +08:00
from utils.utils import get_local_proxy
from utils.user_agent import get_user_agent
2021-05-20 19:25:51 +08:00
2021-07-30 21:21:51 +08:00
url = f"http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
2021-05-20 19:25:51 +08:00
async def translate_msg(language_type, msg):
data = {
2021-07-30 21:21:51 +08:00
"type": parse_language(language_type),
"i": msg,
2021-05-20 19:25:51 +08:00
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"ue": "UTF-8",
"action": "FY_BY_CLICKBUTTON",
2021-07-30 21:21:51 +08:00
"typoResult": "true",
2021-05-20 19:25:51 +08:00
}
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
async with session.post(url, data=data, proxy=get_local_proxy()) as res:
data = await res.json()
2021-07-30 21:21:51 +08:00
if data["errorCode"] == 0:
return data["translateResult"][0][0]["tgt"]
return "翻译惜败.."
2021-05-20 19:25:51 +08:00
# ZH_CN2EN 中文 » 英语
# ZH_CN2JA 中文 » 日语
# ZH_CN2KR 中文 » 韩语
# ZH_CN2FR 中文 » 法语
# ZH_CN2RU 中文 » 俄语
# ZH_CN2SP 中文 » 西语
# EN2ZH_CN 英语 » 中文
# JA2ZH_CN 日语 » 中文
# KR2ZH_CN 韩语 » 中文
# FR2ZH_CN 法语 » 中文
# RU2ZH_CN 俄语 » 中文
# SP2ZH_CN 西语 » 中文
def parse_language(language_type):
2021-07-30 21:21:51 +08:00
if language_type == "英翻":
return "EN2ZH_CN"
if language_type == "日翻":
return "JA2ZH_CN"
if language_type == "韩翻":
return "KR2ZH_CN"
2021-05-20 19:25:51 +08:00
# if language_type == '法翻':
# return 'FR2ZH_CN'
# if language_type == '俄翻':
# return 'RU2ZH_CN'
2021-07-30 21:21:51 +08:00
if language_type == "翻英":
return "ZH_CN2EN"
if language_type == "翻日":
return "ZH_CN2JA"
if language_type == "翻韩":
return "ZH_CN2KR"
2021-05-20 19:25:51 +08:00
# if language_type == '翻法':
# return 'ZH_CN2FR'
# if language_type == '翻俄':
# return 'ZH_CN2RU'