Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get estimated download time and download-speed programmatically? #2091

Closed
Elite opened this issue Jan 4, 2014 · 6 comments
Closed

How to get estimated download time and download-speed programmatically? #2091

Elite opened this issue Jan 4, 2014 · 6 comments

Comments

@Elite
Copy link

@Elite Elite commented Jan 4, 2014

When importing YTDL in python and using the below to download with progress_hook, how to get the estimated download time and download-speed programmatically?

Also, how to capture other messages like info and error's ?

with YoutubeDL(ydl_options) as ydl:
            ydl.add_default_info_extractors()
            ydl.fd.add_progress_hook(progress_hook)
            ydl.download([self.url])
@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Jan 4, 2014

They are stored in the eta and speed fields of the dictionary (look at downloader.http to see all the info that is sent). The ETA is in seconds and the speed in bytes/second.

For getting the messages you just need to set the logger field of ydl_options to a Logger like object (http://docs.python.org/2/library/logging.html#logger-objects), you just need to create an object with the debug and error methods.

@jaimeMF jaimeMF closed this Jan 4, 2014
@Elite
Copy link
Author

@Elite Elite commented Jan 5, 2014

Thanks, can you pls. provide an example snippet of consuming the logger object.

@Elite
Copy link
Author

@Elite Elite commented Jan 5, 2014

Got it -

 logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    filename='TRACE.log',
                    filemode='a')
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        consoleFormatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
        console.setFormatter(consoleFormatter)
        logging.getLogger('').addHandler(console)
        myLog = logging.getLogger("debug")

This works and logs to the TRACE.log, but I have no clue how to emit and consume these messages in python script (not file), any ideas? For example handle situation when an error occurs?

@jaimeMF
Copy link
Collaborator

@jaimeMF jaimeMF commented Jan 5, 2014

Sure, instead of creating a real logging.Logger object you create an object with the debug and info methods:

from youtube_dl import YoutubeDL

class Logger(object):
    def debug(self, msg):
        print('debug msg==> %s' % msg)
    def error(self, msg):
        print('error msg==> %s' % msg)

opts = {
    'logger': Logger(),
}
ydl = YoutubeDL(opts)

with ydl:
    ydl.add_default_info_extractors()
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])

Then you can do anything you want with the messages, write them to a GUI, store them show an error dialog if you receive an error message.

@Elite
Copy link
Author

@Elite Elite commented Jan 5, 2014

Thanks :)

@ghost
Copy link

@ghost ghost commented Jan 5, 2014

@jaimeMF wow looks simple and is better than what i was thinking... something like

try:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])
except:
    print "Unexpected error:", sys.exc_info()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.