diff --git a/dota.py b/dota.py index 150e743..03e6693 100644 --- a/dota.py +++ b/dota.py @@ -176,6 +176,23 @@ class Friend(BaseModel): 'timestamp': end_time, 'url': f"https://www.opendota.com/matches/{match_['match_id']}", }) + + # 生成图片报告 + try: + image_url = image_generator.generate_recent_matches_image(name, matches[:limit]) + except Exception as e: + logger.error(f"生成最近比赛报告图片失败: {str(e)}") + image_url = None + + # 如果成功生成了图片,添加到第一个embed中 + if image_url: + data['embeds'].append({ + 'image': { + 'url': image_url + }, + 'color': 3447003 # 蓝色 + }) + return data diff --git a/image_generator.py b/image_generator.py index 08751dc..ae22100 100644 --- a/image_generator.py +++ b/image_generator.py @@ -3,6 +3,7 @@ from jinja2 import Environment, FileSystemLoader import utils import json import os +import datetime class ImageGenerator: @@ -132,4 +133,84 @@ class ImageGenerator: image_url = utils.upload_image(image_path, image_path) os.remove(image_path) return image_url - return None \ No newline at end of file + return None + + def generate_recent_matches_image(self, player_name, matches): + """ + 生成玩家最近比赛的图片报告 + + Args: + player_name: 玩家名称 + matches: 比赛数据列表 + + Returns: + str: 上传后的图片URL + """ + # 处理比赛数据 + processed_matches = [] + + for match in matches: + # 获取英雄图片 + hero_id = str(match['hero_id']) + hero_img = f"https://cdn.dota2.com/apps/dota2/images/heroes/{self.heroes_data[hero_id]['name'].replace('npc_dota_hero_', '')}_full.png" + + # 格式化时间 + end_time = datetime.datetime.fromtimestamp(match['end_time']).strftime('%Y-%m-%d %H:%M') + + # 格式化时长 + duration_hms = utils.convert_seconds_to_hms(match['duration']) + duration_formatted = f"{duration_hms[0]}:{duration_hms[1]:02d}:{duration_hms[2]:02d}" + + # 获取英雄中文名 + hero_name = utils.get_hero_chinese_name(self.heroes_data[hero_id]['name']) + + processed_match = { + 'win': match['win'], + 'kills': match['kills'], + 'deaths': match['deaths'], + 'assists': match['assists'], + 'hero_img': hero_img, + 'hero_name': hero_name, + 'duration_formatted': duration_formatted, + 'end_time_formatted': end_time, + 'party_size': match['party_size'], + 'average_rank': match['average_rank'] + } + + processed_matches.append(processed_match) + + # 渲染模板 + template = self.env.get_template('recent_matches.html') + html_content = template.render( + player_name=player_name, + matches=processed_matches + ) + + # 使用Playwright生成图片 + with sync_playwright() as playwright: + browser = playwright.chromium.launch() + page = browser.new_page() + page.set_content(html_content) + page.set_viewport_size({"width": 800, "height": 800}) + + # 等待内容完全加载 + page.wait_for_timeout(1000) + + # 调整截图高度以适应内容 + body_height = page.evaluate('document.body.scrollHeight') + body_width = page.evaluate('document.body.offsetWidth') + page.set_viewport_size({"width": body_width, "height": body_height}) + + # 截图 + image_path = f"recent_matches_{player_name.replace(' ', '_')}_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.png" + page.screenshot(path=image_path, full_page=True) + browser.close() + + # 上传图片 + image_url = utils.upload_image(image_path) + + # 删除本地文件 + os.remove(image_path) + + return image_url + \ No newline at end of file diff --git a/templates/recent_matches.html b/templates/recent_matches.html new file mode 100644 index 0000000..385fb40 --- /dev/null +++ b/templates/recent_matches.html @@ -0,0 +1,171 @@ + + +
+ + + + + +