+ parser = optparse.OptionParser(
+ usage='Usage: %prog [options] url...',
+ version='2008.11.01',
+ 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('-u', '--username',
+ dest='username', metavar='UN', help='account username')
+ parser.add_option('-p', '--password',
+ dest='password', metavar='PW', help='account password')
+ parser.add_option('-o', '--output',
+ dest='outtmpl', metavar='TPL', help='output filename template')
+ parser.add_option('-q', '--quiet',
+ action='store_true', dest='quiet', help='activates quiet mode', default=False)
+ parser.add_option('-s', '--simulate',
+ action='store_true', dest='simulate', help='do not download video', default=False)
+ parser.add_option('-t', '--title',
+ action='store_true', dest='usetitle', help='use title in file name', default=False)
+ parser.add_option('-l', '--literal',
+ action='store_true', dest='useliteral', help='use literal title in file name', default=False)
+ parser.add_option('-n', '--netrc',
+ action='store_true', dest='usenetrc', help='use .netrc authentication data', default=False)
+ parser.add_option('-g', '--get-url',
+ action='store_true', dest='geturl', help='simulate, quiet but print URL', default=False)
+ parser.add_option('-e', '--get-title',
+ action='store_true', dest='gettitle', help='simulate, quiet but print title', default=False)
+ parser.add_option('-f', '--format',
+ dest='format', metavar='FMT', help='video format code')
+ parser.add_option('-b', '--best-quality',
+ action='store_const', dest='format', help='alias for -f 18', const='18')
+ parser.add_option('-m', '--mobile-version',
+ action='store_const', dest='format', help='alias for -f 17', const='17')
+ 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)')
+ parser.add_option('-a', '--batch-file',
+ dest='batchfile', metavar='F', help='file containing URLs to download')
+ (opts, args) = parser.parse_args()
+
+ # Batch file verification
+ if opts.batchfile is not None:
+ try:
+ batchurls = [line.strip() for line in open(opts.batchfile, 'r')]
+ 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:
+ sys.exit(u'ERROR: you must provide at least one URL')
+ if opts.usenetrc and (opts.username is not None or opts.password is not None):
+ sys.exit(u'ERROR: using .netrc conflicts with giving username/password')
+ if opts.password is not None and opts.username is None:
+ sys.exit(u'ERROR: account username missing')
+ if opts.outtmpl is not None and (opts.useliteral or opts.usetitle):
+ sys.exit(u'ERROR: using output template conflicts with using title or literal title')
+ if opts.usetitle and opts.useliteral:
+ sys.exit(u'ERROR: 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:
+ sys.exit(u'ERROR: invalid rate limit specified')
+ opts.ratelimit = numeric_limit