LittlePaimon/Paimon_Info/draw_daily_note.py

161 lines
8.8 KiB
Python
Raw Normal View History

2022-04-30 14:13:39 +08:00
import datetime
import os
2022-05-20 18:44:18 +08:00
import random
2022-05-12 21:45:27 +08:00
import matplotlib.pyplot as plt
2022-05-20 18:44:18 +08:00
from PIL import Image, ImageDraw, ImageFont
2022-06-01 12:59:03 +08:00
from pathlib import Path
2022-05-20 18:44:18 +08:00
from utils import aiorequests
from utils.message_util import MessageBuild
from utils.file_handler import load_image
2022-04-30 14:13:39 +08:00
res_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'res')
2022-05-12 21:45:27 +08:00
def get_font(size, font='msyhbd.ttc'):
return ImageFont.truetype(os.path.join(res_path, font), size)
2022-04-30 14:13:39 +08:00
2022-05-12 21:45:27 +08:00
async def draw_ring(per):
if per > 1:
per = 1
elif per < 0:
per = 0
2022-05-12 21:45:27 +08:00
per_list = [per, 1 - per]
colors = ['#507bd0', '#FFFFFF']
plt.pie(per_list, startangle=90, colors=colors)
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(per_list,
wedgeprops={'width': 0.18},
startangle=90,
colors=colors)
plt.savefig('temp.png', transparent=True)
img = Image.open('temp.png').resize((266, 266)).convert('RGBA')
os.remove('temp.png')
plt.cla()
plt.close("all")
2022-05-12 21:45:27 +08:00
return img
2022-04-30 14:13:39 +08:00
async def draw_daily_note_card(data, uid):
if not data:
return '数据出错'
if data['retcode'] == 10102:
return f'uid{uid}没有在米游社公开信息哦,请到 个人主页-管理 中打开'
2022-04-30 14:13:39 +08:00
elif data['retcode'] == 10104:
return f'uid{uid}有误哦检查一下或再手动输入一次uid吧'
2022-04-30 14:13:39 +08:00
elif data['retcode'] != 0:
return f'派蒙获取{uid}数据失败了,获取状态:\n{data["message"]},{data["retcode"]}'
2022-04-30 14:13:39 +08:00
data = data['data']
circle_img = load_image(os.path.join(res_path, 'daily_note', '透明圆.png'))
finished_icon = load_image(os.path.join(res_path, 'daily_note', 'finished.png'))
bg_img = load_image(os.path.join(res_path, 'daily_note', 'ssbq.png'), mode='RGBA')
2022-04-30 14:13:39 +08:00
bg_draw = ImageDraw.Draw(bg_img)
2022-05-12 21:45:27 +08:00
# uid文字
bg_draw.text((152, 251), f"uid{uid}", fill='#5680d2', font=get_font(60, 'number.ttf'))
# 树脂文字
bg_draw.text((337, 480), f"{data['current_resin']}/160", fill='white', font=get_font(48, 'number.ttf'))
bg_img.alpha_composite(await draw_ring(data['current_resin'] / 160), (98, 369))
if data['current_resin'] == 160:
bg_draw.text((892, 480), f"树脂满了哦~", fill='white', font=get_font(40, '优设标题黑.ttf'))
2022-04-30 14:13:39 +08:00
else:
recover_time = datetime.datetime.now() + datetime.timedelta(seconds=int(data['resin_recovery_time']))
recover_time_day = '今天' if recover_time.day == datetime.datetime.now().day else '明天'
recover_time_str = f'将于{recover_time_day}{recover_time.strftime("%H:%M")}回满'
2022-05-12 21:45:27 +08:00
bg_draw.text((780, 480), recover_time_str, fill='white', font=get_font(40, '优设标题黑.ttf'))
# 宝钱文字
2022-05-12 22:10:54 +08:00
bg_draw.text((337, 701), f"{data['current_home_coin']}/{data['max_home_coin']}", fill='white', font=get_font(48, 'number.ttf'))
bg_img.alpha_composite(await draw_ring(data['current_home_coin'] / data['max_home_coin']), (98, 593))
if data['current_home_coin'] == data['max_home_coin']:
2022-05-12 21:45:27 +08:00
bg_draw.text((820, 701), f"洞天宝钱满了哦~", fill='white', font=get_font(40, '优设标题黑.ttf'))
2022-04-30 14:13:39 +08:00
else:
recover_time = datetime.datetime.now() + datetime.timedelta(seconds=int(data['home_coin_recovery_time']))
recover_time_day = recover_time.day - datetime.datetime.now().day
if recover_time_day == 1:
recover_time_day_str = '明天'
elif recover_time_day == 0:
recover_time_day_str = '今天'
else:
recover_time_day_str = str(recover_time.day) + ''
recover_time_str = f'将于{recover_time_day_str}{recover_time.strftime("%H:%M")}攒满'
2022-05-12 21:45:27 +08:00
bg_draw.text((762, 701), recover_time_str, fill='white', font=get_font(40, '优设标题黑.ttf'))
# 委托文字
bg_draw.text((337, 924), f"{data['finished_task_num']}/4", fill='white', font=get_font(48, 'number.ttf'))
bg_img.alpha_composite(await draw_ring(data['finished_task_num'] / 4), (98, 816))
if data['finished_task_num'] == 4:
bg_draw.text((750, 924), "今日委托已全部完成~", fill='white', font=get_font(40, '优设标题黑.ttf'))
2022-04-30 14:13:39 +08:00
else:
2022-05-12 21:45:27 +08:00
bg_draw.text((790, 924), "今日委托完成情况", fill='white', font=get_font(40, '优设标题黑.ttf'))
# 质变文字
if data['transformer']['obtained']:
bg_draw.text((337, 1147), f"{7 - data['transformer']['recovery_time']['Day']}/7", fill='white',
font=get_font(48, 'number.ttf'))
bg_img.alpha_composite(await draw_ring((7 - data['transformer']['recovery_time']['Day']) / 7), (98, 1039))
if data['transformer']['recovery_time']['Day'] == 0:
bg_draw.text((465, 1147), "可使用", fill='white', font=get_font(40, '优设标题黑.ttf'))
else:
bg_draw.text((471, 1127), f"{data['transformer']['recovery_time']['Day']}天后", fill='white',
font=get_font(40, '优设标题黑.ttf'))
bg_draw.text((465, 1167), "可使用", fill='white', font=get_font(40, '优设标题黑.ttf'))
2022-04-30 14:13:39 +08:00
else:
2022-05-12 21:45:27 +08:00
bg_draw.text((337, 1143), "未获得", fill='white', font=get_font(48, '优设标题黑.ttf'))
# 周本文字
bg_draw.text((843, 1147), f"{3 - data['remain_resin_discount_num']}/3", fill='white',
font=get_font(48, 'number.ttf'))
bg_img.alpha_composite(await draw_ring((3 - data['remain_resin_discount_num']) / 3), (604, 1039))
if data['remain_resin_discount_num'] == 0:
bg_draw.text((1005, 1147), "已完成", fill='white', font=get_font(40, '优设标题黑.ttf'))
2022-04-30 14:13:39 +08:00
else:
2022-05-12 21:45:27 +08:00
bg_draw.text((977, 1127), f"剩余{data['remain_resin_discount_num']}", fill='white',
font=get_font(40, '优设标题黑.ttf'))
bg_draw.text((965, 1167), "周本减半", fill='white', font=get_font(40, '优设标题黑.ttf'))
# 深渊文字
2022-05-16 08:36:20 +08:00
abyss_new_month = datetime.datetime.now().month if datetime.datetime.now().day < 16 else datetime.datetime.now().month + 1
abyss_new_day = 16 if datetime.datetime.now().day < 16 else 1
abyss_new = datetime.datetime.strptime('2022.' + str(abyss_new_month) + '.' + str(abyss_new_day) + '.00:00',
2022-04-30 14:13:39 +08:00
'%Y.%m.%d.%H:%M') - datetime.datetime.now()
2022-05-12 21:45:27 +08:00
bg_draw.text((337, 1358), f"{abyss_new.days}/15", fill='white',
font=get_font(48, 'number.ttf'))
bg_draw.text((745, 1358), f"本期深渊还有{abyss_new.days if abyss_new.days <= 15 else 15}天结束", fill='white',
2022-05-12 21:45:27 +08:00
font=get_font(40, '优设标题黑.ttf'))
bg_img.alpha_composite(await draw_ring(abyss_new.days if abyss_new.days <= 15 else 15 / 15), (100, 1249))
2022-05-12 21:45:27 +08:00
# 派遣情况
exp = data['expeditions']
i = 0
for role in exp:
2022-06-01 12:59:03 +08:00
role_avatar = Path() / 'data' / 'LittlePaimon' / 'res' / 'avatar_side' / role['avatar_side_icon'].split('/')[-1]
role_avatar = await aiorequests.get_img(url=role['avatar_side_icon'], size=(135, 135), mode='RGBA', save_path=role_avatar)
2022-05-12 21:45:27 +08:00
bg_img.alpha_composite(role_avatar, (i * 200 + 168, 1537))
bg_img.alpha_composite(await draw_ring(1 - int(role['remained_time']) / 72000), (i * 201 + 101, 1490))
if role['status'] == 'Ongoing':
2022-05-16 08:36:20 +08:00
bg_img.alpha_composite(circle_img, (i * 201 + 172, 1559))
2022-05-12 21:45:27 +08:00
hour = int(role['remained_time']) // 3600
2022-05-12 22:10:54 +08:00
bg_draw.text((i * 200 + 212, 1580), f"{hour}h", fill='white', font=get_font(40, 'number.ttf'))
2022-05-12 21:45:27 +08:00
minute = int(role['remained_time']) % 3600 // 60
2022-05-12 22:10:54 +08:00
bg_draw.text((i * 200 + 197, 1620), f"{minute}m", fill='white', font=get_font(40, 'number.ttf'))
2022-04-30 14:13:39 +08:00
else:
2022-05-12 21:45:27 +08:00
bg_img.alpha_composite(finished_icon, (i * 200 + 191, 1576))
i += 1
2022-05-12 22:10:54 +08:00
bg_draw.text((1220, 1580), "派遣全部", fill="#5680d2", font=get_font(40, '优设标题黑.ttf'))
bg_draw.text((1220, 1620), "完成时间", fill="#5680d2", font=get_font(40, '优设标题黑.ttf'))
max_time = int(max([s['remained_time'] for s in exp]))
2022-05-12 21:45:27 +08:00
if max_time == 0:
bg_draw.text((1410, 1583), "已全部完成~", fill="#5680d2",
font=get_font(60, '优设标题黑.ttf'))
else:
2022-05-12 22:10:54 +08:00
last_finish_time = datetime.datetime.now() + datetime.timedelta(seconds=max_time)
last_finish_day = last_finish_time.day > datetime.datetime.now().day and '明天' or '今天'
last_finish_str = f'{last_finish_day}{last_finish_time.strftime("%H:%M")}'
bg_draw.text((1408, 1588), last_finish_str, fill="#5680d2",
font=get_font(60, '优设标题黑.ttf'))
2022-04-30 14:13:39 +08:00
role_img = load_image(os.path.join(res_path, 'emoticons', random.choice(os.listdir(os.path.join(res_path, 'emoticons')))), size=3.5, mode='RGBA')
2022-05-12 21:45:27 +08:00
bg_img.alpha_composite(role_img, (1220, 200))
now = datetime.datetime.now().strftime('%m月%d%H:%M')
bg_draw.text((554, 1794), 'Created by LittlePaimon·' + now, fill='#5680d2', font=get_font(40, '优设标题黑.ttf'))
return MessageBuild.Image(bg_img, size=0.35, quality=70, mode='RGB')