From 419a5a6773ad4e5659c74997ef9bcab18d2f5ba7 Mon Sep 17 00:00:00 2001 From: Antares Date: Mon, 24 Feb 2020 22:01:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99read=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/read/__init__.py | 0 plugins/read/config_default.py | 12 ------ plugins/read/read.py | 40 ------------------ plugins_new/read/plugin.py | 77 ++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 52 deletions(-) delete mode 100644 plugins/read/__init__.py delete mode 100644 plugins/read/config_default.py delete mode 100644 plugins/read/read.py create mode 100644 plugins_new/read/plugin.py diff --git a/plugins/read/__init__.py b/plugins/read/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/plugins/read/config_default.py b/plugins/read/config_default.py deleted file mode 100644 index 1103345..0000000 --- a/plugins/read/config_default.py +++ /dev/null @@ -1,12 +0,0 @@ -MAX_STRING_LENGTH = 300 - -# Baidu AI Apps -APP_ID = "" -API_KEY = "" -SECRET_KEY = "" - -# Volume 1(low)-10(high) -VOLUME = 8 -# Speech rate 1(slow)-10(quick) -SPEED = 4 - diff --git a/plugins/read/read.py b/plugins/read/read.py deleted file mode 100644 index b0f9144..0000000 --- a/plugins/read/read.py +++ /dev/null @@ -1,40 +0,0 @@ -from register import command -from global_vars import CONFIG -import base64 -import tempfile -import os -from aip import AipSpeech -import threading -config = CONFIG[__name__] - - -def plugin(): - return { - "author": "Antares", - "version": 1.0, - "description": "文字朗读" - } - - -@command(name="read", help="文字转语音") -def read(bot, context, args): - string = " ".join(args[1:]) - if len(string) > config.MAX_STRING_LENGTH: - bot.send(context, "字符串过长") - return - - def handle(): - client = AipSpeech(config.APP_ID, config.API_KEY, config.SECRET_KEY) - voice = client.synthesis(string, 'zh', 1, { - 'vol': config.VOLUME, - 'per': 4, - 'spd': config.SPEED - }) - - if not isinstance(voice, dict): - result = str(base64.encodebytes(voice).decode().replace("\n", "")) - bot.send(context, "[CQ:record,file=base64://{}]".format(result)) - else: - bot.send(context, "转换语音失败,请检查是否含有非法字符") - - threading.Thread(target=handle).start() diff --git a/plugins_new/read/plugin.py b/plugins_new/read/plugin.py new file mode 100644 index 0000000..fe407ed --- /dev/null +++ b/plugins_new/read/plugin.py @@ -0,0 +1,77 @@ +from common.plugin import Plugin +from common.config_loader import ConfigBase +from common.datatypes import PluginMeta +from common.countdown_bot import CountdownBot +from common.loop import TimeTuple +from common.command import ChatType +from common.event import GroupMessageEvent +from typing import List +import base64 +from aip import AipSpeech + + +class ReadConfig(ConfigBase): + MAX_STRING_LENGTH = 300 + + # Baidu AI Apps + APP_ID = "" + API_KEY = "" + SECRET_KEY = "" + + # Volume 1(low)-10(high) + VOLUME = 8 + # Speech rate 1(slow)-10(quick) + SPEED = 4 + + +class ReadPlugin(Plugin): + def get_voice(self, text: str) -> str: + result = self.client.synthesis(text, 'zh', 1, { + 'vol': self.config.VOLUME, + 'per': 4, + 'spd': self.config.SPEED + }) + if isinstance(result, dict): + return "" + else: + return base64.encodebytes(result).decode().replace("\n", "") + + def command_read(self, plugin, args: List[str], raw_string: str, context, evt: GroupMessageEvent): + def wrapper(): + text = " ".join(args[1:]) + if len(text) > self.config.MAX_STRING_LENGTH: + self.bot.send(context, "字符串过长") + else: + b64voice = self.get_voice(text) + if b64voice: + self.bot.send( + context, f"[CQ:record,file=base64://{b64voice}]") + else: + self.bot.send(context, "生成错误,请检查是否含有非法字符") + self.bot.submit_multithread_task(wrapper) + + def on_enable(self): + self.client = AipSpeech( + self.config.APP_ID, self.config.API_KEY, self.config.SECRET_KEY) + self.config: ReadConfig + self.bot: CountdownBot + self.register_command_wrapped( + command_name="read", + command_handler=self.command_read, + help_string="文字转语音", + chats={ChatType.discuss, ChatType.group, ChatType.private}, + ) + + +def get_plugin_class(): + return ReadPlugin + + +def get_config_class(): + return ReadConfig + + +def get_plugin_meta(): + return PluginMeta( + "read", 2.0, "文字转语音" + ) -- Gitee