45 lines
1.5 KiB
Python
45 lines
1.5 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] = []
|
|
self.bvid_set = set()
|
|
|
|
def add_videos_by_number(self, video_numbers: list[str]):
|
|
for video_number in video_numbers:
|
|
try:
|
|
video = Video(video_number)
|
|
except:
|
|
continue
|
|
if (bvid := video.v.get_bvid()) not in self.bvid_set:
|
|
self.bvid_set.add(bvid)
|
|
self.video_list.append(video)
|
|
Thread(target=video.get_info).start()
|
|
|
|
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
|
|
]
|