badl/manager/__init__.py

46 lines
1.5 KiB
Python
Raw Normal View History

2023-02-21 17:50:53 +08:00
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] = []
2023-02-21 19:56:22 +08:00
self.bvid_set = set()
2023-02-21 17:50:53 +08:00
2023-02-21 19:56:22 +08:00
def add_videos_by_number(self, video_numbers: list[str]):
for video_number in video_numbers:
2023-02-21 17:50:53 +08:00
try:
video = Video(video_number)
except:
continue
2023-02-21 19:56:22 +08:00
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()
2023-02-21 17:50:53 +08:00
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
]