mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-06-09 11:27:26 +08:00
feat(bilibili):增加视频清晰度参数,可以通过BILI_QN更改下载的视频清晰度;
在 BilibiliClient 中添加视频质量配置并改进错误处理,修复下载请求被 302 重定向到 CDN,旧代码未跟随重定向且只接受 “OK” ,导致失败,现在即便是低清晰度/CDN 跳转的链接也能正常下载。
This commit is contained in:
@@ -34,6 +34,11 @@ END_DAY = "2024-01-01"
|
|||||||
# 搜索模式
|
# 搜索模式
|
||||||
BILI_SEARCH_MODE = "normal"
|
BILI_SEARCH_MODE = "normal"
|
||||||
|
|
||||||
|
# 视频清晰度(qn)配置,常见取值:
|
||||||
|
# 16=360p, 32=480p, 64=720p, 80=1080p, 112=1080p高码率, 116=1080p60, 120=4K
|
||||||
|
# 注意:更高清晰度需要账号/视频本身支持
|
||||||
|
BILI_QN = 80
|
||||||
|
|
||||||
# 是否爬取用户信息
|
# 是否爬取用户信息
|
||||||
CREATOR_MODE = True
|
CREATOR_MODE = True
|
||||||
|
|
||||||
|
|||||||
@@ -189,10 +189,11 @@ class BilibiliClient(AbstractApiClient):
|
|||||||
if not aid or not cid or aid <= 0 or cid <= 0:
|
if not aid or not cid or aid <= 0 or cid <= 0:
|
||||||
raise ValueError("aid 和 cid 必须存在")
|
raise ValueError("aid 和 cid 必须存在")
|
||||||
uri = "/x/player/wbi/playurl"
|
uri = "/x/player/wbi/playurl"
|
||||||
|
qn_value = getattr(config, "BILI_QN", 80)
|
||||||
params = {
|
params = {
|
||||||
"avid": aid,
|
"avid": aid,
|
||||||
"cid": cid,
|
"cid": cid,
|
||||||
"qn": 80,
|
"qn": qn_value,
|
||||||
"fourk": 1,
|
"fourk": 1,
|
||||||
"fnval": 1,
|
"fnval": 1,
|
||||||
"platform": "pc",
|
"platform": "pc",
|
||||||
@@ -201,15 +202,17 @@ class BilibiliClient(AbstractApiClient):
|
|||||||
return await self.get(uri, params, enable_params_sign=True)
|
return await self.get(uri, params, enable_params_sign=True)
|
||||||
|
|
||||||
async def get_video_media(self, url: str) -> Union[bytes, None]:
|
async def get_video_media(self, url: str) -> Union[bytes, None]:
|
||||||
async with httpx.AsyncClient(proxy=self.proxy) as client:
|
# Follow CDN 302 redirects and treat any 2xx as success (some endpoints return 206)
|
||||||
|
async with httpx.AsyncClient(proxy=self.proxy, follow_redirects=True) as client:
|
||||||
try:
|
try:
|
||||||
response = await client.request("GET", url, timeout=self.timeout, headers=self.headers)
|
response = await client.request("GET", url, timeout=self.timeout, headers=self.headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
if not response.reason_phrase == "OK":
|
if 200 <= response.status_code < 300:
|
||||||
utils.logger.error(f"[BilibiliClient.get_video_media] request {url} err, res:{response.text}")
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return response.content
|
return response.content
|
||||||
|
utils.logger.error(
|
||||||
|
f"[BilibiliClient.get_video_media] Unexpected status {response.status_code} for {url}"
|
||||||
|
)
|
||||||
|
return None
|
||||||
except httpx.HTTPError as exc: # some wrong when call httpx.request method, such as connection error, client error, server error or response status code is not 2xx
|
except httpx.HTTPError as exc: # some wrong when call httpx.request method, such as connection error, client error, server error or response status code is not 2xx
|
||||||
utils.logger.error(f"[BilibiliClient.get_video_media] {exc.__class__.__name__} for {exc.request.url} - {exc}") # 保留原始异常类型名称,以便开发者调试
|
utils.logger.error(f"[BilibiliClient.get_video_media] {exc.__class__.__name__} for {exc.request.url} - {exc}") # 保留原始异常类型名称,以便开发者调试
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user