|
|
|
@ -30,42 +30,38 @@ class VideoDownloader:
|
|
|
|
|
if not initiated with list, take from queue
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, youtube_id_list=False, task=False):
|
|
|
|
|
CACHE_DIR = EnvironmentSettings.CACHE_DIR
|
|
|
|
|
MEDIA_DIR = EnvironmentSettings.MEDIA_DIR
|
|
|
|
|
|
|
|
|
|
def __init__(self, task=False):
|
|
|
|
|
self.obs = False
|
|
|
|
|
self.video_overwrites = False
|
|
|
|
|
self.youtube_id_list = youtube_id_list
|
|
|
|
|
self.channel_overwrites = get_channel_overwrites()
|
|
|
|
|
self.task = task
|
|
|
|
|
self.config = AppConfig().config
|
|
|
|
|
self.cache_dir = EnvironmentSettings.CACHE_DIR
|
|
|
|
|
self.media_dir = EnvironmentSettings.MEDIA_DIR
|
|
|
|
|
self._build_obs()
|
|
|
|
|
self.channels = set()
|
|
|
|
|
self.videos = set()
|
|
|
|
|
|
|
|
|
|
def run_queue(self, auto_only=False):
|
|
|
|
|
"""setup download queue in redis loop until no more items"""
|
|
|
|
|
self._get_overwrites()
|
|
|
|
|
while True:
|
|
|
|
|
video_data = self._get_next(auto_only)
|
|
|
|
|
if self.task.is_stopped() or not video_data:
|
|
|
|
|
self._reset_auto()
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
youtube_id = video_data.get("youtube_id")
|
|
|
|
|
youtube_id = video_data["youtube_id"]
|
|
|
|
|
channel_id = video_data["channel_id"]
|
|
|
|
|
print(f"{youtube_id}: Downloading video")
|
|
|
|
|
self._notify(video_data, "Validate download format")
|
|
|
|
|
|
|
|
|
|
success = self._dl_single_vid(youtube_id)
|
|
|
|
|
success = self._dl_single_vid(youtube_id, channel_id)
|
|
|
|
|
if not success:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
self._notify(video_data, "Add video metadata to index", progress=1)
|
|
|
|
|
|
|
|
|
|
vid_dict = index_new_video(
|
|
|
|
|
youtube_id,
|
|
|
|
|
video_overwrites=self.video_overwrites,
|
|
|
|
|
video_type=VideoTypeEnum(video_data["vid_type"]),
|
|
|
|
|
)
|
|
|
|
|
video_type = VideoTypeEnum(video_data["vid_type"])
|
|
|
|
|
vid_dict = index_new_video(youtube_id, video_type=video_type)
|
|
|
|
|
self.channels.add(vid_dict["channel"]["channel_id"])
|
|
|
|
|
self.videos.add(vid_dict["youtube_id"])
|
|
|
|
|
|
|
|
|
@ -112,13 +108,6 @@ class VideoDownloader:
|
|
|
|
|
|
|
|
|
|
return response["hits"]["hits"][0]["_source"]
|
|
|
|
|
|
|
|
|
|
def _get_overwrites(self):
|
|
|
|
|
"""get channel overwrites"""
|
|
|
|
|
pending = PendingList()
|
|
|
|
|
pending.get_download()
|
|
|
|
|
pending.get_channels()
|
|
|
|
|
self.video_overwrites = pending.video_overwrites
|
|
|
|
|
|
|
|
|
|
def _progress_hook(self, response):
|
|
|
|
|
"""process the progress_hooks from yt_dlp"""
|
|
|
|
|
progress = False
|
|
|
|
@ -149,7 +138,7 @@ class VideoDownloader:
|
|
|
|
|
"""initial obs"""
|
|
|
|
|
self.obs = {
|
|
|
|
|
"merge_output_format": "mp4",
|
|
|
|
|
"outtmpl": (self.cache_dir + "/download/%(id)s.mp4"),
|
|
|
|
|
"outtmpl": (self.CACHE_DIR + "/download/%(id)s.mp4"),
|
|
|
|
|
"progress_hooks": [self._progress_hook],
|
|
|
|
|
"noprogress": True,
|
|
|
|
|
"continuedl": True,
|
|
|
|
@ -209,22 +198,17 @@ class VideoDownloader:
|
|
|
|
|
|
|
|
|
|
self.obs["postprocessors"] = postprocessors
|
|
|
|
|
|
|
|
|
|
def get_format_overwrites(self, youtube_id):
|
|
|
|
|
"""get overwrites from single video"""
|
|
|
|
|
overwrites = self.video_overwrites.get(youtube_id, False)
|
|
|
|
|
if overwrites:
|
|
|
|
|
return overwrites.get("download_format", False)
|
|
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
def _set_overwrites(self, obs: dict, channel_id: str) -> None:
|
|
|
|
|
"""add overwrites to obs"""
|
|
|
|
|
overwrites = self.channel_overwrites.get(channel_id)
|
|
|
|
|
if overwrites and overwrites.get("download_format"):
|
|
|
|
|
obs["format"] = overwrites.get("download_format")
|
|
|
|
|
|
|
|
|
|
def _dl_single_vid(self, youtube_id):
|
|
|
|
|
def _dl_single_vid(self, youtube_id: str, channel_id: str) -> bool:
|
|
|
|
|
"""download single video"""
|
|
|
|
|
obs = self.obs.copy()
|
|
|
|
|
format_overwrite = self.get_format_overwrites(youtube_id)
|
|
|
|
|
if format_overwrite:
|
|
|
|
|
obs["format"] = format_overwrite
|
|
|
|
|
|
|
|
|
|
dl_cache = self.cache_dir + "/download/"
|
|
|
|
|
self._set_overwrites(obs, channel_id)
|
|
|
|
|
dl_cache = os.path.join(self.CACHE_DIR, "download")
|
|
|
|
|
|
|
|
|
|
# check if already in cache to continue from there
|
|
|
|
|
all_cached = ignore_filelist(os.listdir(dl_cache))
|
|
|
|
@ -258,7 +242,7 @@ class VideoDownloader:
|
|
|
|
|
host_gid = EnvironmentSettings.HOST_GID
|
|
|
|
|
# make folder
|
|
|
|
|
folder = os.path.join(
|
|
|
|
|
self.media_dir, vid_dict["channel"]["channel_id"]
|
|
|
|
|
self.MEDIA_DIR, vid_dict["channel"]["channel_id"]
|
|
|
|
|
)
|
|
|
|
|
if not os.path.exists(folder):
|
|
|
|
|
os.makedirs(folder)
|
|
|
|
@ -266,8 +250,8 @@ class VideoDownloader:
|
|
|
|
|
os.chown(folder, host_uid, host_gid)
|
|
|
|
|
# move media file
|
|
|
|
|
media_file = vid_dict["youtube_id"] + ".mp4"
|
|
|
|
|
old_path = os.path.join(self.cache_dir, "download", media_file)
|
|
|
|
|
new_path = os.path.join(self.media_dir, vid_dict["media_url"])
|
|
|
|
|
old_path = os.path.join(self.CACHE_DIR, "download", media_file)
|
|
|
|
|
new_path = os.path.join(self.MEDIA_DIR, vid_dict["media_url"])
|
|
|
|
|
# move media file and fix permission
|
|
|
|
|
shutil.move(old_path, new_path, copy_function=shutil.copyfile)
|
|
|
|
|
if host_uid and host_gid:
|
|
|
|
@ -382,14 +366,14 @@ class DownloadPostProcess:
|
|
|
|
|
playlist = YoutubePlaylist(playlist_id)
|
|
|
|
|
playlist.update_playlist(skip_on_empty=True)
|
|
|
|
|
|
|
|
|
|
if self.download.task:
|
|
|
|
|
if not self.download.task:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
channel_name = playlist.json_data["playlist_channel"]
|
|
|
|
|
playlist_title = playlist.json_data["playlist_name"]
|
|
|
|
|
message = [
|
|
|
|
|
f"Post Processing Playlists for: {channel_name}",
|
|
|
|
|
f"Validate: {playlist_title} - {idx + 1}/{total_playlist}",
|
|
|
|
|
f"{playlist_title} [{idx + 1}/{total_playlist}]",
|
|
|
|
|
]
|
|
|
|
|
progress = (idx + 1) / total_playlist
|
|
|
|
|
self.download.task.send_progress(message, progress=progress)
|
|
|
|
@ -407,9 +391,9 @@ class DownloadPostProcess:
|
|
|
|
|
channel = YoutubeChannel(channel_id)
|
|
|
|
|
channel.get_from_es()
|
|
|
|
|
overwrites = channel.get_overwrites()
|
|
|
|
|
if overwrites and overwrites.get("index_playlists"):
|
|
|
|
|
if "index_playlists" in overwrites:
|
|
|
|
|
channel.get_all_playlists()
|
|
|
|
|
to_refresh.extend(channel.all_playlists)
|
|
|
|
|
to_refresh.extend([i[0] for i in channel.all_playlists])
|
|
|
|
|
|
|
|
|
|
subs = PlaylistSubscription().get_playlists()
|
|
|
|
|
for playlist in subs:
|
|
|
|
|