From cd8f2b5bfff1273df75abd1e20f2bb0cef64ced5 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 15 Apr 2019 16:03:34 +0200 Subject: [PATCH] Fix ANOTHER youtube-dl bug. --- royalnet/audio/youtubedl.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/royalnet/audio/youtubedl.py b/royalnet/audio/youtubedl.py index bb029886..32ee770f 100644 --- a/royalnet/audio/youtubedl.py +++ b/royalnet/audio/youtubedl.py @@ -1,5 +1,6 @@ import typing import logging as _logging +import os from youtube_dl import YoutubeDL from contextlib import contextmanager @@ -21,32 +22,21 @@ class YtdlFile: progress_hooks = [] self.info: "YtdlInfo" = info self.filename: str - self.downloaded_bytes: typing.Optional[int] - self.elapsed: typing.Optional[float] - # Download the file ytdl = YoutubeDL({ "logger": log, # Log messages to a logging.Logger instance. "quiet": True, # Do not print messages to stdout. "noplaylist": True, # Download single video instead of a playlist if in doubt. "no_warnings": True, # Do not print out anything for warnings. "outtmpl": outtmpl, - "progress_hooks": [self._progress_hook, *progress_hooks], + "progress_hooks": progress_hooks, **ytdl_args }) + # Find the file name + self.filename = ytdl.prepare_filename(self.info.__dict__) + # Download the file ytdl.download([self.info.webpage_url]) - - def _progress_hook(self, data: dict): - # Check the status - status = data.get("status") - # Ignore unknown values - if status not in ["downloading", "error", "finished"]: - return - # If the download is finished, set the filename - if status == "finished": - # Filename is always present - self.filename = data["filename"] - self.downloaded_bytes = data.get("downloaded_bytes") - self.elapsed = data.get("elapsed") + # Final checks + assert os.path.exists(self.filename) def _stop_download(self): raise InterruptDownload() @@ -56,6 +46,10 @@ class YtdlFile: info_list = YtdlInfo.create_from_url(url) return [info.download(outtmpl, progress_hooks, **ytdl_args) for info in info_list] + def delete_file(self): + # TODO: _might_ be unsafe, test this + os.remove(self.filename) + class YtdlInfo: """A wrapper around youtube_dl extracted info.""" @@ -150,3 +144,7 @@ class YtdlInfo: if self.webpage_url: return self.webpage_url return self.id + + +if __name__ == "__main__": + f = YtdlFile.create_from_url("https://www.youtube.com/watch?v=BaW_jenozKc&v=UxxajLWwzqY", "./lovely.mp4")