From 5dbed47a761fbc4f8eb7bb179a715a11d2159062 Mon Sep 17 00:00:00 2001 From: azmiao <2362020227@qq.com> Date: Thu, 18 Apr 2024 11:25:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A9=AC=E5=A8=98=E7=9B=AE=E6=A0=87=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uma_target/__init__.py | 54 ++++++++++++++++++++-------------------- uma_target/get_target.py | 41 ++++++++++++++++-------------- 2 files changed, 50 insertions(+), 45 deletions(-) diff --git a/uma_target/__init__.py b/uma_target/__init__.py index a722100..4e31f2b 100644 --- a/uma_target/__init__.py +++ b/uma_target/__init__.py @@ -1,10 +1,12 @@ -import os -import json import base64 +import json +import os from hoshino import Service + from .get_target import get_tar from ..plugin_utils.base_util import get_img_cq +from ..uma_info.info_utils import * sv = Service('uma_target') with open(os.path.join(os.path.dirname(__file__), f'{sv.name}_help.png'), 'rb') as f: @@ -12,37 +14,35 @@ s = base64_data.decode() sv.help = f'![](data:image/jpeg;base64,{s})' +config_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'uma_info') +current_dir = os.path.join(config_dir, 'config_v2.json') + + @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_prefix('查目标') async def search_target(bot, ev): - uma_name_tmp = str(ev.message).replace('-f', '') - is_force = True if str(ev.message).endswith('-f') else False - current_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'uma_info/config.json') - with open(current_dir, 'r', encoding = 'UTF-8') as f: - f_data = json.load(f) - with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'uma_info/replace_dict.json'), 'r', encoding = 'UTF-8') as af: - replace_data = json.load(af) - uma_target = '' - name_list = list(f_data.keys()) - name_list.remove('current_chara') - for uma_name in name_list: - if f_data[uma_name]['category'] == 'umamusume': - other_name_list = list(replace_data[uma_name]) if uma_name in replace_data else [] - if f_data[uma_name]['cn_name']: - cn_name = f_data[uma_name]['cn_name'] - else: - continue - if str(uma_name) == uma_name_tmp or str(cn_name) == uma_name_tmp or\ - str(f_data[uma_name]['jp_name']) == uma_name_tmp or str(uma_name_tmp) in other_name_list: - try: - uma_target = await get_tar(cn_name, is_force) - except: - await bot.finish(ev, f'这只马娘不存在或暂时没有育成目标') + uma_name_tmp = str(ev.message).strip().replace('-f', '') + is_force = True if str(ev.message).strip().endswith('-f') else False + + with open(current_dir, 'r', encoding='UTF-8') as file: + f_data = json.load(file) + rep_dir = os.path.join(config_dir, 'replace_dict.json') + with open(rep_dir, 'r', encoding='UTF-8') as file: + replace_data = json.load(file) + + uma = await query_uma_by_name(uma_name_tmp, f_data, replace_data) + if not uma: + return + + uma_target = await get_tar(uma.cn_name, is_force) if not uma_target: - await bot.finish(ev, f'这只马娘不存在或暂时没有育成目标') - await bot.send(ev, uma_target) \ No newline at end of file + msg = f'马娘 [{uma.cn_name}] 暂时没有育成模板呢' + else: + msg = uma_target + await bot.send(ev, msg) diff --git a/uma_target/get_target.py b/uma_target/get_target.py index 475d445..b340517 100644 --- a/uma_target/get_target.py +++ b/uma_target/get_target.py @@ -1,35 +1,38 @@ import os + import httpx -from bs4 import BeautifulSoup from PIL import Image, ImageDraw, ImageFont, ImageColor - +from bs4 import BeautifulSoup from hoshino import R, logger + from ..plugin_utils.base_util import get_img_cq + # 获取育成目标的数据 async def get_tar_data(uma_name): url = f'https://wiki.biligame.com/umamusume/{uma_name}' res = httpx.get(url, timeout=10) soup = BeautifulSoup(res.text, 'lxml') - target_tmp = soup.find_all('table', {"style":"width:100%;margin:0px"})[-1] + target_tmp = soup.find_all('table', {"style": "width:100%;margin:0px"})[-1] row_list = target_tmp.find_all('tr') data, i = {}, 0 - while (i <= 44 and row_list[i+1].find('td').text.strip()): - race = row_list[i+3].find('td').text.strip().replace('\t', ' ').replace(' ', ' ').replace('\n', ' | ')\ + while i <= 44 and row_list[i + 1].find('td').text.strip(): + race = row_list[i + 3].find('td').text.strip().replace('\t', ' ').replace(' ', ' ').replace('\n', ' | ') \ .replace('\xa0', ' ') data[row_list[i].text.strip()] = { - row_list[i+1].find('th').text.strip(): row_list[i+1].find('td').text.strip(), - row_list[i+2].find('th').text.strip(): row_list[i+2].find('td').text.strip(), - row_list[i+3].find('th').text.strip(): race + row_list[i + 1].find('th').text.strip(): row_list[i + 1].find('td').text.strip(), + row_list[i + 2].find('th').text.strip(): row_list[i + 2].find('td').text.strip(), + row_list[i + 3].find('th').text.strip(): race } i += 4 return data + # 制作图片 async def generate_img(uma_name): data = await get_tar_data(uma_name) target_list = list(data.keys()) - end_img = Image.new('RGB', (1800, 136+309*len(target_list))) + end_img = Image.new('RGB', (1800, 136 + 309 * len(target_list))) title_img = Image.open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'title.png'))).convert("RGBA") target_img = Image.open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'target.png'))).convert("RGBA") end_img.paste(title_img, (0, 0)) @@ -43,19 +46,21 @@ async def generate_img(uma_name): # 调整居中 img_size = draw_img.multiline_textsize(target, font=img_font) start_width = 900 - img_size[0] / 2 - if start_width < 0 : start_width = 15.45 - draw_img.text(xy=(start_width, all_height+15.45), text=target, font=img_font, fill=font_rgb) - draw_img.text(xy=(135, all_height+92.7), text='时间', font=img_font, fill=font_rgb) - draw_img.text(xy=(135, all_height+169.95), text='条件', font=img_font, fill=font_rgb) - draw_img.text(xy=(80, all_height+247.2), text='比赛描述', font=img_font, fill=font_rgb) - draw_img.text(xy=(380, all_height+92.7), text=data[target]['时间'], font=img_font, fill=font_rgb) - draw_img.text(xy=(380, all_height+169.95), text=data[target]['条件'], font=img_font, fill=font_rgb) + if start_width < 0: + start_width = 15.45 + draw_img.text(xy=(start_width, all_height + 15.45), text=target, font=img_font, fill=font_rgb) + draw_img.text(xy=(135, all_height + 92.7), text='时间', font=img_font, fill=font_rgb) + draw_img.text(xy=(135, all_height + 169.95), text='条件', font=img_font, fill=font_rgb) + draw_img.text(xy=(80, all_height + 247.2), text='比赛描述', font=img_font, fill=font_rgb) + draw_img.text(xy=(380, all_height + 92.7), text=data[target]['时间'], font=img_font, fill=font_rgb) + draw_img.text(xy=(380, all_height + 169.95), text=data[target]['条件'], font=img_font, fill=font_rgb) if uma_name == '大和赤骥' and target.startswith('目标4'): img_font = ImageFont.truetype(font_path, 32, index=0) - draw_img.text(xy=(380, all_height+247.2), text=data[target]['比赛描述'], font=img_font, fill=font_rgb) + draw_img.text(xy=(380, all_height + 247.2), text=data[target]['比赛描述'], font=img_font, fill=font_rgb) all_height += 309 return end_img + # 返回目标图片 async def get_tar(uma_name, is_force): if not os.path.exists(os.path.join(R.img('umamusume').path, f'uma_target/')): @@ -67,4 +72,4 @@ async def get_tar(uma_name, is_force): logger.info(f'{uma_name}的育成目标图片不存在,现已成功生成!') logger.info(f'{uma_name}的育成目标图片已存在,即将发送图片!') img_ = await get_img_cq(img_path) - return img_ \ No newline at end of file + return img_