import random import datetime from io import BytesIO import os from PIL import Image, ImageDraw, ImageFont import matplotlib.pyplot as plt from aiohttp import ClientSession from nonebot.adapters.onebot.v11 import MessageSegment from ..utils.util import pil2b64 res_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'res') # def get_font(size): # return ImageFont.truetype(os.path.join(res_path, 'msyhbd.ttc'), size) # # # def get_odd_time(seconds): # m, s = divmod(int(seconds), 60) # h, m = divmod(m, 60) # return "剩余%02d时%02d分%02d秒" % (h, m, s) # # # async def get_avater_pic(avater_url): # async with ClientSession() as session: # res = await session.get(avater_url) # res = await res.read() # avater = Image.open(BytesIO(res)).convert("RGBA").resize((60, 60)) # return avater # # # bg_card_color = {'1': '#C3B8A4', '2': '#C3B8A4', '3': '#4C74A7', '4': '#D7B599'} # # # async def draw_daily_note_card(data, uid): # if not data: # return '数据出错' # if data['retcode'] == 10102: # return '这uid没有在米游社公开信息哦,请到 个人主页-管理 中打开' # elif data['retcode'] == 10104: # return 'uid有误哦,检查一下或再手动输入一次uid吧' # elif data['retcode'] != 0: # return f'派蒙获取数据失败了,获取状态:\n{data["message"]},{data["retcode"]}' # data = data['data'] # # 载入所需素材图标 # bg_color = random.choice(list(bg_card_color.items())) # bg_img = Image.open(os.path.join(res_path, 'daily_note', f'便签背景{bg_color[0]}.png')).convert("RGBA") # enemy = Image.open(os.path.join(res_path, 'daily_note', '周本.png')).convert("RGBA") # task = Image.open(os.path.join(res_path, 'daily_note', '委托.png')).convert("RGBA") # power = Image.open(os.path.join(res_path, 'daily_note', '树脂.png')).convert("RGBA") # money = Image.open(os.path.join(res_path, 'daily_note', '洞天宝钱.png')).convert("RGBA") # send_icon = Image.open(os.path.join(res_path, 'daily_note', '派遣背景.png')).convert("RGBA").resize((110, 55)) # send_finish_icon = Image.open(os.path.join(res_path, 'daily_note', '派遣完成.png')).convert("RGBA").resize((55, 55)) # abyss = Image.open(os.path.join(res_path, 'daily_note', '深渊.png')).convert('RGBA').resize((160, 160)) # tran = Image.open(os.path.join(res_path, 'daily_note', '参量.png')).convert('RGBA').resize((40, 40)) # bg_draw = ImageDraw.Draw(bg_img) # # bg_draw.text((23, 20), '实时便笺', font=get_font(30), fill='white') # bg_draw.text((255, 20), 'UID:' + uid, font=get_font(30), fill='white') # # 树脂 # bg_img.alpha_composite(power, (120, 150)) # bg_draw.text((170, 145), f'{data["current_resin"]}/160', font=get_font(30), fill=bg_color[1]) # if data["current_resin"] == 160: # bg_draw.text((310, 144), '已回满', font=get_font(30), fill=bg_color[1]) # else: # recover_time = datetime.datetime.now() + datetime.timedelta(seconds=int(data['resin_recovery_time'])) # # recover_time_day = recover_time.day > datetime.datetime.now().day and '明天' or '今天' # recover_time_day = '今天' if recover_time.day == datetime.datetime.now().day else '明天' # recover_time_str = f'将于{recover_time_day}{recover_time.strftime("%H:%M")}回满' # bg_draw.text((320, 147), recover_time_str, font=get_font(25), fill=bg_color[1]) # # 洞天宝钱 # bg_img.alpha_composite(money, (120, 220)) # bg_draw.text((170, 220), f'{data["current_home_coin"]}/2400', font=get_font(30), fill=bg_color[1]) # if data["current_home_coin"] == 2400: # bg_draw.text((350, 219), '已存满', font=get_font(30), fill=bg_color[1]) # 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")}攒满' # # recover_time_str = f'将于{recover_time.strftime("%d日%H:%M")}攒满' # bg_draw.text((360, 222), recover_time_str, font=get_font(25), fill=bg_color[1]) # # 委托 # bg_img.alpha_composite(task, (120, 295)) # bg_draw.text((170, 297), f'{data["finished_task_num"]}/4', font=get_font(30), fill=bg_color[1]) # if data["finished_task_num"] == 4: # bg_draw.text((247, 297), '已完成', font=get_font(30), fill=bg_color[1]) # else: # bg_draw.text((245, 298), '未完成', font=get_font(25), fill=bg_color[1]) # # 周本 # bg_img.alpha_composite(enemy, (120, 370)) # bg_draw.text((170, 370), f'{data["remain_resin_discount_num"]}/3', font=get_font(30), fill=bg_color[1]) # if data["remain_resin_discount_num"] == 0: # bg_draw.text((247, 369), '已完成', font=get_font(30), fill=bg_color[1]) # else: # bg_draw.text((245, 372), '未完成', font=get_font(25), fill=bg_color[1]) # # 参量质变仪 # bg_img.alpha_composite(tran, (360, 297)) # if not data['transformer']['obtained']: # bg_draw.text((413, 298), '未获得', font=get_font(25), fill=bg_color[1]) # else: # if data['transformer']['recovery_time']['reached']: # bg_draw.text((415, 297), '已可用', font=get_font(30), fill=bg_color[1]) # else: # bg_draw.text((413, 298), f"{data['transformer']['recovery_time']['Day']}天后", font=get_font(25), # fill=bg_color[1]) # # 深渊 # 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) + '.04:00', # '%Y.%m.%d.%H:%M') - datetime.datetime.now() # abyss_new_str = f'{abyss_new.days + 1}天后刷新' if abyss_new.days <= 8 else '已刷新' # bg_img.alpha_composite(abyss, (520, 264)) # bg_draw.text((568, 300), '深渊', font=get_font(30), fill=bg_color[1]) # if abyss_new_str == '已刷新': # bg_draw.text((561, 350), abyss_new_str, font=get_font(25), fill=bg_color[1]) # else: # bg_draw.text((540, 350), abyss_new_str, font=get_font(25), fill=bg_color[1]) # # 派遣 # h = 430 # if not data['expeditions']: # bg_draw.text((300, h + 140), '没有派遣信息', font=get_font(30), fill=bg_color[1]) # else: # for send in data['expeditions']: # send_avatar = await get_avater_pic(send['avatar_side_icon']) # send_status = '派遣已完成!' if send['status'] == 'Finished' else get_odd_time(send['remained_time']) # bg_draw.rectangle((145, h, 645, h + 55), fill=None, outline=bg_color[1], width=3) # if send['status'] == 'Finished': # bg_img.alpha_composite(send_finish_icon, (590, h)) # bg_img.alpha_composite(send_icon, (150, h)) # bg_img.alpha_composite(send_avatar, (150, h - 10)) # if send_status == '派遣已完成!': # bg_draw.text((329, h + 10), send_status, font=get_font(25), fill=bg_color[1]) # else: # bg_draw.text((300, h + 10), send_status, font=get_font(25), fill=bg_color[1]) # h += 57 # last_finish_second = int(max([s['remained_time'] for s in data['expeditions']])) # if last_finish_second != 0: # last_finish_time = datetime.datetime.now() + datetime.timedelta(seconds=last_finish_second) # 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((211, h + 3.5), last_finish_str, font=get_font(30), fill=bg_color[1]) # else: # bg_draw.text((290, h + 3.5), '派遣已全部完成', font=get_font(30), fill=bg_color[1]) # bg_draw.text((274, 797), 'Created by 惜月の小派蒙', font=get_font(20), fill=bg_color[1]) # # bg_img = pil2b64(bg_img, 70) # bg_img = MessageSegment.image(bg_img) # return bg_img def get_font(size, font='msyhbd.ttc'): return ImageFont.truetype(os.path.join(res_path, font), size) async def get_avatar_pic(avatar_url): async with ClientSession() as session: res = await session.get(avatar_url) res = await res.read() avatar = Image.open(BytesIO(res)).convert("RGBA").resize((135, 135)) return avatar async def draw_ring(per): 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') return img async def draw_daily_note_card(data, uid): if not data: return '数据出错' if data['retcode'] == 10102: return '这uid没有在米游社公开信息哦,请到 个人主页-管理 中打开' elif data['retcode'] == 10104: return 'uid有误哦,检查一下或再手动输入一次uid吧' elif data['retcode'] != 0: return f'派蒙获取数据失败了,获取状态:\n{data["message"]},{data["retcode"]}' data = data['data'] circle_img = Image.open(os.path.join(res_path, 'daily_note', '透明圆.png')) finished_icon = Image.open(os.path.join(res_path, 'daily_note', 'finished.png')) bg_img = Image.open(os.path.join(res_path, 'daily_note', 'ssbq.png')).convert("RGBA") bg_draw = ImageDraw.Draw(bg_img) # 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')) 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")}回满' bg_draw.text((780, 480), recover_time_str, fill='white', font=get_font(40, '优设标题黑.ttf')) # 宝钱文字 bg_draw.text((337, 701), f"{data['current_home_coin']}/2400", fill='white', font=get_font(48, 'number.ttf')) bg_img.alpha_composite(await draw_ring(data['current_home_coin'] / 2400), (98, 593)) if data['current_home_coin'] == 2400: bg_draw.text((820, 701), f"洞天宝钱满了哦~", fill='white', font=get_font(40, '优设标题黑.ttf')) 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")}攒满' 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')) else: 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')) else: 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')) else: 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')) # 深渊文字 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) + '.04:00', '%Y.%m.%d.%H:%M') - datetime.datetime.now() 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}天结束", fill='white', font=get_font(40, '优设标题黑.ttf')) bg_img.alpha_composite(await draw_ring(abyss_new.days / 15), (100, 1249)) # 派遣情况 exp = data['expeditions'] i = 0 for role in exp: role_avatar = await get_avatar_pic(role['avatar_side_icon']) 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': bg_img.alpha_composite(circle_img, (i * 200 + 172, 1559)) hour = int(role['remained_time']) // 3600 bg_draw.text((i * 200 + 205, 1580), f"{hour}h", fill='white', font=get_font(40, 'number.ttf')) minute = int(role['remained_time']) % 3600 // 60 bg_draw.text((i * 200 + 200, 1620), f"{minute}m", fill='white', font=get_font(40, 'number.ttf')) else: bg_img.alpha_composite(finished_icon, (i * 200 + 191, 1576)) i += 1 bg_draw.text((1210, 1580), "距离派遣全", fill="#5680d2", font=get_font(40, '优设标题黑.ttf')) bg_draw.text((1210, 1620), "部完成还有", fill="#5680d2", font=get_font(40, '优设标题黑.ttf')) max_time = int(max(exp, key=lambda x: x['remained_time'])['remained_time']) if max_time == 0: bg_draw.text((1410, 1583), "已全部完成~", fill="#5680d2", font=get_font(60, '优设标题黑.ttf')) else: bg_draw.text((1410, 1593), f"{max_time // 3600}h{max_time % 3600 // 60}m{max_time & 60}s", fill="#5680d2", font=get_font(60, 'number.ttf')) role_img = random.choice(os.listdir(os.path.join(res_path, 'emoticons'))) role_img = Image.open(os.path.join(res_path, 'emoticons', role_img)).convert('RGBA') role_img = role_img.resize((int(role_img.size[0] * 3.5), int(role_img.size[1] * 3.5)), Image.ANTIALIAS) 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')) bg_img = bg_img.resize((int(bg_img.size[0] * 0.4), int(bg_img.size[1] * 0.4)), Image.ANTIALIAS) bg_img = pil2b64(bg_img) bg_img = MessageSegment.image(bg_img) return bg_img