Add new --cookies option to be able to save cookies to disk (fixes issue #208)

This commit is contained in:
Ricardo Garcia 2010-10-23 12:54:00 +02:00
parent e08878f498
commit 80066952bc
1 changed files with 45 additions and 21 deletions

View File

@ -4,6 +4,7 @@
# Author: Danny Colligan # Author: Danny Colligan
# Author: Benjamin Johnson # Author: Benjamin Johnson
# License: Public domain code # License: Public domain code
import cookielib
import htmlentitydefs import htmlentitydefs
import httplib import httplib
import locale import locale
@ -184,22 +185,25 @@ class FileDownloader(object):
Available options: Available options:
username: Username for authentication purposes. username: Username for authentication purposes.
password: Password for authentication purposes. password: Password for authentication purposes.
usenetrc: Use netrc for authentication instead. usenetrc: Use netrc for authentication instead.
quiet: Do not print messages to stdout. quiet: Do not print messages to stdout.
forceurl: Force printing final URL. forceurl: Force printing final URL.
forcetitle: Force printing title. forcetitle: Force printing title.
simulate: Do not download the video files. forcethumbnail: Force printing thumbnail URL.
format: Video format code. forcedescription: Force printing description.
format_limit: Highest quality format to try. simulate: Do not download the video files.
outtmpl: Template for output names. format: Video format code.
ignoreerrors: Do not stop on download errors. format_limit: Highest quality format to try.
ratelimit: Download speed limit, in bytes/sec. outtmpl: Template for output names.
nooverwrites: Prevent overwriting files. ignoreerrors: Do not stop on download errors.
retries: Number of times to retry for HTTP error 5xx ratelimit: Download speed limit, in bytes/sec.
continuedl: Try to continue downloads if possible. nooverwrites: Prevent overwriting files.
noprogress: Do not print the progress bar. retries: Number of times to retry for HTTP error 5xx
continuedl: Try to continue downloads if possible.
noprogress: Do not print the progress bar.
playliststart: Playlist item to start at.
""" """
params = None params = None
@ -2096,11 +2100,6 @@ if __name__ == '__main__':
stream.close() stream.close()
downloader.to_stdout('Updated to version %s' % latest_version) downloader.to_stdout('Updated to version %s' % latest_version)
# General configuration
urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()))
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor()))
socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
# Parse command line # Parse command line
parser = optparse.OptionParser( parser = optparse.OptionParser(
usage='Usage: %prog [options] url...', usage='Usage: %prog [options] url...',
@ -2175,10 +2174,27 @@ if __name__ == '__main__':
action='store_true', dest='nooverwrites', help='do not overwrite files', default=False) action='store_true', dest='nooverwrites', help='do not overwrite files', default=False)
filesystem.add_option('-c', '--continue', filesystem.add_option('-c', '--continue',
action='store_true', dest='continue_dl', help='resume partially downloaded files', default=False) action='store_true', dest='continue_dl', help='resume partially downloaded files', default=False)
filesystem.add_option('--cookies',
dest='cookiefile', metavar='FILE', help='file to dump cookie jar to')
parser.add_option_group(filesystem) parser.add_option_group(filesystem)
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
# Open appropriate CookieJar
if opts.cookiefile is None:
jar = cookielib.CookieJar()
else:
try:
jar = cookielib.MozillaCookieJar(opts.cookiefile)
except (IOError, OSError), err:
sys.exit(u'ERROR: unable to open cookie file')
# General configuration
cookie_processor = urllib2.HTTPCookieProcessor(jar)
urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()))
urllib2.install_opener(urllib2.build_opener(cookie_processor))
socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
# Batch file verification # Batch file verification
batchurls = [] batchurls = []
if opts.batchfile is not None: if opts.batchfile is not None:
@ -2292,6 +2308,14 @@ if __name__ == '__main__':
else: else:
sys.exit() sys.exit()
retcode = fd.download(all_urls) retcode = fd.download(all_urls)
# Dump cookie jar if requested
if opts.cookiefile is not None:
try:
jar.save()
except (IOError, OSError), err:
sys.exit(u'ERROR: unable to save cookie jar')
sys.exit(retcode) sys.exit(retcode)
except DownloadError: except DownloadError: