Refactor main function

pull/8/head
Georgi Valkov 13 years ago
parent 51c8e53ffe
commit 5adcaa4385

@ -2872,180 +2872,182 @@ def parseOpts():
return parser, opts, args return parser, opts, args
def main():
parser, opts, args = parseOpts()
if __name__ == '__main__': # Open appropriate CookieJar
try: if opts.cookiefile is None:
parser, opts, args = parseOpts() jar = cookielib.CookieJar()
else:
# Open appropriate CookieJar try:
if opts.cookiefile is None: jar = cookielib.MozillaCookieJar(opts.cookiefile)
jar = cookielib.CookieJar() if os.path.isfile(opts.cookiefile) and os.access(opts.cookiefile, os.R_OK):
else: jar.load()
try: except (IOError, OSError), err:
jar = cookielib.MozillaCookieJar(opts.cookiefile) sys.exit(u'ERROR: unable to open cookie file')
if os.path.isfile(opts.cookiefile) and os.access(opts.cookiefile, os.R_OK):
jar.load()
except (IOError, OSError), err:
sys.exit(u'ERROR: unable to open cookie file')
# Dump user agent # Dump user agent
if opts.dump_user_agent: if opts.dump_user_agent:
print std_headers['User-Agent'] print std_headers['User-Agent']
sys.exit(0) sys.exit(0)
# General configuration # General configuration
cookie_processor = urllib2.HTTPCookieProcessor(jar) cookie_processor = urllib2.HTTPCookieProcessor(jar)
urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler(), cookie_processor, YoutubeDLHandler())) urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler(), cookie_processor, YoutubeDLHandler()))
socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words) 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:
try:
if opts.batchfile == '-':
batchfd = sys.stdin
else:
batchfd = open(opts.batchfile, 'r')
batchurls = batchfd.readlines()
batchurls = [x.strip() for x in batchurls]
batchurls = [x for x in batchurls if len(x) > 0 and not re.search(r'^[#/;]', x)]
except IOError:
sys.exit(u'ERROR: batch file could not be read')
all_urls = batchurls + args
# Conflicting, missing and erroneous options
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 or opts.autonumber):
parser.error(u'using output template conflicts with using title, literal title or auto number')
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 opts.retries is not None:
try:
opts.retries = long(opts.retries)
except (TypeError, ValueError), err:
parser.error(u'invalid retry count specified')
try: try:
opts.playliststart = long(opts.playliststart) if opts.batchfile == '-':
if opts.playliststart <= 0: batchfd = sys.stdin
raise ValueError else:
except (TypeError, ValueError), err: batchfd = open(opts.batchfile, 'r')
parser.error(u'invalid playlist start number specified') batchurls = batchfd.readlines()
batchurls = [x.strip() for x in batchurls]
batchurls = [x for x in batchurls if len(x) > 0 and not re.search(r'^[#/;]', x)]
except IOError:
sys.exit(u'ERROR: batch file could not be read')
all_urls = batchurls + args
# Conflicting, missing and erroneous options
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 or opts.autonumber):
parser.error(u'using output template conflicts with using title, literal title or auto number')
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 opts.retries is not None:
try: try:
opts.playlistend = long(opts.playlistend) opts.retries = long(opts.retries)
if opts.playlistend != -1 and (opts.playlistend <= 0 or opts.playlistend < opts.playliststart):
raise ValueError
except (TypeError, ValueError), err: except (TypeError, ValueError), err:
parser.error(u'invalid playlist end number specified') parser.error(u'invalid retry count specified')
if opts.extractaudio: try:
if opts.audioformat not in ['best', 'aac', 'mp3']: opts.playliststart = long(opts.playliststart)
parser.error(u'invalid audio format specified') if opts.playliststart <= 0:
raise ValueError
# Information extractors except (TypeError, ValueError), err:
youtube_ie = YoutubeIE() parser.error(u'invalid playlist start number specified')
metacafe_ie = MetacafeIE(youtube_ie) try:
dailymotion_ie = DailymotionIE() opts.playlistend = long(opts.playlistend)
youtube_pl_ie = YoutubePlaylistIE(youtube_ie) if opts.playlistend != -1 and (opts.playlistend <= 0 or opts.playlistend < opts.playliststart):
youtube_user_ie = YoutubeUserIE(youtube_ie) raise ValueError
youtube_search_ie = YoutubeSearchIE(youtube_ie) except (TypeError, ValueError), err:
google_ie = GoogleIE() parser.error(u'invalid playlist end number specified')
google_search_ie = GoogleSearchIE(google_ie) if opts.extractaudio:
photobucket_ie = PhotobucketIE() if opts.audioformat not in ['best', 'aac', 'mp3']:
yahoo_ie = YahooIE() parser.error(u'invalid audio format specified')
yahoo_search_ie = YahooSearchIE(yahoo_ie)
deposit_files_ie = DepositFilesIE() # Information extractors
facebook_ie = FacebookIE() youtube_ie = YoutubeIE()
generic_ie = GenericIE() metacafe_ie = MetacafeIE(youtube_ie)
dailymotion_ie = DailymotionIE()
# File downloader youtube_pl_ie = YoutubePlaylistIE(youtube_ie)
fd = FileDownloader({ youtube_user_ie = YoutubeUserIE(youtube_ie)
'usenetrc': opts.usenetrc, youtube_search_ie = YoutubeSearchIE(youtube_ie)
'username': opts.username, google_ie = GoogleIE()
'password': opts.password, google_search_ie = GoogleSearchIE(google_ie)
'quiet': (opts.quiet or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename), photobucket_ie = PhotobucketIE()
'forceurl': opts.geturl, yahoo_ie = YahooIE()
'forcetitle': opts.gettitle, yahoo_search_ie = YahooSearchIE(yahoo_ie)
'forcethumbnail': opts.getthumbnail, deposit_files_ie = DepositFilesIE()
'forcedescription': opts.getdescription, facebook_ie = FacebookIE()
'forcefilename': opts.getfilename, generic_ie = GenericIE()
'simulate': (opts.simulate or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename),
'format': opts.format, # File downloader
'format_limit': opts.format_limit, fd = FileDownloader({
'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(preferredencoding())) 'usenetrc': opts.usenetrc,
or (opts.format == '-1' and opts.usetitle and u'%(stitle)s-%(id)s-%(format)s.%(ext)s') 'username': opts.username,
or (opts.format == '-1' and opts.useliteral and u'%(title)s-%(id)s-%(format)s.%(ext)s') 'password': opts.password,
or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s') 'quiet': (opts.quiet or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename),
or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(stitle)s-%(id)s.%(ext)s') 'forceurl': opts.geturl,
or (opts.useliteral and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s') 'forcetitle': opts.gettitle,
or (opts.usetitle and u'%(stitle)s-%(id)s.%(ext)s') 'forcethumbnail': opts.getthumbnail,
or (opts.useliteral and u'%(title)s-%(id)s.%(ext)s') 'forcedescription': opts.getdescription,
or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s') 'forcefilename': opts.getfilename,
or u'%(id)s.%(ext)s'), 'simulate': (opts.simulate or opts.geturl or opts.gettitle or opts.getthumbnail or opts.getdescription or opts.getfilename),
'ignoreerrors': opts.ignoreerrors, 'format': opts.format,
'ratelimit': opts.ratelimit, 'format_limit': opts.format_limit,
'nooverwrites': opts.nooverwrites, 'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(preferredencoding()))
'retries': opts.retries, or (opts.format == '-1' and opts.usetitle and u'%(stitle)s-%(id)s-%(format)s.%(ext)s')
'continuedl': opts.continue_dl, or (opts.format == '-1' and opts.useliteral and u'%(title)s-%(id)s-%(format)s.%(ext)s')
'noprogress': opts.noprogress, or (opts.format == '-1' and u'%(id)s-%(format)s.%(ext)s')
'playliststart': opts.playliststart, or (opts.usetitle and opts.autonumber and u'%(autonumber)s-%(stitle)s-%(id)s.%(ext)s')
'playlistend': opts.playlistend, or (opts.useliteral and opts.autonumber and u'%(autonumber)s-%(title)s-%(id)s.%(ext)s')
'logtostderr': opts.outtmpl == '-', or (opts.usetitle and u'%(stitle)s-%(id)s.%(ext)s')
'consoletitle': opts.consoletitle, or (opts.useliteral and u'%(title)s-%(id)s.%(ext)s')
'nopart': opts.nopart, or (opts.autonumber and u'%(autonumber)s-%(id)s.%(ext)s')
'updatetime': opts.updatetime, or u'%(id)s.%(ext)s'),
}) 'ignoreerrors': opts.ignoreerrors,
fd.add_info_extractor(youtube_search_ie) 'ratelimit': opts.ratelimit,
fd.add_info_extractor(youtube_pl_ie) 'nooverwrites': opts.nooverwrites,
fd.add_info_extractor(youtube_user_ie) 'retries': opts.retries,
fd.add_info_extractor(metacafe_ie) 'continuedl': opts.continue_dl,
fd.add_info_extractor(dailymotion_ie) 'noprogress': opts.noprogress,
fd.add_info_extractor(youtube_ie) 'playliststart': opts.playliststart,
fd.add_info_extractor(google_ie) 'playlistend': opts.playlistend,
fd.add_info_extractor(google_search_ie) 'logtostderr': opts.outtmpl == '-',
fd.add_info_extractor(photobucket_ie) 'consoletitle': opts.consoletitle,
fd.add_info_extractor(yahoo_ie) 'nopart': opts.nopart,
fd.add_info_extractor(yahoo_search_ie) 'updatetime': opts.updatetime,
fd.add_info_extractor(deposit_files_ie) })
fd.add_info_extractor(facebook_ie) fd.add_info_extractor(youtube_search_ie)
fd.add_info_extractor(youtube_pl_ie)
# This must come last since it's the fd.add_info_extractor(youtube_user_ie)
# fallback if none of the others work fd.add_info_extractor(metacafe_ie)
fd.add_info_extractor(generic_ie) fd.add_info_extractor(dailymotion_ie)
fd.add_info_extractor(youtube_ie)
# PostProcessors fd.add_info_extractor(google_ie)
if opts.extractaudio: fd.add_info_extractor(google_search_ie)
fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat)) fd.add_info_extractor(photobucket_ie)
fd.add_info_extractor(yahoo_ie)
# Update version fd.add_info_extractor(yahoo_search_ie)
if opts.update_self: fd.add_info_extractor(deposit_files_ie)
updateSelf(fd, sys.argv[0]) fd.add_info_extractor(facebook_ie)
# Maybe do nothing # This must come last since it's the
if len(all_urls) < 1: # fallback if none of the others work
if not opts.update_self: fd.add_info_extractor(generic_ie)
parser.error(u'you must provide at least one URL')
else: # PostProcessors
sys.exit() if opts.extractaudio:
retcode = fd.download(all_urls) fd.add_post_processor(FFmpegExtractAudioPP(preferredcodec=opts.audioformat))
# Update version
if opts.update_self:
updateSelf(fd, sys.argv[0])
# Maybe do nothing
if len(all_urls) < 1:
if not opts.update_self:
parser.error(u'you must provide at least one URL')
else:
sys.exit()
retcode = fd.download(all_urls)
# Dump cookie jar if requested # Dump cookie jar if requested
if opts.cookiefile is not None: if opts.cookiefile is not None:
try: try:
jar.save() jar.save()
except (IOError, OSError), err: except (IOError, OSError), err:
sys.exit(u'ERROR: unable to save cookie jar') sys.exit(u'ERROR: unable to save cookie jar')
sys.exit(retcode) sys.exit(retcode)
if __name__ == '__main__':
try:
main()
except DownloadError: except DownloadError:
sys.exit(1) sys.exit(1)
except SameFileError: except SameFileError:

Loading…
Cancel
Save