|
12 | 12 |
|
13 | 13 |
|
14 | 14 | class VideoDownloader:
|
15 |
| - failed_videos: list[str] = [] |
| 15 | + video_output_path: str = 'videos' |
| 16 | + username: Optional[str] = None |
| 17 | + password: Optional[str] = None |
16 | 18 |
|
17 |
| - def download_videos( |
| 19 | + def __init__( |
18 | 20 | self,
|
19 |
| - links: list[str], |
| 21 | + video_output_path: Optional[str] = None, |
20 | 22 | username: Optional[str] = None,
|
21 | 23 | password: Optional[str] = None,
|
22 |
| - ) -> None: |
23 |
| - arguments = [(link, username, password) for link in links] |
| 24 | + ): |
| 25 | + self.video_output_path = video_output_path or self.video_output_path |
| 26 | + self.username = username |
| 27 | + self.password = password |
| 28 | + |
| 29 | + def download_videos(self, links: list[str]) -> None: |
| 30 | + arguments = [(link, ) for link in links] |
24 | 31 | self._execute_parallel(
|
25 | 32 | self.download_video,
|
26 | 33 | arguments,
|
27 | 34 | )
|
28 | 35 |
|
29 |
| - def download_video(self, link: str, username: Optional[str] = None, password: Optional[str] = None) -> None: |
| 36 | + def download_video(self, link: str) -> None: |
30 | 37 | try:
|
31 | 38 | self._download_public_video(link)
|
32 | 39 | except DownloadError as error:
|
33 |
| - if not username or not password: |
| 40 | + if not self.username or not self.password: |
34 | 41 | raise ValueError('Username and password are required for private videos') from error
|
35 | 42 | print('Failed to download video, trying to download with authentication ...')
|
36 |
| - self._download_twitter_private_video(link, username, password) |
37 |
| - self.failed_videos.append(link) |
| 43 | + self._download_twitter_private_video(link, self.username, self.password) |
38 | 44 | except YoutubeDLError:
|
39 | 45 | link = link.replace('\n', '')
|
40 | 46 | print(f'{link} failed')
|
41 | 47 |
|
42 | 48 | def _download_public_video(self, link: str, video_filename: Optional[str] = None) -> None:
|
43 |
| - youtube_dl_output_template = f'videos/{video_filename}' if video_filename else 'videos/%(title)s.%(ext)s' |
| 49 | + youtube_dl_output_template = \ |
| 50 | + f'{self.video_output_path}/{video_filename}' \ |
| 51 | + if video_filename else \ |
| 52 | + f'{self.video_output_path}/%(title)s.%(ext)s' |
44 | 53 | youtube_dl_option = {'format': 'bestvideo/best', 'outtmpl': youtube_dl_output_template}
|
45 | 54 | with youtube_dl.YoutubeDL(youtube_dl_option) as youtube_dl_downloader:
|
46 | 55 | youtube_dl_downloader.download([link])
|
|
0 commit comments