From 08c811b709932c8402b21ff9bfd6649aabc651eb Mon Sep 17 00:00:00 2001 From: Antares Date: Wed, 4 Mar 2020 00:46:02 +0800 Subject: [PATCH 1/2] qwq --- plugins_new/weather/plugin.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins_new/weather/plugin.py b/plugins_new/weather/plugin.py index 5fa09a9..192da76 100644 --- a/plugins_new/weather/plugin.py +++ b/plugins_new/weather/plugin.py @@ -5,11 +5,11 @@ from common.countdown_bot import CountdownBot from common.loop import TimeTuple from common.command import ChatType from common.event import MessageEvent -import aiohttp -import urllib from typing import Dict, List from io import StringIO +import aiohttp + class WeatherConfig(ConfigBase): # 和风天气 https://www.heweather.com @@ -19,7 +19,7 @@ class WeatherConfig(ConfigBase): class WeatherPlugin(Plugin): async def get_data(self, local: str, type1: str, type2: str) -> dict: async with self.aioclient.get(f"https://free-api.heweather.net/s6/{type1}/{type2}", params={ - "location": urllib.parse.quote(local), + "location": local, "key": self.config.APP_KEY, "lang": "zh" }) as resp: -- Gitee From c3048d731bfd649df97334ba211cdaa986a3df2e Mon Sep 17 00:00:00 2001 From: Antares Date: Wed, 4 Mar 2020 01:14:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?aiphttp=E9=87=8D=E6=9E=84read=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins_new/read/plugin.py | 70 +++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/plugins_new/read/plugin.py b/plugins_new/read/plugin.py index 7d92b37..a9eae4e 100644 --- a/plugins_new/read/plugin.py +++ b/plugins_new/read/plugin.py @@ -6,8 +6,11 @@ from common.loop import TimeTuple from common.command import ChatType from common.event import MessageEvent from typing import List + +import urllib import base64 -from aip import AipSpeech +import aiohttp +import json class ReadConfig(ConfigBase): @@ -25,34 +28,46 @@ class ReadConfig(ConfigBase): 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 "" + async def get_voice(self, text: str, token: str) -> bytes: + async with self.aioclient.post("https://tsn.baidu.com/text2audio", + data=f"tex={urllib.parse.quote(urllib.parse.quote(text))}\ +&ctp=1&tok={token}&cuid=qwqqwqqwqqwq&spd={self.config.SPEED}\ +&per=4&vol={self.config.VOLUME}&lan=zh") as resp: + resp: aiohttp.ClientResponse + result = await resp.read() + return result + + async def get_token(self) -> str: + async with self.aioclient.get("https://openapi.baidu.com/oauth/2.0/token", params={ + "grant_type": "client_credentials", + "client_id": self.config.API_KEY, + "client_secret": self.config.SECRET_KEY + }) as resp: + resp: aiohttp.ClientResponse + result = await resp.json() + if "access_token" in result: + return result['access_token'] + else: + return "xxx" + + async def command_read(self, plugin, args: List[str], raw_string: str, context, evt: MessageEvent): + text = " ".join(args) + if len(text) > self.config.MAX_STRING_LENGTH: + await self.bot.client_async.send(context, "字符串过长") else: - return base64.encodebytes(result).decode().replace("\n", "") - - def command_read(self, plugin, args: List[str], raw_string: str, context, evt: MessageEvent): - def wrapper(): - text = " ".join(args) - 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) + token = await self.get_token() + data = await self.get_voice(text, token) + try: + info = json.loads(data.decode()) + except Exception: + b64voice = base64.encodebytes(data).decode().replace('\n', '') + await self.bot.client_async.send( + context, f"[CQ:record,file=base64://{b64voice}]") + return + await self.bot.client_async.send(context, f"Error: {info}") def on_enable(self): - self.client = AipSpeech( - self.config.APP_ID, self.config.API_KEY, self.config.SECRET_KEY) + self.aioclient = aiohttp.ClientSession() self.config: ReadConfig self.bot: CountdownBot self.register_command_wrapped( @@ -60,6 +75,7 @@ class ReadPlugin(Plugin): command_handler=self.command_read, help_string="文字转语音", chats={ChatType.discuss, ChatType.group, ChatType.private}, + is_async=True ) @@ -73,5 +89,5 @@ def get_config_class(): def get_plugin_meta(): return PluginMeta( - "Antares", 2.0, "文字转语音" + "Antares", 3.0, "文字转语音" ) -- Gitee