✨ Download manager
This commit is contained in:
parent
8077378510
commit
23c8f3884d
6 changed files with 76 additions and 94 deletions
42
manager/__init__.py
Normal file
42
manager/__init__.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
from video import Video
|
||||
from threading import Thread
|
||||
from sanitize_filename import sanitize
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class Manager:
|
||||
def __init__(self):
|
||||
self.video_list: list[Video] = []
|
||||
|
||||
def add_videos_by_number(self, video_numbers: str):
|
||||
for video_number in video_numbers.split():
|
||||
try:
|
||||
video = Video(video_number)
|
||||
except:
|
||||
continue
|
||||
Thread(target=video.get_info).start()
|
||||
self.video_list.append(video)
|
||||
|
||||
def download(self, id: int, parent_dir: str | Path = Path(".")):
|
||||
if isinstance(parent_dir, str):
|
||||
parent_dir = Path(parent_dir)
|
||||
if not 0 <= id < len(self.video_list):
|
||||
raise Exception(f"id ({id}) out of range!")
|
||||
video = self.video_list[id]
|
||||
if not hasattr(video, "title"):
|
||||
raise Exception(f"No information for video {video.number}")
|
||||
filename = sanitize(video.title) + ".m4a"
|
||||
t = Thread(target=video.download, args=(parent_dir / filename,))
|
||||
t.start()
|
||||
|
||||
def get_progress(self):
|
||||
return [
|
||||
{
|
||||
"number": v.number,
|
||||
"title": v.title,
|
||||
"author": v.author,
|
||||
"received": v.received_bytes,
|
||||
"total": v.length,
|
||||
}
|
||||
for v in self.video_list
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue