feat(yuc_wiki):增加下季新番功能并优化相关操作

This commit is contained in:
SlyAimer 2025-03-18 15:32:53 +08:00
parent 40b7afafee
commit 755fc2fb17
3 changed files with 40 additions and 15 deletions

View file

@ -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> 标签
hr_tags = soup.find_all('hr')
if len(hr_tags) >= 2:
second_hr = hr_tags[1]
# 删除第二个 <hr> 标签之后的所有内容
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},

View file

@ -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(" ")

View file

@ -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)
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("新番信息被外星人抢走啦,请重试。这绝对不是咱的错,绝对不是!")