edumike
edumike

Reputation: 3309

YoutubeDL - How to get a status object after download has completed

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

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])

Upvotes: 3

Views: 10874

Answers (1)

edumike
edumike

Reputation: 3309

I got the answer here: https://github.com/rg3/youtube-dl/issues/7120

Basically my one to many model of song files to song requests was wrong - rewriting this relationship allowed me to use the hook to db updates.

Upvotes: 3

Related Questions