Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit b98f48c

Browse files
committed
Support setting output path
1 parent 0a5c5d1 commit b98f48c

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

main.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from os import system
2+
from typing import Optional
33

44
from playwright.sync_api import sync_playwright
55
from pydantic import BaseModel
@@ -11,7 +11,7 @@ class Settings(BaseModel):
1111
username: str
1212
password: str
1313
recent_liked: str
14-
videos_path: str
14+
videos_path: Optional[str] = None
1515

1616

1717
def get_settings() -> Settings:
@@ -44,10 +44,6 @@ def backup_links(links: list[str]) -> None:
4444
links_file.write('\n'.join(links))
4545

4646

47-
def move_videos(path: str) -> None:
48-
system(f'mv videos/*.mp4 {path}')
49-
50-
5147
def main() -> None:
5248
settings = get_settings()
5349

@@ -67,9 +63,8 @@ def main() -> None:
6763

6864
browser.close()
6965

70-
video_downloader = VideoDownloader()
71-
video_downloader.download_videos(links, settings.username, settings.password)
72-
move_videos(settings.videos_path)
66+
video_downloader = VideoDownloader(settings.videos_path, settings.username, settings.password)
67+
video_downloader.download_videos(links)
7368

7469

7570
main()

utils/video_downloader.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,44 @@
1212

1313

1414
class VideoDownloader:
15-
failed_videos: list[str] = []
15+
video_output_path: str = 'videos'
16+
username: Optional[str] = None
17+
password: Optional[str] = None
1618

17-
def download_videos(
19+
def __init__(
1820
self,
19-
links: list[str],
21+
video_output_path: Optional[str] = None,
2022
username: Optional[str] = None,
2123
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]
2431
self._execute_parallel(
2532
self.download_video,
2633
arguments,
2734
)
2835

29-
def download_video(self, link: str, username: Optional[str] = None, password: Optional[str] = None) -> None:
36+
def download_video(self, link: str) -> None:
3037
try:
3138
self._download_public_video(link)
3239
except DownloadError as error:
33-
if not username or not password:
40+
if not self.username or not self.password:
3441
raise ValueError('Username and password are required for private videos') from error
3542
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)
3844
except YoutubeDLError:
3945
link = link.replace('\n', '')
4046
print(f'{link} failed')
4147

4248
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'
4453
youtube_dl_option = {'format': 'bestvideo/best', 'outtmpl': youtube_dl_output_template}
4554
with youtube_dl.YoutubeDL(youtube_dl_option) as youtube_dl_downloader:
4655
youtube_dl_downloader.download([link])

0 commit comments

Comments
 (0)