mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 06:12:53 +08:00
feat✨: 翻译
This commit is contained in:
parent
876bba479c
commit
126dbc39b4
91
zhenxun/plugins/translate/__init__.py
Normal file
91
zhenxun/plugins/translate/__init__.py
Normal file
@ -0,0 +1,91 @@
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot_plugin_alconna import Alconna, Args, Arparma, Match, Option, on_alconna
|
||||
from nonebot_plugin_saa import Image, Text
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.depends import CheckConfig
|
||||
from zhenxun.utils.image_utils import ImageTemplate
|
||||
|
||||
from .data_source import language, translate_message
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="翻译",
|
||||
description="出国旅游好助手",
|
||||
usage="""
|
||||
指令:
|
||||
翻译语种: (查看soruce与to可用值,代码与中文都可)
|
||||
示例:
|
||||
翻译 你好: 将中文翻译为英文
|
||||
翻译 Hello: 将英文翻译为中文
|
||||
|
||||
翻译 你好 -to 希腊语: 将"你好"翻译为希腊语
|
||||
翻译 你好: 允许form和to使用中文
|
||||
翻译 你好 -form:中文 to:日语 你好: 指定原语种并将"你好"翻译为日文
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
menu_type="一些工具",
|
||||
configs=[
|
||||
RegisterConfig(key="APPID", value=None, help="百度翻译APPID"),
|
||||
RegisterConfig(key="SECRET_KEY", value=None, help="百度翻译秘钥"),
|
||||
],
|
||||
).dict(),
|
||||
)
|
||||
|
||||
_matcher = on_alconna(
|
||||
Alconna(
|
||||
"翻译",
|
||||
Args["text", str],
|
||||
Option("-s|--source", Args["source_text", str, "auto"]),
|
||||
Option("-t|--to", Args["to_text", str, "auto"]),
|
||||
),
|
||||
priority=5,
|
||||
block=True,
|
||||
)
|
||||
|
||||
_language_matcher = on_alconna(Alconna("翻译语种"), priority=5, block=True)
|
||||
|
||||
|
||||
@_language_matcher.handle()
|
||||
async def _(session: EventSession, arparma: Arparma):
|
||||
s = ""
|
||||
column_list = ["语种", "代码"]
|
||||
data_list = []
|
||||
for key, value in language.items():
|
||||
data_list.append([key, value])
|
||||
image = await ImageTemplate.table_page("翻译语种", "", column_list, data_list)
|
||||
await Image(image.pic2bytes()).send()
|
||||
logger.info(f"查看翻译语种", arparma.header_result, session=session)
|
||||
|
||||
|
||||
@_matcher.handle(
|
||||
parameterless=[
|
||||
CheckConfig(config="APPID"),
|
||||
CheckConfig(config="SECRET_KEY"),
|
||||
]
|
||||
)
|
||||
async def _(
|
||||
session: EventSession,
|
||||
arparma: Arparma,
|
||||
text: str,
|
||||
source_text: Match[str],
|
||||
to_text: Match[str],
|
||||
):
|
||||
source = source_text.result if source_text.available else "auto"
|
||||
to = to_text.result if to_text.available else "auto"
|
||||
values = language.values()
|
||||
keys = language.keys()
|
||||
if source not in values and source not in keys:
|
||||
await Text("源语种不支持...").finish()
|
||||
if to not in values and to not in keys:
|
||||
await Text("目标语种不支持...").finish()
|
||||
result = await translate_message(text, source, to)
|
||||
await Text(result).send(reply=True)
|
||||
logger.info(
|
||||
f"source: {source}, to: {to}, 翻译: {text}",
|
||||
arparma.header_result,
|
||||
session=session,
|
||||
)
|
||||
83
zhenxun/plugins/translate/data_source.py
Normal file
83
zhenxun/plugins/translate/data_source.py
Normal file
@ -0,0 +1,83 @@
|
||||
import time
|
||||
from hashlib import md5
|
||||
|
||||
from zhenxun.configs.config import Config
|
||||
from zhenxun.utils.http_utils import AsyncHttpx
|
||||
|
||||
URL = "http://api.fanyi.baidu.com/api/trans/vip/translate"
|
||||
|
||||
|
||||
language = {
|
||||
"自动": "auto",
|
||||
"粤语": "yue",
|
||||
"韩语": "kor",
|
||||
"泰语": "th",
|
||||
"葡萄牙语": "pt",
|
||||
"希腊语": "el",
|
||||
"保加利亚语": "bul",
|
||||
"芬兰语": "fin",
|
||||
"斯洛文尼亚语": "slo",
|
||||
"繁体中文": "cht",
|
||||
"中文": "zh",
|
||||
"文言文": "wyw",
|
||||
"法语": "fra",
|
||||
"阿拉伯语": "ara",
|
||||
"德语": "de",
|
||||
"荷兰语": "nl",
|
||||
"爱沙尼亚语": "est",
|
||||
"捷克语": "cs",
|
||||
"瑞典语": "swe",
|
||||
"越南语": "vie",
|
||||
"英语": "en",
|
||||
"日语": "jp",
|
||||
"西班牙语": "spa",
|
||||
"俄语": "ru",
|
||||
"意大利语": "it",
|
||||
"波兰语": "pl",
|
||||
"丹麦语": "dan",
|
||||
"罗马尼亚语": "rom",
|
||||
"匈牙利语": "hu",
|
||||
}
|
||||
|
||||
|
||||
async def translate_message(word: str, form: str, to: str) -> str:
|
||||
"""翻译
|
||||
|
||||
参数:
|
||||
word (str): 翻译文字
|
||||
form (str): 源语言
|
||||
to (str): 目标语言
|
||||
|
||||
返回:
|
||||
str: 翻译后的文字
|
||||
"""
|
||||
if form in language:
|
||||
form = language[form]
|
||||
if to in language:
|
||||
to = language[to]
|
||||
salt = str(time.time())
|
||||
app_id = Config.get_config("translate", "APPID")
|
||||
secret_key = Config.get_config("translate", "SECRET_KEY")
|
||||
sign = app_id + word + salt + secret_key # type: ignore
|
||||
md5_ = md5()
|
||||
md5_.update(sign.encode("utf-8"))
|
||||
sign = md5_.hexdigest()
|
||||
params = {
|
||||
"q": word,
|
||||
"from": form,
|
||||
"to": to,
|
||||
"appid": app_id,
|
||||
"salt": salt,
|
||||
"sign": sign,
|
||||
}
|
||||
url = URL + "?"
|
||||
for key, value in params.items():
|
||||
url += f"{key}={value}&"
|
||||
url = url[:-1]
|
||||
resp = await AsyncHttpx.get(url)
|
||||
data = resp.json()
|
||||
if data.get("error_code"):
|
||||
return data.get("error_msg")
|
||||
if trans_result := data.get("trans_result"):
|
||||
return trans_result[0]["dst"]
|
||||
return "没有找到翻译捏..."
|
||||
Loading…
Reference in New Issue
Block a user