mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-10-09 02:08:11 +08:00
feat(ks): 支持分享短链 /f/<token> 形式的视频输入
快手分享短链 https://www.kuaishou.com/f/X9Idt15MQb9L2cv 路径里是 share_token 而不是视频 ID,它只做 302 跳转,真实地址在 Location 里。原先的解析器只认 /short-video/<id> 和纯 ID,遇到短链会抛 ValueError 被 continue 掉——只有一行 ERROR 日志,看起来像"爬了但没数据"。 - help.py: 新增 /f/<token> 分支,返回 url_type="short"(token 不是视频 ID, 必须跟随重定向),并补上第三种形式的文档 - client.py: 新增 resolve_short_url,GET 时 follow_redirects=False,读 301/302/303/307/308 的 Location - core.py: url_type == "short" 时先解析短链再解析一次,失败则跳过该条 - 新增 tests/test_kuaishou_url_parse.py(6 条,不发网络请求) 验证:两条真实短链分别解析到 3xyziwesje8e9jg / 3xbbkdxtxqm8sae,详情均成功; 带 query 的标准视频页不会被误判成短链。
This commit is contained in:
@@ -240,6 +240,37 @@ class KuaiShouClient(AbstractApiClient, ProxyRefreshMixin):
|
||||
}
|
||||
return await self.post("", post_data)
|
||||
|
||||
async def resolve_short_url(self, short_url: str) -> str:
|
||||
"""解析快手分享短链(/f/xxx),返回重定向后的真实 URL。
|
||||
|
||||
短链路径里的 share_token 不是视频 ID,只能靠 302 的 Location 拿到
|
||||
/short-video/<id> 形式的真实地址。
|
||||
"""
|
||||
async with make_async_client(proxy=self.proxy, follow_redirects=False) as client:
|
||||
try:
|
||||
utils.logger.info(
|
||||
f"[KuaiShouClient.resolve_short_url] Resolving short URL: {short_url}"
|
||||
)
|
||||
response = await client.get(short_url, timeout=10, headers=self.headers)
|
||||
|
||||
# 短链通常返回 302
|
||||
if response.status_code in (301, 302, 303, 307, 308):
|
||||
redirect_url = response.headers.get("Location", "")
|
||||
utils.logger.info(
|
||||
f"[KuaiShouClient.resolve_short_url] Resolved to: {redirect_url}"
|
||||
)
|
||||
return redirect_url
|
||||
|
||||
utils.logger.warning(
|
||||
f"[KuaiShouClient.resolve_short_url] Unexpected status code: {response.status_code}"
|
||||
)
|
||||
return ""
|
||||
except Exception as e:
|
||||
utils.logger.error(
|
||||
f"[KuaiShouClient.resolve_short_url] Failed to resolve short URL: {e}"
|
||||
)
|
||||
return ""
|
||||
|
||||
async def get_video_info(self, photo_id: str) -> Dict:
|
||||
"""
|
||||
Kuaishou web video detail api
|
||||
|
||||
Reference in New Issue
Block a user