feat(bot): 添加定时任务清理缓存功能

refactor(light_novel): 将轻小说图片路径改为临时路径
refactor(data_source): 将日报图片路径改为临时路径
feat(platform): 添加定时清理缓存功能
refactor(html_to_img): 将轻小说图片路径改为临时路径
refactor(daily_report): 将日报图片路径改为临时路径
refactor(path_config): 移除日报和轻小说路径配置
This commit is contained in:
SlyAimer 2025-03-03 11:25:03 +08:00
parent e1328deddb
commit d6116f7b2c
7 changed files with 42 additions and 26 deletions

11
bot.py
View file

@ -1,18 +1,23 @@
import nonebot import nonebot
from nonebot.adapters.qq import Adapter as QQAdapter from nonebot.adapters.qq import Adapter as QQAdapter
from nonebot import logger from nonebot import logger
from nonebot.log import default_format, default_filter from nonebot.log import default_format
from apscheduler.schedulers.blocking import BlockingScheduler
from src.configs.path_config import log_path
from src.plugins.platform import clean_temp_cache
nonebot.init() nonebot.init()
driver = nonebot.get_driver() driver = nonebot.get_driver()
driver.register_adapter(QQAdapter) # 注册QQ适配器 driver.register_adapter(QQAdapter) # 注册QQ适配器
nonebot.load_from_toml("pyproject.toml") nonebot.load_from_toml("pyproject.toml")
logger.add("src/resources/log/error.log", level="ERROR", format=default_format, rotation="1 week") logger.add(log_path, level="ERROR", format=default_format, rotation="1 week")
from src.clover_sqlite.data_init.db_connect import disconnect, init from src.clover_sqlite.data_init.db_connect import disconnect, init
driver.on_startup(init) driver.on_startup(init)
driver.on_shutdown(disconnect) driver.on_shutdown(disconnect)
scheduler = BlockingScheduler()
scheduler.add_job(clean_temp_cache, 'cron', hour=0, minute=0)
scheduler.start()
if __name__ == "__main__": if __name__ == "__main__":
nonebot.run() nonebot.run()

View file

@ -6,7 +6,7 @@ from pathlib import Path
from nonebot_plugin_htmlrender import template_to_pic from nonebot_plugin_htmlrender import template_to_pic
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
from src.configs.path_config import light_novel_path from src.configs.path_config import temp_path
import src.clover_lightnovel.wenku8 as Wenku8 import src.clover_lightnovel.wenku8 as Wenku8
@ -17,13 +17,13 @@ async def save_img(data: bytes):
:param data: :param data:
:return: :return:
""" """
file_path = light_novel_path + f"{datetime.now().date()}.png" file_path = temp_path + f"{datetime.now().date()}轻小说.png"
with open(file_path, "wb") as file: with open(file_path, "wb") as file:
file.write(data) file.write(data)
async def get_ln_image(): async def get_ln_image():
now = datetime.now() now = datetime.now()
file = Path() / light_novel_path / f"{now.date()}.png" file = Path() / temp_path / f"{now.date()}轻小说.png"
if os.path.exists(file): if os.path.exists(file):
with file.open("rb") as image_file: with file.open("rb") as image_file:
return image_file.read() return image_file.read()

View file

@ -13,7 +13,7 @@ from tenacity import retry, stop_after_attempt, wait_fixed
from zhdate import ZhDate from zhdate import ZhDate
from .config import Anime, Hitokoto, SixData from .config import Anime, Hitokoto, SixData
from .date import get_festivals_dates from .date import get_festivals_dates
from src.configs.path_config import daily_news_path from src.configs.path_config import temp_path
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
@ -68,7 +68,7 @@ async def save_img(data: bytes):
:param data: :param data:
:return: :return:
""" """
file_path = daily_news_path + f"{datetime.now().date()}.png" file_path = temp_path + f"{datetime.now().date()}.png"
with open(file_path, "wb") as file: with open(file_path, "wb") as file:
file.write(data) file.write(data)
@ -98,7 +98,7 @@ class Report:
async def get_report_image(cls) -> bytes: async def get_report_image(cls) -> bytes:
"""获取数据""" """获取数据"""
now = datetime.now() now = datetime.now()
file = Path() / daily_news_path / f"{now.date()}.png" file = Path() / temp_path / f"{now.date()}.png"
if os.path.exists(file): if os.path.exists(file):
with file.open("rb") as image_file: with file.open("rb") as image_file:
return image_file.read() return image_file.read()

View file

@ -22,12 +22,6 @@ os.makedirs(good_bad, exist_ok=True)
#谁说 生成图片路径 #谁说 生成图片路径
who_say_path = path+'/image/who_say/' who_say_path = path+'/image/who_say/'
os.makedirs(who_say_path, exist_ok=True) os.makedirs(who_say_path, exist_ok=True)
# 日报
daily_news_path = path+'/image/report/'
os.makedirs(daily_news_path, exist_ok=True)
# 轻小说
light_novel_path = path + '/image/lightnovel/'
os.makedirs(light_novel_path, exist_ok=True)
#yuc_wiki 动漫wiki #yuc_wiki 动漫wiki
yuc_wiki_path = path + '/image/yuc_wiki/' yuc_wiki_path = path + '/image/yuc_wiki/'
os.makedirs(yuc_wiki_path, exist_ok=True) os.makedirs(yuc_wiki_path, exist_ok=True)

View file

@ -4,7 +4,7 @@ from nonebot.rule import to_me
from nonebot.plugin import on_command from nonebot.plugin import on_command
from nonebot.adapters.qq import MessageSegment from nonebot.adapters.qq import MessageSegment
from src.clover_report.data_source import Report from src.clover_report.data_source import Report
from src.configs.path_config import daily_news_path from src.configs.path_config import temp_path
import os import os
@ -12,9 +12,8 @@ daily_report = on_command("日报", rule=to_me(), priority=10)
@daily_report.handle() @daily_report.handle()
async def handle_function(): async def handle_function():
now = datetime.now() now = datetime.now()
file = Path() / daily_news_path / f"{now.date()}.png" file = Path() / temp_path / f"{now.date()}日报.png"
if not os.path.exists(file): if not os.path.exists(file):
await daily_report.send("您是今天第一个查看日报的哦,来看看世界上都发生了些什么吧💫\nCrunching the latest news, just for you. Hang tight…") await daily_report.send("您是今天第一个查看日报的哦,来看看世界上都发生了些什么吧💫\nCrunching the latest news, just for you. Hang tight…")
await Report.get_report_image() await Report.get_report_image()
now = datetime.now().date() await daily_report.finish(MessageSegment.file_image(Path(temp_path+f"{now}日报.png")))
await daily_report.finish(MessageSegment.file_image(Path(daily_news_path+f"{now}.png")))

View file

@ -6,7 +6,7 @@ from nonebot.rule import to_me
from nonebot.plugin import on_command from nonebot.plugin import on_command
from nonebot.adapters.qq import MessageSegment from nonebot.adapters.qq import MessageSegment
from src.clover_lightnovel.html_to_img import get_ln_image from src.clover_lightnovel.html_to_img import get_ln_image
from src.configs.path_config import light_novel_path from src.configs.path_config import temp_path
import os import os
@ -14,7 +14,7 @@ light_novel = on_command("轻小说", rule=to_me(), priority=10, block=True)
@light_novel.handle() @light_novel.handle()
async def get_ln(): async def get_ln():
now = datetime.now() now = datetime.now()
file = Path() / light_novel_path / f"{now.date()}.png" file = Path() / temp_path / f"{now.date()}轻小说.png"
if not os.path.exists(file): if not os.path.exists(file):
await light_novel.send("正在为您整理最新轻小说咨询哦,请稍等🥳") await light_novel.send("正在为您整理最新轻小说咨询哦,请稍等🥳")
try: try:
@ -25,4 +25,4 @@ async def get_ln():
"\033[31;1m requests.exceptions.InvalidURL \033[0m" + str(e)) "\033[31;1m requests.exceptions.InvalidURL \033[0m" + str(e))
await light_novel.finish("获取信息失败了,请重试。") await light_novel.finish("获取信息失败了,请重试。")
now = datetime.now().date() now = datetime.now().date()
await light_novel.finish(MessageSegment.file_image(Path(light_novel_path+f"{now}.png"))) await light_novel.finish(MessageSegment.file_image(Path(temp_path+f"{now}轻小说.png")))

View file

@ -1,10 +1,14 @@
import os
import glob
import time
import psutil
import platform
import logging
from pathlib import Path from pathlib import Path
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.plugin import on_command from nonebot.plugin import on_command
from nonebot.adapters.qq import Message, MessageSegment from nonebot.adapters.qq import Message, MessageSegment
import platform from src.configs.path_config import temp_path,video_path
import psutil
import time
repository = on_command("repo", rule=to_me(), priority=10, block=True) repository = on_command("repo", rule=to_me(), priority=10, block=True)
@ -40,3 +44,17 @@ async def get_platform_info():
"\n\n[Python版本]: " + python_version + "\n\n[Python版本]: " + python_version +
"\n\n[Bot源码]: 请发送 /repo \n[联系我们]: cloverta@petalmail·com") "\n\n[Bot源码]: 请发送 /repo \n[联系我们]: cloverta@petalmail·com")
await platform_info.finish(content) await platform_info.finish(content)
def clean_temp_cache():
"""定时清理缓存文件"""
path_list = [temp_path, video_path]
for folder_path in path_list:
files = get_files_in_folder(folder_path)
for file in files:
os.remove(file)
def get_files_in_folder(folder_path: Path):
return [Path(f) for f in glob.glob(str(folder_path / "*")) if Path(f).is_file()]