From 755fc2fb17442ee0972638167aa0909599c28e9b Mon Sep 17 00:00:00 2001 From: SlyAimer <2289782085@qq.com> Date: Tue, 18 Mar 2025 15:32:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(yuc=5Fwiki):=E5=A2=9E=E5=8A=A0=E4=B8=8B?= =?UTF-8?q?=E5=AD=A3=E6=96=B0=E7=95=AA=E5=8A=9F=E8=83=BD=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clover_yuc_wiki/yuc_wiki.py | 40 ++++++++++++++++++++++++--------- src/plugins/check.py | 4 ++-- src/plugins/yuc_wiki.py | 11 +++++++-- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/clover_yuc_wiki/yuc_wiki.py b/src/clover_yuc_wiki/yuc_wiki.py index 3e26ea3..4180ed4 100644 --- a/src/clover_yuc_wiki/yuc_wiki.py +++ b/src/clover_yuc_wiki/yuc_wiki.py @@ -3,7 +3,7 @@ from pathlib import Path import requests from os import getcwd from bs4 import BeautifulSoup -from datetime import datetime +from datetime import datetime,timedelta from nonebot_plugin_htmlrender import template_to_pic from src.configs.path_config import yuc_wiki_path @@ -14,13 +14,20 @@ async def get_yuc_wiki(keyword): """ 获取当季动漫 """ + template_name,response = '','' try: if keyword == '本季新番': template_name = await generate_season_url() - response = requests.get(base_url + f'{template_name}', timeout=10) + response = requests.get(base_url + f'{template_name}') + elif keyword == '下季新番': + template_name = await generate_next_season_url() + response = requests.get(base_url + f'{template_name}') else: template_name = 'forecast_anime' - response = requests.get(new, timeout=10) + response = requests.get(new) + + if response.status_code != 200: + return None soup = await dispose_html(response) with open(yuc_wiki_path+f'{template_name}.html', 'w', encoding='utf-8') as f: @@ -40,6 +47,19 @@ async def generate_season_url(): quarter_month = ((now.month - 1) // 3) * 3 + 1 return f"{now.year}{quarter_month:02d}" +async def generate_next_season_url(): + """ + 获取下季度 + :return: + """ + now = datetime.now() + quarter_month = ((now.month - 1) // 3) * 3 + 1 + current_quarter_start = datetime(now.year, quarter_month, 1) + + next_quarter_start = current_quarter_start + timedelta(days=90) + next_quarter_month = ((next_quarter_start.month - 1) // 3) * 3 + 1 + return f"{next_quarter_start.year}{next_quarter_month:02d}" + async def dispose_html(response): """ 处理html @@ -48,32 +68,30 @@ async def dispose_html(response): """ response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') - # 删除第一个 table 标签 + first_table = soup.select_one('table') if first_table: first_table.decompose() - # 批量处理需要删除的标签 + for tag in soup.select('header, aside'): tag.decompose() - # 查找第二个
标签 hr_tags = soup.find_all('hr') if len(hr_tags) >= 2: second_hr = hr_tags[1] - # 删除第二个
标签之后的所有内容 + next_element = second_hr.next_sibling while next_element: next_sibling = next_element.next_sibling next_element.extract() next_element = next_sibling - # 统一处理资源路径 for tag in soup.find_all(['a', 'link', 'img', 'script', 'source']): - # 处理图片懒加载 + if tag.name == 'img' and tag.get('data-src'): tag['src'] = tag['data-src'] del tag['data-src'] - # 统一修正路径 + attr = 'href' if tag.name in ['a', 'link'] else 'src' if tag.has_attr(attr): path = tag[attr].lstrip('/\\') @@ -94,7 +112,7 @@ async def get_yuc_wiki_image(template_name,width,height): template_path=yuc_wiki_path, template_name=f'{template_name}.html', templates={"data": None}, - quality=90, + quality=50, type="jpeg", pages={ "viewport": {"width": width, "height": height}, diff --git a/src/plugins/check.py b/src/plugins/check.py index c850c65..179ef33 100644 --- a/src/plugins/check.py +++ b/src/plugins/check.py @@ -10,11 +10,11 @@ from src.clover_sqlite.models.user import UserList menu = ["/重启","/今日运势","/今日塔罗","/图","/随机图","/搜番","/日报","/点歌","/摸摸头","/群老婆","/今日老婆", "/开启ai","/关闭ai","/角色列表","/添加人设", "/更新人设", "/删除人设", "/切换人设", "/管理员注册", "/待办", "/test","/天气","我喜欢你", "❤", "/待办查询", "/新建待办", "/删除待办" ,"/cf","/B站搜索", "/BV搜索", "/喜报", "/悲报", "/luxun","/鲁迅说", - "/奶龙", "/repo", "/info", "/menu", "/轻小说","/本季新番","/新番观察","/绝对色感"] + "/奶龙", "/repo", "/info", "/menu", "/轻小说","/本季新番","/下季新番","/新番观察","/绝对色感"] send_menu = ["/menu","/今日运势","/今日塔罗","/图","/随机图","搜番","/日报","/点歌","/摸摸头","/群老婆","/待办","/天气", "/待办查询", "/新建待办", "/删除待办" ,"/cf","/B站搜索", "/BV搜索", "/喜报", "/悲报","/鲁迅说", - "/轻小说","/本季新番","/新番观察","/绝对色感"] + "/轻小说","/本季新番","/新番观察","/下季新番","/绝对色感"] async def check_value_in_menu(message: MessageEvent) -> bool: value = message.get_plaintext().strip().split(" ") diff --git a/src/plugins/yuc_wiki.py b/src/plugins/yuc_wiki.py index 062f16a..b8669c8 100644 --- a/src/plugins/yuc_wiki.py +++ b/src/plugins/yuc_wiki.py @@ -1,4 +1,6 @@ from pathlib import Path + +import nonebot from nonebot.rule import to_me from nonebot.plugin import on_command from nonebot.adapters.qq import MessageSegment,MessageEvent @@ -6,9 +8,14 @@ from src.clover_yuc_wiki.yuc_wiki import get_yuc_wiki -yuc_wiki = on_command("本季新番",aliases={'新番观察'} ,rule=to_me(), priority=10, block=True) +yuc_wiki = on_command("本季新番",aliases={'下季新番','新番观察'} ,rule=to_me(), priority=10, block=True) @yuc_wiki.handle() async def handle_function(message: MessageEvent): keyword = message.get_plaintext().replace("/", "").strip(" ") yuc_wiki_image = await get_yuc_wiki(keyword) - await yuc_wiki.finish(MessageSegment.file_image(Path(yuc_wiki_image))) + if yuc_wiki_image is None: + await yuc_wiki.finish("暂无新番信息") + try: + await yuc_wiki.finish(MessageSegment.file_image(Path(yuc_wiki_image))) + except Exception: + await yuc_wiki.finish("新番信息被外星人抢走啦,请重试。这绝对不是咱的错,绝对不是!")