Commits

Anonymous committed b7e9605

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

Comments (0)

Files changed (1)

 # Author: Danny Colligan
 # Author: Benjamin Johnson
 # License: Public domain code
+import cookielib
 import htmlentitydefs
 import httplib
 import locale
 
 	Available options:
 
-	username:	Username for authentication purposes.
-	password:	Password for authentication purposes.
-	usenetrc:	Use netrc for authentication instead.
-	quiet:		Do not print messages to stdout.
-	forceurl:	Force printing final URL.
-	forcetitle:	Force printing title.
-	simulate:	Do not download the video files.
-	format:		Video format code.
-	format_limit:	Highest quality format to try.
-	outtmpl:	Template for output names.
-	ignoreerrors:	Do not stop on download errors.
-	ratelimit:	Download speed limit, in bytes/sec.
-	nooverwrites:	Prevent overwriting files.
-	retries:	Number of times to retry for HTTP error 5xx
-	continuedl:	Try to continue downloads if possible.
-	noprogress:	Do not print the progress bar.
+	username:         Username for authentication purposes.
+	password:         Password for authentication purposes.
+	usenetrc:         Use netrc for authentication instead.
+	quiet:            Do not print messages to stdout.
+	forceurl:         Force printing final URL.
+	forcetitle:       Force printing title.
+	forcethumbnail:   Force printing thumbnail URL.
+	forcedescription: Force printing description.
+	simulate:         Do not download the video files.
+	format:           Video format code.
+	format_limit:     Highest quality format to try.
+	outtmpl:          Template for output names.
+	ignoreerrors:     Do not stop on download errors.
+	ratelimit:        Download speed limit, in bytes/sec.
+	nooverwrites:     Prevent overwriting files.
+	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
 			stream.close()
 			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
 		parser = optparse.OptionParser(
 			usage='Usage: %prog [options] url...',
 				action='store_true', dest='nooverwrites', help='do not overwrite files', default=False)
 		filesystem.add_option('-c', '--continue',
 				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)
 
 		(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
 		batchurls = []
 		if opts.batchfile is not None:
 			else:
 				sys.exit()
 		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)
 
 	except DownloadError:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.