diff --git a/uma_tasks/__init__.py b/uma_tasks/__init__.py index ead075f..b45feb1 100644 --- a/uma_tasks/__init__.py +++ b/uma_tasks/__init__.py @@ -1,10 +1,11 @@ -import os -import json import base64 +import json +import os from hoshino import Service, priv, R -from .update_tasks import del_img, update_info, del_img + from .generate import get_title, get_task_info +from .update_tasks import del_img, update_info, del_img from ..plugin_utils.base_util import get_img_cq current_dir = os.path.join(os.path.dirname(__file__), f'tasks_config.json') @@ -15,17 +16,19 @@ s = base64_data.decode() sv.help = f'![](data:image/jpeg;base64,{s})' + @sv.on_fullmatch('马娘限时任务帮助') async def get_help(bot, ev): img_path = os.path.join(os.path.dirname(__file__), f'{sv.name}_help.png') sv_help = await get_img_cq(img_path) await bot.send(ev, sv_help) + @sv.on_rex(r'^限时任务(\S{1,3})$') async def check_meanings(bot, ev): task_id = ev['match'].group(1) - with open(current_dir, 'r', encoding='UTF-8') as f: - f_data = json.load(f) + with open(current_dir, 'r', encoding='UTF-8') as file: + f_data = json.load(file) if task_id == '列表': task_list = [] for task_id_tmp in list(f_data['tasks'].keys()): @@ -38,11 +41,12 @@ async def check_meanings(bot, ev): except: return number = int(f_data['number']) - if task_id not in range(1, number+1): + if task_id not in range(1, number + 1): await bot.finish(ev, f'未找到此编号的限时任务:{task_id}\n目前支持 1-{number}') msg = await get_task_info(str(task_id), f_data) await bot.send(ev, msg) + # 手动更新本地数据 @sv.on_fullmatch('手动更新限时任务') async def force_update(bot, ev): @@ -55,4 +59,4 @@ async def force_update(bot, ev): await del_img(R.img('umamusume').path) await bot.send(ev, '限时任务信息刷新完成') except Exception as e: - await bot.send(ev, f'限时任务信息刷新失败:{e}') \ No newline at end of file + await bot.send(ev, f'限时任务信息刷新失败:{e}') diff --git a/uma_tasks/generate.py b/uma_tasks/generate.py index 411819e..da9ae72 100644 --- a/uma_tasks/generate.py +++ b/uma_tasks/generate.py @@ -1,10 +1,12 @@ -from prettytable import PrettyTable -from PIL import Image, ImageDraw, ImageFont import os +from PIL import Image, ImageDraw, ImageFont from hoshino import R, logger +from prettytable import PrettyTable + from ..plugin_utils.base_util import get_img_cq + # 获取限时任务列表 async def get_title(f_data): info_data = { @@ -25,6 +27,7 @@ async def get_title(f_data): msg = await get_img_cq(img_dir) return msg + # 获取限时任务内容 async def get_task_info(task_id, f_data): title = f_data['tasks'][task_id]['title'] @@ -36,8 +39,10 @@ async def get_task_info(task_id, f_data): info_data['info'][s_task_id] = {} info_data['info'][s_task_id]['task_name'] = f_data['tasks'][task_id]['task_list'][s_task_id]['任务名'] info_data['info'][s_task_id]['condition'] = f_data['tasks'][task_id]['task_list'][s_task_id]['达成条件'] - info_data['info'][s_task_id]['race_time'] = f_data['tasks'][task_id]['task_list'][s_task_id].get('比赛时间', '无') - info_data['info'][s_task_id]['race_env'] = f_data['tasks'][task_id]['task_list'][s_task_id].get('比赛环境', '无') + info_data['info'][s_task_id]['race_time'] = f_data['tasks'][task_id]['task_list'][s_task_id].get('比赛时间', + '无') + info_data['info'][s_task_id]['race_env'] = f_data['tasks'][task_id]['task_list'][s_task_id].get('比赛环境', + '无') info_data['info'][s_task_id]['suggest_uma'] = f_data['tasks'][task_id]['task_list'][s_task_id]['推荐赛马娘'] info_data['info'][s_task_id]['reward'] = f_data['tasks'][task_id]['task_list'][s_task_id]['奖励'] img_dir = os.path.join(R.img('umamusume').path, f'uma_tasks/task_id_{task_id}.png') @@ -51,6 +56,7 @@ async def get_task_info(task_id, f_data): msg = await get_img_cq(img_dir) return msg + # 生成图片 async def create_img(is_title, info_data, filename_tmp): if is_title: @@ -58,36 +64,36 @@ async def create_img(is_title, info_data, filename_tmp): else: field_names = ('任务名', '达成条件', '比赛时间', '比赛环境', '推荐赛马娘', '奖励') titles = info_data['title'] - table = PrettyTable(field_names = field_names, title = titles) + table = PrettyTable(field_names=field_names, title=titles) - for id in list(info_data['info'].keys()): + for task_id in list(info_data['info'].keys()): if is_title: - task_name = info_data['info'][id] - table.add_row([id, task_name]) + task_name = info_data['info'][task_id] + table.add_row([task_id, task_name]) else: - task_name = info_data['info'][id]['task_name'] - condition = info_data['info'][id]['condition'] - race_time = info_data['info'][id]['race_time'] - race_env = info_data['info'][id]['race_env'] - suggest_uma = info_data['info'][id]['suggest_uma'] - reward = info_data['info'][id]['reward'] + task_name = info_data['info'][task_id]['task_name'] + condition = info_data['info'][task_id]['condition'] + race_time = info_data['info'][task_id]['race_time'] + race_env = info_data['info'][task_id]['race_env'] + suggest_uma = info_data['info'][task_id]['suggest_uma'] + reward = info_data['info'][task_id]['reward'] table.add_row([task_name, condition, race_time, race_env, suggest_uma, reward]) table_info = str(table) space = 5 current_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'simhei.ttf') font = ImageFont.truetype(current_dir, 20, encoding='utf-8') - im = Image.new('RGB',(10, 10),(255, 255, 255, 0)) + im = Image.new('RGB', (10, 10), (255, 255, 255, 0)) draw = ImageDraw.Draw(im, 'RGB') img_size = draw.multiline_textsize(table_info, font=font) - im_new = im.resize((img_size[0]+space*2, img_size[1]+space*2)) + im_new = im.resize((img_size[0] + space * 2, img_size[1] + space * 2)) del draw del im draw = ImageDraw.Draw(im_new, 'RGB') - draw.multiline_text((space,space), table_info, fill=(0, 0, 0), font=font) + draw.multiline_text((space, space), table_info, fill=(0, 0, 0), font=font) save_dir = os.path.join(R.img('umamusume').path, 'uma_tasks/') if not os.path.exists(save_dir): os.mkdir(save_dir) path_dir = os.path.join(save_dir, filename_tmp) im_new.save(path_dir, 'PNG') - del draw \ No newline at end of file + del draw diff --git a/uma_tasks/update_init.py b/uma_tasks/update_init.py index f8d83b7..9ef6876 100644 --- a/uma_tasks/update_init.py +++ b/uma_tasks/update_init.py @@ -1,11 +1,14 @@ -import os import json +import os from hoshino import logger, R -from .update_tasks import del_img, update_info, judge_update, del_img + +from .update_tasks import update_info, judge_update, del_img # 启动时自动更新至最新版限时任务信息 current_dir = os.path.join(os.path.dirname(__file__), f'tasks_config.json') + + async def update(): if not os.path.exists(current_dir): logger.info('====未检测到马娘限时任务信息文件,正在开始创建文件和更新信息====') @@ -18,6 +21,7 @@ async def update(): except Exception as e: logger.info(f'====限时任务信息更新失败:{e}====') + async def auto_update(): flag = await judge_update() if not flag: @@ -29,4 +33,4 @@ async def auto_update(): await del_img(R.img('umamusume').path) logger.info('限时任务信息刷新完成') except Exception as e: - logger.error(f'限时任务信息刷新失败:{e}') \ No newline at end of file + logger.error(f'限时任务信息刷新失败:{e}') diff --git a/uma_tasks/update_tasks.py b/uma_tasks/update_tasks.py index 565ae34..377c97c 100644 --- a/uma_tasks/update_tasks.py +++ b/uma_tasks/update_tasks.py @@ -1,15 +1,16 @@ -from bs4 import BeautifulSoup -import os import json +import os import re -from datetime import datetime import shutil +from datetime import datetime +from bs4 import BeautifulSoup from hoshino import aiorequests url = 'https://wiki.biligame.com/umamusume/期间限定任务' current_dir = os.path.join(os.path.dirname(__file__), f'tasks_config.json') + # 获取标题列表 async def get_title_list(soup): title_list = [] @@ -18,6 +19,7 @@ async def get_title_list(soup): title_list.append(title.text.replace('\u3000', '')) return title_list + # 获取最新的更新时间 async def get_update_time(): update_url = 'https://wiki.biligame.com/umamusume/index.php?title=期间限定任务&action=history' @@ -25,9 +27,11 @@ async def get_update_time(): soup = BeautifulSoup(await rep.text, 'lxml') last_time_tmp = soup.find('a', {'class': 'mw-changeslist-date'}).text.replace(' ', '') group = re.search(r'^([0-9]{4})年([0-9]{1,2})月([0-9]{1,2})日\S*([0-9]{2}):([0-9]{2})$', last_time_tmp) - last_time = datetime(int(group.group(1)), int(group.group(2)), int(group.group(3)), int(group.group(4)), int(group.group(5))) + last_time = datetime(int(group.group(1)), int(group.group(2)), int(group.group(3)), int(group.group(4)), + int(group.group(5))) return last_time + # 更新数据 async def update_info(): rep = await aiorequests.get(url, timeout=10) @@ -88,6 +92,7 @@ async def update_info(): json.dump(f_data, f, indent=4, ensure_ascii=False) break + # 判断是否有更新 async def judge_update(): last_time = await get_update_time() @@ -99,6 +104,7 @@ async def judge_update(): else: return False + # 若有更新就删除已经生成过的所有图片 async def del_img(root_path): path = os.path.join(root_path, 'uma_tasks/') @@ -106,4 +112,4 @@ async def del_img(root_path): shutil.rmtree(path) os.mkdir(path) else: - os.mkdir(path) \ No newline at end of file + os.mkdir(path)