badl/manager/__init__.py
2023-02-21 17:50:53 +08:00

42 lines
1.3 KiB
Python

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
]