1
0
forked from bot/app

更改内容:

1. 更新文言文翻译
2. 删除重复的翻译键
3. 重写status缓存机制并black格式化
请注意:未经过本地测试,需要观察者辅助测试,谢谢!
This commit is contained in:
2024-07-14 02:19:57 +08:00
parent bb20d9623d
commit a4c7ee738c
16 changed files with 294 additions and 292 deletions

View File

@ -23,12 +23,12 @@ from nonebot_plugin_apscheduler import scheduler
commit_hash = Repo(".").head.commit.hexsha
protocol_names = {
0: "iPad",
1: "Android Phone",
2: "Android Watch",
3: "Mac",
5: "iPad",
6: "Android Pad",
0: "iPad",
1: "Android Phone",
2: "Android Watch",
3: "Mac",
5: "iPad",
6: "Android Pad",
}
"""
@ -67,73 +67,74 @@ status_card_cache = {} # lang -> bytes
# 60s刷新一次
# 之前写的什么鬼玩意,这么重要的功能这样写???
@scheduler.scheduled_job("cron", second="*/40")
async def refresh_status_card():
nonebot.logger.debug("Refreshing status card cache...")
async def refresh_status_card(languale: str = "zh-CN"):
nonebot.logger.debug("Refreshing status card cache of lang {}...".format(languale))
global status_card_cache
bot_data = await get_bots_data()
hardware_data = await get_hardware_data()
liteyuki_data = await get_liteyuki_data()
for lang in status_card_cache.keys():
status_card_cache[lang] = await generate_status_card(
bot_data,
hardware_data,
liteyuki_data,
lang=lang,
use_cache=False
)
status_card_cache = {}
# bot_data = await get_bots_data()
# hardware_data = await get_hardware_data()
# liteyuki_data = await get_liteyuki_data()
# for lang in status_card_cache.keys():
# status_card_cache[lang] = await generate_status_card(
# bot_data,
# hardware_data,
# liteyuki_data,
# lang=lang,
# use_cache=False
# )
async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang="zh-CN", bot_id="0",
use_cache=False
) -> bytes:
if not use_cache:
return await template2image(
get_path("templates/status.html", abs_path=True),
{
"data": {
"bot" : bot,
"hardware" : hardware,
"liteyuki" : liteyuki,
"localization": get_local_data(lang)
}
},
)
else:
if lang not in status_card_cache:
status_card_cache[lang] = await generate_status_card(bot, hardware, liteyuki, lang=lang, bot_id=bot_id)
return status_card_cache[lang]
# 获取状态卡片
# bot_id 参数已经是bot参数的一部分了不需要保留但为了“兼容性”……
async def generate_status_card(
bot: dict,
hardware: dict,
liteyuki: dict,
lang="zh-CN",
bot_id="0",
) -> bytes:
return await template2image(
get_path("templates/status.html", abs_path=True),
{
"data": {
"bot": bot,
"hardware": hardware,
"liteyuki": liteyuki,
"localization": get_local_data(lang),
}
},
)
def get_local_data(lang_code) -> dict:
lang = Language(lang_code)
return {
"friends" : lang.get("status.friends"),
"groups" : lang.get("status.groups"),
"plugins" : lang.get("status.plugins"),
"bots" : lang.get("status.bots"),
"message_sent" : lang.get("status.message_sent"),
"message_received": lang.get("status.message_received"),
"cpu" : lang.get("status.cpu"),
"memory" : lang.get("status.memory"),
"swap" : lang.get("status.swap"),
"disk" : lang.get("status.disk"),
"usage" : lang.get("status.usage"),
"total" : lang.get("status.total"),
"used" : lang.get("status.used"),
"free" : lang.get("status.free"),
"days" : lang.get("status.days"),
"hours" : lang.get("status.hours"),
"minutes" : lang.get("status.minutes"),
"seconds" : lang.get("status.seconds"),
"runtime" : lang.get("status.runtime"),
"threads" : lang.get("status.threads"),
"cores" : lang.get("status.cores"),
"process" : lang.get("status.process"),
"resources" : lang.get("status.resources"),
"description" : lang.get("status.description"),
"friends": lang.get("status.friends"),
"groups": lang.get("status.groups"),
"plugins": lang.get("status.plugins"),
"bots": lang.get("status.bots"),
"message_sent": lang.get("status.message_sent"),
"message_received": lang.get("status.message_received"),
"cpu": lang.get("status.cpu"),
"memory": lang.get("status.memory"),
"swap": lang.get("status.swap"),
"disk": lang.get("status.disk"),
"usage": lang.get("status.usage"),
"total": lang.get("status.total"),
"used": lang.get("status.used"),
"free": lang.get("status.free"),
"days": lang.get("status.days"),
"hours": lang.get("status.hours"),
"minutes": lang.get("status.minutes"),
"seconds": lang.get("status.seconds"),
"runtime": lang.get("status.runtime"),
"threads": lang.get("status.threads"),
"cores": lang.get("status.cores"),
"process": lang.get("status.process"),
"resources": lang.get("status.resources"),
"description": lang.get("status.description"),
}
@ -142,8 +143,8 @@ async def get_bots_data(self_id: str = "0") -> dict:
Returns:
"""
result = {
"self_id": self_id,
"bots" : [],
"self_id": self_id,
"bots": [],
}
for bot_id, bot in nonebot.get_bots().items():
groups = 0
@ -181,15 +182,25 @@ async def get_bots_data(self_id: str = "0") -> dict:
else:
icon = None
bot_data = {
"name" : bot_name,
"icon" : icon,
"id" : bot_id,
"protocol_name" : protocol_names.get(version_info.get("protocol_name"), "Online"),
"groups" : groups,
"friends" : friends,
"message_sent" : satori_counter.msg_sent if isinstance(bot, satori.Bot) else statistics.get("message_sent", 0),
"message_received": satori_counter.msg_received if isinstance(bot, satori.Bot) else statistics.get("message_received", 0),
"app_name" : app_name
"name": bot_name,
"icon": icon,
"id": bot_id,
"protocol_name": protocol_names.get(
version_info.get("protocol_name"), "Online"
),
"groups": groups,
"friends": friends,
"message_sent": (
satori_counter.msg_sent
if isinstance(bot, satori.Bot)
else statistics.get("message_sent", 0)
),
"message_received": (
satori_counter.msg_received
if isinstance(bot, satori.Bot)
else statistics.get("message_received", 0)
),
"app_name": app_name,
}
result["bots"].append(bot_data)
@ -221,27 +232,27 @@ async def get_hardware_data() -> dict:
else:
brand = "Unknown"
result = {
"cpu" : {
"percent": psutil.cpu_percent(),
"name" : f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}",
"cores" : psutil.cpu_count(logical=False),
"threads": psutil.cpu_count(logical=True),
"freq" : psutil.cpu_freq().current # MHz
},
"memory": {
"percent" : mem.percent,
"total" : mem.total,
"used" : mem.used,
"free" : mem.free,
"usedProcess": mem_used_process,
},
"swap" : {
"percent": swap.percent,
"total" : swap.total,
"used" : swap.used,
"free" : swap.free
},
"disk" : [],
"cpu": {
"percent": psutil.cpu_percent(),
"name": f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}",
"cores": psutil.cpu_count(logical=False),
"threads": psutil.cpu_count(logical=True),
"freq": psutil.cpu_freq().current, # MHz
},
"memory": {
"percent": mem.percent,
"total": mem.total,
"used": mem.used,
"free": mem.free,
"usedProcess": mem_used_process,
},
"swap": {
"percent": swap.percent,
"total": swap.total,
"used": swap.used,
"free": swap.free,
},
"disk": [],
}
for disk in psutil.disk_partitions(all=True):
@ -249,13 +260,15 @@ async def get_hardware_data() -> dict:
disk_usage = psutil.disk_usage(disk.mountpoint)
if disk_usage.total == 0:
continue # 虚拟磁盘
result["disk"].append({
"name" : disk.mountpoint,
result["disk"].append(
{
"name": disk.mountpoint,
"percent": disk_usage.percent,
"total" : disk_usage.total,
"used" : disk_usage.used,
"free" : disk_usage.free
})
"total": disk_usage.total,
"used": disk_usage.used,
"free": disk_usage.free,
}
)
except:
pass
@ -265,14 +278,15 @@ async def get_hardware_data() -> dict:
async def get_liteyuki_data() -> dict:
temp_data: TempConfig = common_db.where_one(TempConfig(), default=TempConfig())
result = {
"name" : list(get_config("nickname", [__NAME__]))[0],
"version" : f"{__VERSION__}{'-' + commit_hash[:7] if (commit_hash and len(commit_hash) > 8) else ''}",
"plugins" : len(nonebot.get_loaded_plugins()),
"resources": len(get_loaded_resource_packs()),
"nonebot" : f"{nonebot.__version__}",
"python" : f"{platform.python_implementation()} {platform.python_version()}",
"system" : f"{platform.system()} {platform.release()}",
"runtime" : time.time() - temp_data.data.get("start_time", time.time()), # 运行时间秒数
"bots" : len(nonebot.get_bots())
"name": list(get_config("nickname", [__NAME__]))[0],
"version": f"{__VERSION__}{'-' + commit_hash[:7] if (commit_hash and len(commit_hash) > 8) else ''}",
"plugins": len(nonebot.get_loaded_plugins()),
"resources": len(get_loaded_resource_packs()),
"nonebot": f"{nonebot.__version__}",
"python": f"{platform.python_implementation()} {platform.python_version()}",
"system": f"{platform.system()} {platform.release()}",
"runtime": time.time()
- temp_data.data.get("start_time", time.time()), # 运行时间秒数
"bots": len(nonebot.get_bots()),
}
return result

View File

@ -17,7 +17,7 @@ status_alc = on_alconna(
Subcommand(
"process",
alias={"proc", "p", "进程"},
)
),
),
)
@ -25,17 +25,15 @@ status_alc = on_alconna(
@status_alc.handle()
async def _(event: T_MessageEvent, bot: T_Bot):
ulang = get_user_lang(event_utils.get_user_id(event))
if ulang.lang_code in status_card_cache:
image = status_card_cache[ulang.lang_code]
else:
image = await generate_status_card(
if ulang.lang_code not in status_card_cache.keys():
status_card_cache[ulang.lang_code] = await generate_status_card(
bot=await get_bots_data(),
hardware=await get_hardware_data(),
liteyuki=await get_liteyuki_data(),
lang=ulang.lang_code,
bot_id=bot.self_id,
use_cache=True
)
image = status_card_cache[ulang.lang_code]
await status_alc.finish(UniMessage.image(raw=image))