返回 douyin-downloader
downloader_factory.py
根目录 / core / downloader_factory.py
1 from typing import Any, Optional
2
3 from auth import CookieManager
4 from config import ConfigLoader
5 from control import QueueManager, RateLimiter, RetryHandler
6 from core.api_client import DouyinAPIClient
7 from core.downloader_base import BaseDownloader
8 from core.live_downloader import LiveDownloader
9 from core.mix_downloader import MixDownloader
10 from core.music_downloader import MusicDownloader
11 from core.user_downloader import UserDownloader
12 from core.video_downloader import VideoDownloader
13 from storage import Database, FileManager
14 from utils.logger import setup_logger
15
16 logger = setup_logger("DownloaderFactory")
17
18
19 class DownloaderFactory:
20 @staticmethod
21 def create(
22 url_type: str,
23 config: ConfigLoader,
24 api_client: DouyinAPIClient,
25 file_manager: FileManager,
26 cookie_manager: CookieManager,
27 database: Optional[Database] = None,
28 rate_limiter: Optional[RateLimiter] = None,
29 retry_handler: Optional[RetryHandler] = None,
30 queue_manager: Optional[QueueManager] = None,
31 progress_reporter: Optional[Any] = None,
32 ) -> Optional[BaseDownloader]:
33
34 common_args = {
35 "config": config,
36 "api_client": api_client,
37 "file_manager": file_manager,
38 "cookie_manager": cookie_manager,
39 "database": database,
40 "rate_limiter": rate_limiter,
41 "retry_handler": retry_handler,
42 "queue_manager": queue_manager,
43 "progress_reporter": progress_reporter,
44 }
45
46 if url_type == "video":
47 return VideoDownloader(**common_args)
48 elif url_type == "user":
49 return UserDownloader(**common_args)
50 elif url_type == "gallery":
51 return VideoDownloader(**common_args)
52 elif url_type == "collection":
53 return MixDownloader(**common_args)
54 elif url_type == "music":
55 return MusicDownloader(**common_args)
56 elif url_type == "live":
57 return LiveDownloader(**common_args)
58 elif url_type == "short":
59 logger.error(
60 "Short URL was not resolved before dispatching. "
61 "Please call api_client.resolve_short_url() first."
62 )
63 return None
64 else:
65 logger.error("Unsupported URL type: %s", url_type)
66 return None
67
67 lines PYTHON