-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_downloader.py
More file actions
92 lines (71 loc) · 3.82 KB
/
test_downloader.py
File metadata and controls
92 lines (71 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Downloader factory and platform handler tests.
Covers:
- Factory creates correct downloader for each platform URL
- Unknown URLs get GenericDownloader (fallback)
- Platform-specific can_handle and extract methods
All console output must be in English only (no emoji, no Chinese).
"""
import os
import sys
import unittest
from video_transcript_api.downloaders import create_downloader
from video_transcript_api.downloaders.douyin import DouyinDownloader
from video_transcript_api.downloaders.bilibili import BilibiliDownloader
from video_transcript_api.downloaders.xiaohongshu import XiaohongshuDownloader
from video_transcript_api.downloaders.youtube import YoutubeDownloader
from video_transcript_api.downloaders.xiaoyuzhou import XiaoyuzhouDownloader
from video_transcript_api.downloaders.generic import GenericDownloader
class TestDownloaderFactory(unittest.TestCase):
"""Test downloader factory routing."""
def test_create_downloader_douyin(self):
"""Douyin URL should create DouyinDownloader."""
downloader = create_downloader("https://v.douyin.com/sample")
self.assertIsInstance(downloader, DouyinDownloader)
def test_create_downloader_bilibili(self):
"""Bilibili URL should create BilibiliDownloader."""
downloader = create_downloader("https://www.bilibili.com/video/BV1234")
self.assertIsInstance(downloader, BilibiliDownloader)
def test_create_downloader_xiaohongshu(self):
"""Xiaohongshu URL should create XiaohongshuDownloader."""
downloader = create_downloader("https://www.xiaohongshu.com/explore/12345")
self.assertIsInstance(downloader, XiaohongshuDownloader)
def test_create_downloader_youtube(self):
"""YouTube URL should create YoutubeDownloader."""
downloader = create_downloader("https://www.youtube.com/watch?v=12345")
self.assertIsInstance(downloader, YoutubeDownloader)
def test_create_downloader_xiaoyuzhou(self):
"""Xiaoyuzhou URL should create XiaoyuzhouDownloader."""
downloader = create_downloader("https://www.xiaoyuzhoufm.com/episode/687893e0a12f9ff06a98a597")
self.assertIsInstance(downloader, XiaoyuzhouDownloader)
def test_create_downloader_unsupported_returns_generic(self):
"""Unknown URL should return GenericDownloader (not None)."""
downloader = create_downloader("https://www.unsupported.com/video/12345")
self.assertIsInstance(downloader, GenericDownloader)
class TestXiaoyuzhouDownloaderBasic(unittest.TestCase):
"""Test Xiaoyuzhou downloader basic methods (no network)."""
def test_can_handle(self):
"""Should handle xiaoyuzhoufm.com URLs."""
downloader = XiaoyuzhouDownloader()
self.assertTrue(downloader.can_handle("https://www.xiaoyuzhoufm.com/episode/12345"))
self.assertFalse(downloader.can_handle("https://www.youtube.com/watch?v=12345"))
self.assertFalse(downloader.can_handle("https://www.bilibili.com/video/BV12345"))
def test_extract_episode_id(self):
"""Should extract episode ID from URL."""
downloader = XiaoyuzhouDownloader()
episode_id = downloader._extract_episode_id(
"https://www.xiaoyuzhoufm.com/episode/687893e0a12f9ff06a98a597"
)
self.assertEqual(episode_id, "687893e0a12f9ff06a98a597")
def test_extract_episode_id_invalid(self):
"""Invalid URL should raise ValueError."""
downloader = XiaoyuzhouDownloader()
with self.assertRaises(ValueError):
downloader._extract_episode_id("https://www.example.com/invalid")
def test_get_subtitle(self):
"""Xiaoyuzhou has no subtitle support."""
downloader = XiaoyuzhouDownloader()
result = downloader.get_subtitle("https://www.xiaoyuzhoufm.com/episode/12345")
self.assertIsNone(result)
if __name__ == '__main__':
unittest.main()