fix: bilivideo url

This commit is contained in:
xtaodada 2024-04-04 00:42:07 +08:00
parent 8c84d4fdb5
commit 36caca3ead
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659

View File

@ -9,7 +9,7 @@ import aiofiles
from bilibili_api import HEADERS from bilibili_api import HEADERS
from bilibili_api.audio import Audio from bilibili_api.audio import Audio
from bilibili_api.video import Video, VideoDownloadURLDataDetecter, VideoQuality from bilibili_api.video import Video, VideoDownloadURLDataDetecter, VideoQuality
from httpx import AsyncClient, Response from httpx import AsyncClient, Response, URL
from pyrogram.enums import ParseMode from pyrogram.enums import ParseMode
from pyrogram.types import Message from pyrogram.types import Message
@ -24,6 +24,13 @@ LOCK = Lock()
EDIT_TEMP_SECONDS = 10.0 EDIT_TEMP_SECONDS = 10.0
MESSAGE_MAP: Dict[int, float] = {} MESSAGE_MAP: Dict[int, float] = {}
UPLOAD_MESSAGE_MAP: Dict[int, int] = {} UPLOAD_MESSAGE_MAP: Dict[int, int] = {}
CDN = [
"",
"upos-hz-mirrorakam.akamaized.net",
"upos-sz-mirroraliov.bilivideo.com",
"cn-hk-eq-01-09.bilivideo.com",
"cn-sccd-cu-01-08.bilivideo.com",
]
class BilibiliDownloaderError(Exception): class BilibiliDownloaderError(Exception):
@ -143,12 +150,20 @@ async def message_edit(
async def download_url(url: str, out: str, m: Message, start: str): async def download_url(url: str, out: str, m: Message, start: str):
for i in CDN:
if i:
u = URL(url).host
url = url.replace(u, i)
async with AsyncClient(headers=HEADERS, timeout=60) as sess: async with AsyncClient(headers=HEADERS, timeout=60) as sess:
head = await sess.head(url)
if head.status_code != 200:
if i == CDN[-1]:
raise BilibiliDownloaderError("下载链接异常,请尝试重新下载")
logger.warning(f"下载链接异常,使用 CDN {i} 失败,尝试使用其他 CDN")
continue
async with sess.stream("GET", url) as resp: async with sess.stream("GET", url) as resp:
logger.info(f"Downloading {start}") logger.info(f"Downloading {start}")
resp: Response resp: Response
if resp.status_code != 200:
raise BilibiliDownloaderError("下载链接异常,请尝试重新下载")
length = resp.headers.get("content-length") length = resp.headers.get("content-length")
if not length: if not length:
raise FileNoSize raise FileNoSize
@ -317,8 +332,10 @@ async def go_download(v: Video, p_num: int, m: Message, task: bool = True):
if len(streams) < 2: if len(streams) < 2:
raise BilibiliDownloaderError("获取下载链接异常") raise BilibiliDownloaderError("获取下载链接异常")
# MP4 流下载 # MP4 流下载
await download_url(streams[0].url, video_temp_path, m, "视频 m4s ") video_url = streams[0].url
await download_url(streams[1].url, audio_temp_path, m, "音频 m4s ") audio_url = streams[1].url
await download_url(video_url, video_temp_path, m, "视频 m4s ")
await download_url(audio_url, audio_temp_path, m, "音频 m4s ")
# 混流 # 混流
logger.info("Merging video and audio") logger.info("Merging video and audio")
_, result = await execute( _, result = await execute(