I'm trying basically to get information out of what seems to be a status object that's hitting the hook in Youtube-DL, and then I'm trying to save it to the db. I've got a 'song' object with attributes such as "filename" I'm trying to save once the download is complete, and maybe even continually update the database with progress.
There's four ways I can think of to do this but I've not been able to get them to work
- Send the my_hook function a db and song object and then save it all in there once status == finished. Problem is I'm unable to pass additional parameters to the hook unless I'm missing something
- Get the my_hook function to return d and then save that, problem is that I don't think I can access that it would return to (youtube-dl source)
- Get ydl.download([song.url]) to return a status object that I can process, I don't think it does this though
- I don't want to do this, but I can output a .json file and get it from there, or just simply guess the name of the file given that I'm dictating it initially :(
Code looks like this:
def my_hook(d):
if d['status'] == 'finished':
file_tuple = os.path.split(os.path.abspath(d['filename']))
print("Done downloading {}".format(file_tuple[1]))
if d['status'] == 'downloading':
print(d['filename'], d['_percent_str'], d['_eta_str'])
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
class Downloader(object):
def get_opts(self):
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': os.path.join(app.config['VIDEOS_FOLDER'], '%(id)s.%(ext)s'),
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
return ydl_opts
def download(self, song):
ydl = youtube_dl.YoutubeDL(self.get_opts())
ydl.download([song.url])