LittlePaimon/Paimon_Calendar/generate.py

50 lines
1.8 KiB
Python
Raw Normal View History

2022-06-22 10:36:40 +08:00
from nonebot import require
2022-06-21 20:37:54 +08:00
2022-06-22 10:36:40 +08:00
require("nonebot_plugin_htmlrender")
2022-06-21 20:37:54 +08:00
from nonebot_plugin_htmlrender import html_to_pic
import jinja2
2022-05-27 21:13:59 +08:00
from .event import *
from .draw import *
2022-06-21 20:37:54 +08:00
body = []
2022-06-22 10:36:40 +08:00
template_path = Path(__file__).parent / 'template'
2022-06-21 20:37:54 +08:00
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path), enable_async=True)
2022-05-27 21:13:59 +08:00
async def generate_day_schedule(server='cn'):
2022-06-21 20:37:54 +08:00
events = await get_events(server, 0, 15)
2022-05-27 21:13:59 +08:00
has_prediction = False
2022-06-21 20:37:54 +08:00
""" 追加数据前先执行清除,以防数据叠加 """
body.clear()
2022-05-27 21:13:59 +08:00
for event in events:
if event['start_days'] > 0:
has_prediction = True
2022-06-21 20:37:54 +08:00
template = env.get_template('calendar.html')
2022-05-27 21:13:59 +08:00
for event in events:
if event['start_days'] <= 0:
2022-06-21 20:37:54 +08:00
time = '即将结束' if event["left_days"] == 0 else f'{str(event["left_days"])}天后结束'
body.append({
2022-06-22 10:36:40 +08:00
'title': event['title'],
'time': time,
2022-06-21 20:37:54 +08:00
'online': f'{datetime.strftime(event["start"], r"%m-%d")} ~ {datetime.strftime(event["end"], r"%m-%d")}',
2022-06-22 10:36:40 +08:00
'color': event['color'],
2022-06-21 20:37:54 +08:00
'banner': event['banner']
})
2022-05-27 21:13:59 +08:00
if has_prediction:
for event in events:
if event['start_days'] > 0:
2022-06-21 20:37:54 +08:00
time = '即将开始' if event["start_days"] == 0 else f'{str(event["start_days"])}天后开始'
body.append({
2022-06-22 10:36:40 +08:00
'title': event['title'],
'time': time,
2022-06-21 20:37:54 +08:00
'online': f'{datetime.strftime(event["start"], r"%m-%d")} ~ {datetime.strftime(event["end"], r"%m-%d")}',
2022-06-22 10:36:40 +08:00
'color': event['color'],
2022-06-21 20:37:54 +08:00
'banner': event['banner']
})
content = await template.render_async(body=body, css_path=template_path)
return await html_to_pic(content, wait=0, viewport={"width": 600, "height": 100})