50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from biliup_next.modules.ingest.providers.bilibili_url import BilibiliUrlIngestProvider
|
|
|
|
|
|
class FakeYtDlpAdapter:
|
|
def probe(self, *, yt_dlp_cmd: str, source_url: str): # noqa: ANN001
|
|
return {
|
|
"id": "BV1TEST1234",
|
|
"title": "测试视频标题",
|
|
"uploader": "测试主播",
|
|
"duration": 321.0,
|
|
}
|
|
|
|
def download(self, *, yt_dlp_cmd: str, source_url: str, output_template: str, format_selector=None): # noqa: ANN001
|
|
output_path = Path(output_template.replace("%(ext)s", "mp4"))
|
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
output_path.write_bytes(b"fake-video")
|
|
return type("Result", (), {"returncode": 0, "stdout": "ok", "stderr": ""})()
|
|
|
|
|
|
class BilibiliUrlIngestProviderTests(unittest.TestCase):
|
|
def test_resolve_and_download_source(self) -> None:
|
|
provider = BilibiliUrlIngestProvider(yt_dlp=FakeYtDlpAdapter())
|
|
settings = {"yt_dlp_cmd": "yt-dlp"}
|
|
|
|
resolved = provider.resolve_source("https://www.bilibili.com/video/BV1TEST1234", settings)
|
|
|
|
self.assertEqual(resolved["video_id"], "BV1TEST1234")
|
|
self.assertEqual(resolved["title"], "测试视频标题")
|
|
self.assertEqual(resolved["streamer"], "测试主播")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
downloaded = provider.download_source(
|
|
"https://www.bilibili.com/video/BV1TEST1234",
|
|
Path(tmpdir),
|
|
settings,
|
|
task_id=str(resolved["task_id"]),
|
|
)
|
|
self.assertTrue(downloaded.exists())
|
|
self.assertEqual(downloaded.suffix, ".mp4")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|