feat(ingest): parse video refs and fetch detail by bvid

This commit is contained in:
kairee committed 2026-11-01 13:46:46 +08:00
1 parent e903c49921
commit d2adb65e08
6 files changed
+410

No files matched your search

+98
View File
@@ -340,6 +340,100 @@ def test_view_unavailable_and_malformed():
raise AssertionError("缺少 stat 字段应报协议错误")
def test_view_detail_by_aid_and_bvid():
transport = QueueTransport([http_json(VIEW_PAYLOAD)])
client, _ = make_client(transport)
detail = client.view_detail(aid=117530286889638)
assert detail.aid == 117530286889638
assert detail.bvid == "BV1xvxi6LEZD"
assert detail.up_mid == 434442072
assert detail.up_name == "_-抱抱-_"
assert detail.pubdate == 1793370433
assert detail.duration == 9504
assert detail.pages == 2
assert detail.cover and detail.cover.startswith("http")
assert detail.metrics()["view"] == 819
assert "aid=117530286889638" in transport.calls[0]["url"]
transport = QueueTransport([http_json(VIEW_PAYLOAD)])
client, _ = make_client(transport)
detail = client.view_detail(bvid="BV1xvxi6LEZD")
assert detail.aid == 117530286889638
assert "bvid=BV1xvxi6LEZD" in transport.calls[0]["url"]
def test_view_detail_fallbacks_and_required_arg():
payload = view_for(1)
for key in ("owner", "duration", "pages", "videos", "pic"):
payload["data"].pop(key, None)
transport = QueueTransport([http_json(payload)])
client, _ = make_client(transport)
detail = client.view_detail(aid=1)
assert detail.up_mid is None and detail.up_name is None
assert detail.duration == 0
assert detail.pages is None
assert detail.cover is None
client, _ = make_client(QueueTransport([]))
for kwargs in ({}, {"aid": 1, "bvid": "BV1xvxi6LEZD"}):
try:
client.view_detail(**kwargs)
except ValueError:
pass
else:
raise AssertionError("必须且只能提供 aid 或 bvid")
def test_view_detail_unavailable():
transport = QueueTransport([
http_json(VIEW_UNAVAILABLE), http_json(VIEW_UNAVAILABLE),
])
client, _ = make_client(transport)
try:
client.view_detail(bvid="BV1xvxi6LEZD")
except BilibiliAPIError as exc:
assert exc.code == 62012
else:
raise AssertionError("不可访问视频应抛业务错误")
def test_resolve_short_link():
redirect = HTTPResponse(
302,
{"location": "https://www.bilibili.com/video/BV1x8x166EzY"},
b"",
)
transport = QueueTransport([redirect])
client, _ = make_client(transport)
final = client.resolve_short_link("https://b23.tv/abc")
assert final == "https://www.bilibili.com/video/BV1x8x166EzY"
assert transport.calls[0]["method"] == "GET"
assert "b23.tv" in transport.calls[0]["url"]
# 多跳重定向,大小写不同的 Location 头
transport = QueueTransport([
HTTPResponse(301, {"Location": "https://b23.tv/next"}, b""),
HTTPResponse(302, {"location": "https://www.bilibili.com/video/BV1x8x166EzY"}, b""),
])
client, _ = make_client(transport)
assert client.resolve_short_link("https://b23.tv/start").endswith("BV1x8x166EzY")
# 非重定向直接返回最终 URL
transport = QueueTransport([HTTPResponse(200, {}, b"<html>")])
client, _ = make_client(transport)
assert client.resolve_short_link("https://b23.tv/x") == "https://b23.tv/x"
# HTTP 错误应抛出
transport = QueueTransport([HTTPResponse(404, {}, b"not found")])
client, _ = make_client(transport)
try:
client.resolve_short_link("https://b23.tv/x")
except BilibiliHTTPError as exc:
assert exc.status == 404
else:
raise AssertionError("短链 HTTP 404 应抛出")
def test_relation_stat():
transport = QueueTransport([http_json(RELATION_PAYLOAD)])
client, _ = make_client(transport)
@@ -436,6 +530,10 @@ def main():
test_timeout_and_method_forwarded()
test_view_seven_metrics()
test_view_unavailable_and_malformed()
test_view_detail_by_aid_and_bvid()
test_view_detail_fallbacks_and_required_arg()
test_view_detail_unavailable()
test_resolve_short_link()
test_relation_stat()
test_check_login()
test_check_login_without_cookie_makes_no_request()