😴优化协程,减少requests使用,另外,转换音乐时管理员可用全局之资源

This commit is contained in:
EillesWan 2024-08-22 01:56:58 +08:00
parent 3294c821f4
commit 31dfcb98f7
4 changed files with 159 additions and 142 deletions

View File

@ -1,7 +1,8 @@
import zhDateTime
import requests
import random import random
import aiohttp
import zhDateTime
from src.utils import event as event_utils from src.utils import event as event_utils
from src.utils.base.language import get_user_lang, get_default_lang_code, Language from src.utils.base.language import get_user_lang, get_default_lang_code, Language
from src.utils.base.ly_typing import T_Bot, T_MessageEvent from src.utils.base.ly_typing import T_Bot, T_MessageEvent
@ -65,9 +66,7 @@ yanlun = on_alconna(
) )
yanlun_path = ( yanlun_path = "https://nd.liteyuki.icu/api/v3/share/content/Xpue?path=null"
"https://nd.liteyuki.icu/api/v3/share/content/Xpue?path=null"
)
# 每天4点更新 # 每天4点更新
@ -77,32 +76,16 @@ async def every_day_update():
nonebot.logger.success(ulang.get("yanlun.refresh.success", COUNT=update_yanlun())) nonebot.logger.success(ulang.get("yanlun.refresh.success", COUNT=update_yanlun()))
def update_yanlun(): async def update_yanlun():
global yanlun_texts, yanlun_seqs global yanlun_texts, yanlun_seqs
solar_datetime = zhDateTime.DateTime.now()
lunar_datetime = solar_datetime.to_lunar()
solar_date = (solar_datetime.month, solar_datetime.day)
lunar_date = (lunar_datetime.lunar_month, lunar_datetime.lunar_day)
if solar_date == (4, 3):
yanlun_texts = ["金羿ELS 生日快乐~", "Happy Birthday, Eilles!"]
elif solar_date == (8, 6):
yanlun_texts = ["诸葛亮与八卦阵 生日快乐~", "Happy Birthday, bgArray~!"]
elif solar_date == (8, 16):
yanlun_texts = ["鱼旧梦 生日快乐~", "Happy Birthday, ElapsingDreams~!"]
else:
try: try:
yanlun_texts = ( async with aiohttp.ClientSession() as client:
requests.get( resp = await client.get(yanlun_path)
yanlun_path, yanlun_texts = (await resp.text()).strip("\n").split("\n")
) except (ConnectionError, aiohttp.ClientError, aiohttp.WebSocketError) as E:
.text.strip("\n") nonebot.logger.warning(f"读取言·论信息发生 客户端或通道 错误:\n{E}")
.split("\n")
)
except (ConnectionError, requests.HTTPError, requests.RequestException) as E:
nonebot.logger.warning(f"读取言·论信息发生 互联网连接 错误:\n{E}")
yanlun_texts = ["以梦想为驱使 创造属于自己的未来"] yanlun_texts = ["以梦想为驱使 创造属于自己的未来"]
# noinspection PyBroadException # noinspection PyBroadException
except BaseException as E: except BaseException as E:
@ -115,8 +98,32 @@ def update_yanlun():
return len(yanlun_texts) return len(yanlun_texts)
yanlun_seqs = [] @nonebot.get_driver().on_startup
update_yanlun() async def _():
global yanlun_texts, yanlun_seqs
solar_datetime = zhDateTime.DateTime.now()
lunar_datetime = solar_datetime.to_lunar()
solar_date = (solar_datetime.month, solar_datetime.day)
lunar_date = (lunar_datetime.lunar_month, lunar_datetime.lunar_day)
if solar_date == (4, 3):
yanlun_seqs = yanlun_texts = ["金羿ELS 生日快乐~", "Happy Birthday, Eilles!"]
elif solar_date == (8, 6):
yanlun_seqs = yanlun_texts = [
"诸葛亮与八卦阵 生日快乐~",
"Happy Birthday, bgArray~!",
]
elif solar_date == (8, 16):
yanlun_seqs = yanlun_texts = [
"鱼旧梦 生日快乐~",
"Happy Birthday, ElapsingDreams~!",
]
else:
nonebot.logger.info("正在获取言·论信息")
nonebot.logger.success("成功取得 言·论 {}".format(await update_yanlun()))
def random_yanlun_text() -> str: def random_yanlun_text() -> str:
@ -188,46 +195,7 @@ async def _(
# print(result.options) # print(result.options)
ulang = get_user_lang(event_utils.get_user_id(event)) # type: ignore ulang = get_user_lang(event_utils.get_user_id(event)) # type: ignore
if result.options["refresh"].value: if result.options["refresh"].value:
global yanlun_texts await update_yanlun()
try:
yanlun_texts = (
requests.get(
yanlun_path,
)
.text.strip("\n")
.split("\n")
)
await yanlun.send(
UniMessage.text(
ulang.get("yanlun.refresh.success", COUNT=len(yanlun_texts))
)
)
except (ConnectionError, requests.HTTPError, requests.RequestException) as E:
await yanlun.send(
UniMessage.text(
ulang.get(
"yanlun.refresh.failed",
ERR=ulang.get("yanlun.errtype.net"),
ERRCODE=f"\n{E}",
)
)
)
yanlun_texts = ["以梦想为驱使 创造属于自己的未来"]
# noinspection PyBroadException
except BaseException as E:
await yanlun.send(
UniMessage.text(
ulang.get(
"yanlun.refresh.failed",
ERR=ulang.get("yanlun.errtype.unknown"),
ERRCODE=f"\n{E}",
)
)
)
yanlun_texts = ["灵光焕发 深艺献心"]
yanlun_seqs = yanlun_texts.copy()
random.shuffle(yanlun_seqs)
if result.options["count"].value: if result.options["count"].value:
authors = [ authors = [
( (

View File

@ -1,19 +1,18 @@
import os import os
import sys import sys
import time
import json import json
import random import random
# import uuid
import shutil import shutil
import asyncio
from io import StringIO from io import StringIO
from pathlib import Path from pathlib import Path
from typing import Annotated, Any from typing import Annotated, Any, Union
# from nonebot import require # from nonebot import require
import requests import aiohttp
import zhDateTime import zhDateTime
import Musicreater import Musicreater
@ -39,8 +38,8 @@ import nonebot.adapters
import nonebot.drivers import nonebot.drivers
import nonebot.rule import nonebot.rule
from nonebot.params import CommandArg # from nonebot.params import CommandArg
from nonebot.permission import SUPERUSER from nonebot.permission import SUPERUSER, SuperUser
from nonebot.adapters.onebot.v11.event import ( from nonebot.adapters.onebot.v11.event import (
GroupUploadNoticeEvent, GroupUploadNoticeEvent,
GroupMessageEvent, GroupMessageEvent,
@ -56,7 +55,7 @@ from src.utils.base.language import get_user_lang
from src.utils.message.message import MarkdownMessage from src.utils.message.message import MarkdownMessage
from .execute_auto_translator import auto_translate # type: ignore from .execute_auto_translator import auto_translate # type: ignore
from .utils import utime_hanzify from .utils import hanzi_timeid
nonebot.require("nonebot_plugin_alconna") nonebot.require("nonebot_plugin_alconna")
nonebot.require("nonebot_plugin_apscheduler") nonebot.require("nonebot_plugin_apscheduler")
@ -182,6 +181,26 @@ def query_convert_points(
return store, people_convert_point[usr_id][item]["point"] return store, people_convert_point[usr_id][item]["point"]
def get_stored_path(
user_id: str, item: Union[Path, os.PathLike], superuser: bool = False
) -> Path:
if not isinstance(item, Path):
item_ = Path(item).name
else:
item_ = item.name
result_dest = database_dir / user_id / item_
if not result_dest.exists() and superuser:
for usr_id_ in filesaves.keys():
if (result_dest_ := database_dir / usr_id_ / item_).exists():
result_dest = result_dest_
break
return result_dest
# 每天4点更新 # 每天4点更新
@scheduler.scheduled_job("cron", hour=4) @scheduler.scheduled_job("cron", hour=4)
async def every_day_update(): async def every_day_update():
@ -195,7 +214,7 @@ async def every_day_update():
async def _(): async def _():
nonebot.logger.info("正在删除临时文件目录") nonebot.logger.info("正在删除临时文件目录")
while temporary_dir.exists(): while temporary_dir.exists():
time.sleep(1) await asyncio.sleep(1)
try: try:
shutil.rmtree(temporary_dir) shutil.rmtree(temporary_dir)
except Exception as E: except Exception as E:
@ -331,12 +350,12 @@ async def _(
os.makedirs(savepath, exist_ok=True) os.makedirs(savepath, exist_ok=True)
async with aiohttp.ClientSession() as client:
resp = await client.get(file_infomation["url"], verify_ssl=False)
(savepath / file_infomation["name"]).open("wb").write( (savepath / file_infomation["name"]).open("wb").write(
requests.get( await resp.content.read()
file_infomation["url"],
verify=False,
).content
) )
now = zhDateTime.DateTime.now() now = zhDateTime.DateTime.now()
try: try:
filesaves[usr_id][file_infomation["name"]] = { filesaves[usr_id][file_infomation["name"]] = {
@ -586,9 +605,13 @@ async def _(
nonebot.logger.info(result.options) nonebot.logger.info(result.options)
usr_id = str(event.user_id) usr_id = event.get_user_id()
if (qres := query_convert_points(usr_id, "music"))[0] is False: superuser_permission = await SUPERUSER(bot, event)
if ((qres := query_convert_points(usr_id, "music"))[0] is False) and (
not superuser_permission
):
await linglun_convert.finish( await linglun_convert.finish(
UniMessage.text( UniMessage.text(
"转换点数不足当前剩余⌊p⌋≈{:.2f}|{}".format( "转换点数不足当前剩余⌊p⌋≈{:.2f}|{}".format(
@ -599,7 +622,9 @@ async def _(
at_sender=True, at_sender=True,
) )
if usr_id not in filesaves.keys(): if (usr_id not in filesaves.keys()) and (
superuser_permission and not len(filesaves)
):
await linglun_convert.finish( await linglun_convert.finish(
UniMessage.text("服务器内未存入你的任何文件请先使用上传midi文件吧") UniMessage.text("服务器内未存入你的任何文件请先使用上传midi文件吧")
) )
@ -643,7 +668,7 @@ async def _(
# ) # )
nonebot.logger.info(_args) nonebot.logger.info(_args)
usr_data_path = database_dir / usr_id # usr_data_path = database_dir / usr_id
(usr_temp_path := temporary_dir / usr_id).mkdir(exist_ok=True) (usr_temp_path := temporary_dir / usr_id).mkdir(exist_ok=True)
if (_ppnt := _args["pitched-note-table"].lower()) in [ if (_ppnt := _args["pitched-note-table"].lower()) in [
@ -660,7 +685,11 @@ async def _(
else Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE else Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE
) )
) )
elif (_ppnt := (usr_data_path / _args["pitched-note-table"])).exists(): elif (
_ppnt := get_stored_path(
usr_id, _args["pitched-note-table"], superuser_permission
)
).exists():
pitched_notechart = Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE.copy() pitched_notechart = Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE.copy()
pitched_notechart.update(json.load(_ppnt.open("r"))) pitched_notechart.update(json.load(_ppnt.open("r")))
else: else:
@ -683,7 +712,11 @@ async def _(
else Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE else Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE
) )
) )
elif (_ppnt := (usr_data_path / _args["percussion-note-table"])).exists(): elif (
_ppnt := get_stored_path(
usr_id, _args["percussion-note-table"], superuser_permission
)
).exists():
percussion_notechart = Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE.copy() percussion_notechart = Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE.copy()
percussion_notechart.update(json.load(_ppnt.open("r"))) percussion_notechart.update(json.load(_ppnt.open("r")))
else: else:
@ -762,7 +795,14 @@ async def _(
if file_to_convert.endswith(".mid") or file_to_convert.endswith(".midi"): if file_to_convert.endswith(".mid") or file_to_convert.endswith(".midi"):
nonebot.logger.info("载入转换文件:{}".format(file_to_convert)) nonebot.logger.info("载入转换文件:{}".format(file_to_convert))
all_files[file_to_convert] = {} to_convert_path = get_stored_path(
usr_id, file_to_convert, superuser_permission
)
if to_convert_path.is_file():
all_files[to_convert_path.name] = {}
else:
continue
if isinstance( if isinstance(
msct_obj := query_convert_points(usr_id, "music", 0)[0], tuple msct_obj := query_convert_points(usr_id, "music", 0)[0], tuple
@ -779,9 +819,7 @@ async def _(
) )
and ( and (
msct_obj[0].music_name msct_obj[0].music_name
== os.path.splitext( == os.path.splitext(to_convert_path.name)[0].replace(" ", "_")
os.path.basename(usr_data_path / file_to_convert)
)[0].replace(" ", "_")
) )
): ):
nonebot.logger.info("载入已有缓存。") nonebot.logger.info("载入已有缓存。")
@ -792,7 +830,7 @@ async def _(
else: else:
if go_chk_point(): if go_chk_point():
msct_obj = Musicreater.MidiConvert.from_midi_file( msct_obj = Musicreater.MidiConvert.from_midi_file(
midi_file_path=usr_data_path / file_to_convert, midi_file_path=str(to_convert_path),
mismatch_error_ignorance=not _args["enable-mismatch-error"], mismatch_error_ignorance=not _args["enable-mismatch-error"],
play_speed=_args["play-speed"], play_speed=_args["play-speed"],
default_tempo=_args["default-tempo"], default_tempo=_args["default-tempo"],
@ -821,10 +859,10 @@ async def _(
else: else:
buffer.write( buffer.write(
"点数不足或出现错误:{}".format( "点数不足或出现错误:{}".format(
_args, to_convert_path.name,
) )
) )
break continue
# people_convert_point[usr_id] += 0.5 # people_convert_point[usr_id] += 0.5
@ -956,15 +994,15 @@ async def _(
"无可供转换的文件", "无可供转换的文件",
) )
await linglun_convert.finish( await linglun_convert.finish(
UniMessage("不是哥们,二氧化碳咱这转不成面包,那是中科院的事。") UniMessage(
"不是哥/姐/Any们二氧化碳咱这转不成面包那是中科院的事。\n*所指向之文件皆不存在"
)
) )
except Exception as e: except Exception as e:
nonebot.logger.error("转换存在错误:{}".format(e)) nonebot.logger.error("转换存在错误:{}".format(e))
buffer.write( buffer.write(
"[ERROR] {}\n".format(e).replace( "[ERROR] {}\n".format(e).replace(str(Path(__file__).parent.resolve()), "[]")
"C:\\Users\\Administrator\\Desktop\\RyBot\\", "[]"
)
) )
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
@ -975,11 +1013,7 @@ async def _(
fp := str( fp := str(
( (
temporary_dir temporary_dir
/ ( / (fn := "msctr[{}]-{}.zip".format(hanzi_timeid(), usr_id))
fn := "msctr[{}]-{}.zip".format(
utime_hanzify(zhDateTime.DateTime.now().to_lunar()), usr_id
)
)
).resolve() ).resolve()
), ),
) )

View File

@ -5,20 +5,19 @@ import shutil
import random import random
from io import StringIO from io import StringIO
from pathlib import Path
# from pathlib import Path
# import nonebot.rule # import nonebot.rule
import nonebot import nonebot
import soundfile import soundfile
import zhDateTime
import Musicreater import Musicreater
import Musicreater.plugin import Musicreater.plugin
import nonebot.adapters.onebot.v11.exception import nonebot.adapters.onebot.v11.exception
from .MusicPreview.main import PreviewMusic from .MusicPreview.main import PreviewMusic
from nonebot.permission import SUPERUSER
from nonebot.adapters.onebot.v11.event import ( from nonebot.adapters.onebot.v11.event import (
GroupMessageEvent, GroupMessageEvent,
PrivateMessageEvent, PrivateMessageEvent,
@ -48,11 +47,11 @@ from .msctexec import (
query_convert_points, query_convert_points,
filesaves, filesaves,
configdict, configdict,
database_dir,
temporary_dir, temporary_dir,
file_to_delete, file_to_delete,
get_stored_path,
) )
from .utils import utime_hanzify from .utils import hanzi_timeid
mspv_sync = on_alconna( mspv_sync = on_alconna(
Alconna( Alconna(
@ -109,13 +108,16 @@ async def _(
event: GroupMessageEvent | PrivateMessageEvent, event: GroupMessageEvent | PrivateMessageEvent,
bot: T_Bot, bot: T_Bot,
): ):
# print("E:\\Work2024\\test-midi\\" + name.result)
nonebot.logger.info(result.options) nonebot.logger.info(result.options)
usr_id = str(event.user_id) usr_id = event.get_user_id()
if (qres := query_convert_points(usr_id, "music"))[0] is False: superuser_permission = await SUPERUSER(bot, event)
if ((qres := query_convert_points(usr_id, "music"))[0] is False) and (
not superuser_permission
):
await mspv_sync.finish( await mspv_sync.finish(
UniMessage.text( UniMessage.text(
"转换点数不足当前剩余⌊p⌋≈{:.2f}|{}".format( "转换点数不足当前剩余⌊p⌋≈{:.2f}|{}".format(
@ -126,7 +128,9 @@ async def _(
at_sender=True, at_sender=True,
) )
if usr_id not in filesaves.keys(): if (usr_id not in filesaves.keys()) and (
superuser_permission and not len(filesaves)
):
await mspv_sync.finish( await mspv_sync.finish(
UniMessage.text("服务器内未存入你的任何文件请先使用上传midi文件吧") UniMessage.text("服务器内未存入你的任何文件请先使用上传midi文件吧")
) )
@ -173,7 +177,7 @@ async def _(
) )
) )
usr_data_path = database_dir / usr_id # usr_data_path = database_dir / usr_id
(usr_temp_path := temporary_dir / usr_id).mkdir(exist_ok=True) (usr_temp_path := temporary_dir / usr_id).mkdir(exist_ok=True)
if (_ppnt := _args["pitched-note-table"].lower()) in [ if (_ppnt := _args["pitched-note-table"].lower()) in [
@ -190,7 +194,11 @@ async def _(
else Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE else Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE
) )
) )
elif (_ppnt := (usr_data_path / _args["pitched-note-table"])).exists(): elif (
_ppnt := get_stored_path(
usr_id, _args["pitched-note-table"], superuser_permission
)
).exists():
pitched_notechart = Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE.copy() pitched_notechart = Musicreater.MM_TOUCH_PITCHED_INSTRUMENT_TABLE.copy()
pitched_notechart.update(json.load(_ppnt.open("r"))) pitched_notechart.update(json.load(_ppnt.open("r")))
else: else:
@ -213,7 +221,11 @@ async def _(
else Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE else Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE
) )
) )
elif (_ppnt := (usr_data_path / _args["percussion-note-table"])).exists(): elif (
_ppnt := get_stored_path(
usr_id, _args["percussion-note-table"], superuser_permission
)
).exists():
percussion_notechart = Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE.copy() percussion_notechart = Musicreater.MM_TOUCH_PERCUSSION_INSTRUMENT_TABLE.copy()
percussion_notechart.update(json.load(_ppnt.open("r"))) percussion_notechart.update(json.load(_ppnt.open("r")))
else: else:
@ -274,6 +286,14 @@ async def _(
# print("1") # print("1")
# await mspv_sync.finish("处理中") # await mspv_sync.finish("处理中")
to_convert_path = get_stored_path(
usr_id, file_to_convert, superuser_permission
)
if to_convert_path.is_file():
all_files.append(to_convert_path.name)
else:
continue
if isinstance( if isinstance(
msct_obj := query_convert_points(usr_id, "music", 0)[0], tuple msct_obj := query_convert_points(usr_id, "music", 0)[0], tuple
) and ( ) and (
@ -291,9 +311,7 @@ async def _(
) )
and ( and (
msct_obj[0].music_name msct_obj[0].music_name
== os.path.splitext( == os.path.splitext(to_convert_path.name)[0].replace(" ", "_")
os.path.basename(usr_data_path / file_to_convert)
)[0].replace(" ", "_")
) )
): ):
nonebot.logger.info("载入已有缓存。") nonebot.logger.info("载入已有缓存。")
@ -302,7 +320,7 @@ async def _(
if go_chk_point(): if go_chk_point():
msct_obj = Musicreater.MidiConvert.from_midi_file( msct_obj = Musicreater.MidiConvert.from_midi_file(
midi_file_path=usr_data_path / file_to_convert, midi_file_path=str(to_convert_path),
mismatch_error_ignorance=not _args["enable-mismatch-error"], mismatch_error_ignorance=not _args["enable-mismatch-error"],
play_speed=_args["play-speed"], play_speed=_args["play-speed"],
default_tempo=_args["default-tempo"], default_tempo=_args["default-tempo"],
@ -329,12 +347,10 @@ async def _(
else: else:
buffer.write( buffer.write(
"点数不足或出现错误:\n{}".format( "点数不足或出现错误:\n{}".format(
_args, to_convert_path.name,
) )
) )
break continue
all_files.append(file_to_convert)
music_temp = PreviewMusic( music_temp = PreviewMusic(
msct_obj, msct_obj,
@ -379,15 +395,15 @@ async def _(
"无可供转换的文件", "无可供转换的文件",
) )
await mspv_sync.finish( await mspv_sync.finish(
UniMessage("我服了老弟,这机器人也不能给路易十六理发啊。") UniMessage(
"我服了老弟,这机器人也不能给路易十六理发啊。\n*所指向之文件皆不存在"
)
) )
except Exception as e: except Exception as e:
nonebot.logger.error("合成存在错误:{}".format(e)) nonebot.logger.error("合成存在错误:{}".format(e))
buffer.write( buffer.write(
"[ERROR] {}\n".format(e).replace( "[ERROR] {}\n".format(e).replace(str(Path(__file__).parent.resolve()), "[]")
"C:\\Users\\Administrator\\Desktop\\RyBot\\", "[]"
)
) )
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
@ -398,11 +414,7 @@ async def _(
usr_temp_path, usr_temp_path,
fp := str( fp := str(
temporary_dir temporary_dir
/ ( / (fn := "mprwav[{}]{}.zip".format(hanzi_timeid(), usr_id))
fn := "mprwav[{}]{}.zip".format(
utime_hanzify(zhDateTime.DateTime.now().to_lunar()), usr_id
)
)
), ),
) )

View File

@ -1,9 +1,12 @@
import zhDateTime import zhDateTime
from typing import Optional
def hanzi_timeid(
def utime_hanzify( zhd: Optional[zhDateTime.zhDateTime] = None,
zhd: zhDateTime.zhDateTime = zhDateTime.DateTime.now().to_lunar(),
) -> str: ) -> str:
if not zhd:
zhd = zhDateTime.DateTime.now().to_lunar()
return "{地支时}{刻}{分}{秒}".format( return "{地支时}{刻}{分}{秒}".format(
地支时=zhDateTime.DÌZHĪ[zhd.shichen] 地支时=zhDateTime.DÌZHĪ[zhd.shichen]
+ ( + (