-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic.py
More file actions
105 lines (87 loc) · 4.35 KB
/
Copy pathpublic.py
File metadata and controls
105 lines (87 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import time
import zipfile
from pathlib import Path
from typing import List, Set, Union
from astrbot.api import logger
import aiofiles
import httpx
from .api import update_pl
from .libraries.image import *
async def check_mai(force: bool = False): # noqa: FBT001
"""检查mai资源"""
await update_pl() # 获取json文件
if not Path(STATIC).joinpath("mai/pic").exists() or force:
logger.info("初次使用,正在尝试自动下载资源\n资源包大小预计90M")
try:
async with httpx.AsyncClient() as client:
async with client.stream("GET", "https://www.diving-fish.com/maibot/static.zip") as response:
total_size = int(response.headers["Content-Length"])
downloaded_size = 0
last_update_time = time.time()
async with aiofiles.open("static.zip", "wb") as f:
async for chunk in response.aiter_bytes():
await f.write(chunk)
downloaded_size += len(chunk)
current_time = time.time()
if current_time - last_update_time >= 1:
progress = downloaded_size / total_size * 100
logger.info(f"下载进度: {progress:.2f}%")
last_update_time = current_time
logger.info("已成功下载,正在尝试解压mai资源")
with zipfile.ZipFile("static.zip", "r") as zip_file:
zip_file.extractall(Path("data/maimai"))
logger.info("mai资源已完整,尝试删除缓存")
Path("static.zip").unlink() # 删除下载的压缩文件
msg = "mai资源下载成功,请使用【mai帮助】获取指令"
except Exception as e:
logger.warning(f"自动下载出错\n{e}\n请自行尝试手动下载")
msg = f"自动下载出错\n{e}\n请自行尝试手动下载"
return msg
logger.info("已经成功下载,无需重复下载")
return "已经成功下载,无需重复下载"
import asyncio
from bs4 import BeautifulSoup
COVER_URL = "https://www.diving-fish.com/covers/"
async def download_cover(client: httpx.AsyncClient, url: str, path: Path):
"""下载单个封面文件"""
try:
response = await client.get(url)
if response.status_code == 200:
async with aiofiles.open(path, "wb") as f:
await f.write(response.content)
return True
return False
except Exception as e:
logger.error(f"下载封面失败: {url}, 错误: {e}")
return False
async def update_covers():
"""检查并更新缺失的封面图片"""
cover_dir = Path("data/maimai/mai/cover")
if not cover_dir.exists():
cover_dir.mkdir(parents=True, exist_ok=True)
logger.info(f"创建封面目录: {cover_dir}")
try:
async with httpx.AsyncClient() as client:
response = await client.get(COVER_URL)
if response.status_code != 200:
logger.error(f"无法访问封面索引页面: {COVER_URL}")
return
soup = BeautifulSoup(response.text, 'html.parser')
remote_files = {a['href'] for a in soup.find_all('a') if a['href'].endswith('.png')}
local_files = {f.name for f in cover_dir.glob('*.png')}
missing_files = remote_files - local_files
if not missing_files:
logger.info("所有封面文件均已为最新,无需更新。")
return
logger.info(f"发现 {len(missing_files)} 个缺失的封面,开始下载...")
tasks = []
async with httpx.AsyncClient() as client:
for filename in missing_files:
url = f"{COVER_URL}{filename}"
path = cover_dir / filename
tasks.append(download_cover(client, url, path))
results = await asyncio.gather(*tasks)
successful_downloads = sum(1 for r in results if r)
logger.info(f"封面更新完成。成功下载 {successful_downloads} / {len(missing_files)} 个文件。")
except Exception as e:
logger.error(f"更新封面时发生未知错误: {e}")