+ socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
+
+ # Parse command line
+ parser = optparse.OptionParser(
+ usage='Usage: %prog [options] url...',
+ version='INTERNAL',
+ conflict_handler='resolve',
+ )
+
+ parser.add_option('-h', '--help',
+ action='help', help='print this help text and exit')
+ parser.add_option('-v', '--version',
+ action='version', help='print program version and exit')
+ parser.add_option('-i', '--ignore-errors',
+ action='store_true', dest='ignoreerrors', help='continue on download errors', default=False)
+ parser.add_option('-r', '--rate-limit',
+ dest='ratelimit', metavar='L', help='download rate limit (e.g. 50k or 44.6m)')
+
+ authentication = optparse.OptionGroup(parser, 'Authentication Options')
+ authentication.add_option('-u', '--username',
+ dest='username', metavar='UN', help='account username')
+ authentication.add_option('-p', '--password',
+ dest='password', metavar='PW', help='account password')
+ authentication.add_option('-n', '--netrc',
+ action='store_true', dest='usenetrc', help='use .netrc authentication data', default=False)
+ parser.add_option_group(authentication)
+
+ video_format = optparse.OptionGroup(parser, 'Video Format Options')
+ video_format.add_option('-f', '--format',
+ action='append', dest='format', metavar='FMT', help='video format code')
+ video_format.add_option('-b', '--best-quality',
+ action='append_const', dest='format', help='download the best quality video possible', const='0')
+ video_format.add_option('-m', '--mobile-version',
+ action='append_const', dest='format', help='alias for -f 17', const='17')
+ video_format.add_option('-d', '--high-def',
+ action='append_const', dest='format', help='alias for -f 22', const='22')
+ parser.add_option_group(video_format)
+
+ verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')
+ verbosity.add_option('-q', '--quiet',
+ action='store_true', dest='quiet', help='activates quiet mode', default=False)
+ verbosity.add_option('-s', '--simulate',
+ action='store_true', dest='simulate', help='do not download video', default=False)
+ verbosity.add_option('-g', '--get-url',
+ action='store_true', dest='geturl', help='simulate, quiet but print URL', default=False)
+ verbosity.add_option('-e', '--get-title',
+ action='store_true', dest='gettitle', help='simulate, quiet but print title', default=False)
+ verbosity.add_option('-t', '--title',
+ action='store_true', dest='usetitle', help='use title in file name', default=False)
+ verbosity.add_option('-l', '--literal',
+ action='store_true', dest='useliteral', help='use literal title in file name', default=False)
+ parser.add_option_group(verbosity)
+
+ filesystem = optparse.OptionGroup(parser, 'Filesystem Options')
+ filesystem.add_option('-o', '--output',
+ dest='outtmpl', metavar='TPL', help='output filename template')
+ filesystem.add_option('-a', '--batch-file',
+ dest='batchfile', metavar='F', help='file containing URLs to download')
+ filesystem.add_option('-w', '--no-overwrites',
+ action='store_true', dest='nooverwrites', help='do not overwrite files', default=False)
+ parser.add_option_group(filesystem)
+
+ (opts, args) = parser.parse_args()
+
+ # Batch file verification
+ batchurls = []
+ if opts.batchfile is not None:
+ try:
+ batchurls = open(opts.batchfile, 'r').readlines()
+ batchurls = [x.strip() for x in batchurls]
+ batchurls = [x for x in batchurls if len(x) > 0]
+ except IOError:
+ sys.exit(u'ERROR: batch file could not be read')
+ all_urls = batchurls + args
+
+ # Conflicting, missing and erroneous options
+ if len(all_urls) < 1:
+ parser.error(u'you must provide at least one URL')
+ if opts.usenetrc and (opts.username is not None or opts.password is not None):
+ parser.error(u'using .netrc conflicts with giving username/password')
+ if opts.password is not None and opts.username is None:
+ parser.error(u'account username missing')
+ if opts.outtmpl is not None and (opts.useliteral or opts.usetitle):
+ parser.error(u'using output template conflicts with using title or literal title')
+ if opts.usetitle and opts.useliteral:
+ parser.error(u'using title conflicts with using literal title')
+ if opts.username is not None and opts.password is None:
+ opts.password = getpass.getpass(u'Type account password and press return:')
+ if opts.ratelimit is not None:
+ numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
+ if numeric_limit is None:
+ parser.error(u'invalid rate limit specified')
+ opts.ratelimit = numeric_limit
+ if len(opts.format) > 1:
+ parser.error(u'pass at most one of the video format option flags (-f, -b, -m, -d)')