86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
from server.services.downloads import _local_master
|
|
from server.services.huangdou.playlist import _choose_src
|
|
from server.services.playlist import expand_variant, is_direct_media, is_media_playlist
|
|
|
|
|
|
def test_direct_media_and_hls_choice() -> None:
|
|
assert is_direct_media("https://cdn.example/chapters/2/preview-r10.mp4?auth=1")
|
|
assert not is_direct_media("https://host.example/api/drama/hls/id/1/play.m3u8?line=free")
|
|
|
|
media = "#EXTM3U\n#EXT-X-KEY:METHOD=AES-128,URI=\"enc.key\"\n#EXTINF:1,\nseg_000.ts\n"
|
|
assert is_media_playlist(media)
|
|
master = _local_master(media)
|
|
assert "video.m3u8" in master
|
|
assert "seg_000.ts" not in master
|
|
|
|
src, kind = _choose_src({
|
|
"m3u8": "https://host.example/play.m3u8?x=1",
|
|
"preview_m3u8": "https://cdn.example/preview-r10.mp4",
|
|
"lines": [],
|
|
})
|
|
assert kind == "hls"
|
|
assert src.endswith("play.m3u8?x=1")
|
|
|
|
from server.services.huangdou.client import client
|
|
|
|
client.play_line = "line2"
|
|
src, kind = _choose_src({
|
|
"m3u8": "https://host.example/play.m3u8?line=free",
|
|
"lines": [
|
|
{"name": "free", "url": "https://host.example/play.m3u8?line=free"},
|
|
{"name": "line2", "url": "https://host.example/play.m3u8?line=line2"},
|
|
],
|
|
})
|
|
assert kind == "hls"
|
|
assert "line=line2" in src
|
|
client.play_line = ""
|
|
src, kind = _choose_src({
|
|
"m3u8": "https://host.example/play.m3u8?line=free",
|
|
"lines": [
|
|
{"name": "free", "url": "https://host.example/play.m3u8?line=free"},
|
|
{"name": "line2", "url": "https://host.example/play.m3u8?line=line2"},
|
|
],
|
|
})
|
|
assert "line=free" in src
|
|
|
|
src, kind = _choose_src({
|
|
"m3u8": "",
|
|
"preview_m3u8": "https://cdn.example/chapters/2/preview-r10.mp4",
|
|
"lines": [],
|
|
})
|
|
assert kind == "file"
|
|
assert src.endswith(".mp4")
|
|
|
|
try:
|
|
expand_variant("https://cdn.example/preview-r10.mp4")
|
|
except RuntimeError as exc:
|
|
assert "整段视频" in str(exc)
|
|
else:
|
|
raise AssertionError("mp4 不应走播放列表")
|
|
|
|
|
|
def test_catalog_follows_pages_until_the_api_stops() -> None:
|
|
from server.services.huangdou.catalog import _take_new
|
|
|
|
pages = {
|
|
1: [{"id": str(i), "name": f"剧{i}"} for i in range(50)],
|
|
2: [{"id": str(i), "name": f"剧{i}"} for i in range(50, 100)],
|
|
3: [{"id": str(i), "name": f"剧{i}"} for i in range(100, 120)],
|
|
}
|
|
got = _take_new(lambda page: pages.get(page, []), set())
|
|
assert len(got) == 120
|
|
assert got[-1]["id"] == "119"
|
|
|
|
repeated = {
|
|
1: [{"id": str(i), "name": f"剧{i}"} for i in range(50)],
|
|
2: [{"id": str(i), "name": f"剧{i}"} for i in range(50)],
|
|
}
|
|
got = _take_new(lambda page: repeated.get(page, []), set())
|
|
assert len(got) == 50
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_direct_media_and_hls_choice()
|
|
test_catalog_follows_pages_until_the_api_stops()
|
|
print("ok")
|