Use list comprehension instead of map
[youtube-dl] / youtube_dl / __init__.py
index 5fc39184ac6bd09eeb88d251acf63287b4bff161..f2f238a4fb39f360f001be6f5e023d2abd2fd9a5 100644 (file)
@@ -18,17 +18,17 @@ __authors__  = (
        'Ori Avtalion',
        'shizeeg',
        'Filippo Valsorda',
+       'Christian Albrecht',
        )
 
 __license__ = 'Public Domain'
-__version__ = '2012.11.28'
+__version__ = '2012.11.29'
 
 UPDATE_URL = 'https://raw.github.com/rg3/youtube-dl/master/youtube-dl'
 UPDATE_URL_VERSION = 'https://raw.github.com/rg3/youtube-dl/master/LATEST_VERSION'
 UPDATE_URL_EXE = 'https://raw.github.com/rg3/youtube-dl/master/youtube-dl.exe'
 
 
-import cookielib
 import getpass
 import optparse
 import os
@@ -37,7 +37,6 @@ import shlex
 import socket
 import subprocess
 import sys
-import urllib2
 import warnings
 
 from utils import *
@@ -54,7 +53,7 @@ def updateSelf(downloader, filename):
 
        downloader.to_screen(u'Updating to latest version...')
 
-       urlv = urllib2.urlopen(UPDATE_URL_VERSION)
+       urlv = compat_urllib_request.urlopen(UPDATE_URL_VERSION)
        newversion = urlv.read().strip()
        if newversion == __version__:
                downloader.to_screen(u'youtube-dl is up-to-date (' + __version__ + ')')
@@ -68,12 +67,12 @@ def updateSelf(downloader, filename):
                        sys.exit('ERROR: no write permissions on %s' % directory)
 
                try:
-                       urlh = urllib2.urlopen(UPDATE_URL_EXE)
+                       urlh = compat_urllib_request.urlopen(UPDATE_URL_EXE)
                        newcontent = urlh.read()
                        urlh.close()
                        with open(exe + '.new', 'wb') as outf:
                                outf.write(newcontent)
-               except (IOError, OSError), err:
+               except (IOError, OSError) as err:
                        sys.exit('ERROR: unable to download latest version')
 
                try:
@@ -88,21 +87,21 @@ del "%s"
                        b.close()
 
                        os.startfile(bat)
-               except (IOError, OSError), err:
+               except (IOError, OSError) as err:
                        sys.exit('ERROR: unable to overwrite current version')
 
        else:
                try:
-                       urlh = urllib2.urlopen(UPDATE_URL)
+                       urlh = compat_urllib_request.urlopen(UPDATE_URL)
                        newcontent = urlh.read()
                        urlh.close()
-               except (IOError, OSError), err:
+               except (IOError, OSError) as err:
                        sys.exit('ERROR: unable to download latest version')
 
                try:
                        with open(filename, 'wb') as outf:
                                outf.write(newcontent)
-               except (IOError, OSError), err:
+               except (IOError, OSError) as err:
                        sys.exit('ERROR: unable to overwrite current version')
 
        downloader.to_screen(u'Updated youtube-dl. Restart youtube-dl to use the new version.')
@@ -126,9 +125,12 @@ def parseOpts():
 
                opts = []
 
-               if option._short_opts: opts.append(option._short_opts[0])
-               if option._long_opts: opts.append(option._long_opts[0])
-               if len(opts) > 1: opts.insert(1, ', ')
+               if option._short_opts:
+                       opts.append(option._short_opts[0])
+               if option._long_opts:
+                       opts.append(option._long_opts[0])
+               if len(opts) > 1:
+                       opts.insert(1, ', ')
 
                if option.takes_value(): opts.append(' %s' % option.metavar)
 
@@ -187,6 +189,11 @@ def parseOpts():
                        dest='ratelimit', metavar='LIMIT', help='download rate limit (e.g. 50k or 44.6m)')
        general.add_option('-R', '--retries',
                        dest='retries', metavar='RETRIES', help='number of retries (default is %default)', default=10)
+       general.add_option('--buffer-size',
+                       dest='buffersize', metavar='SIZE', help='size of download buffer (e.g. 1024 or 16k) (default is %default)', default="1024")
+       general.add_option('--no-resize-buffer',
+                       action='store_true', dest='noresizebuffer',
+                       help='do not automatically adjust the buffer size. By default, the buffer size is automatically resized from an initial value of SIZE.', default=False)
        general.add_option('--dump-user-agent',
                        action='store_true', dest='dump_user_agent',
                        help='display the current browser identification', default=False)
@@ -362,7 +369,7 @@ def gen_extractors():
                YoukuIE(),
                XNXXIE(),
                GooglePlusIE(),
-
+               ArteTvIE(),
                GenericIE()
        ]
 
@@ -371,13 +378,13 @@ def _real_main():
 
        # Open appropriate CookieJar
        if opts.cookiefile is None:
-               jar = cookielib.CookieJar()
+               jar = compat_cookiejar.CookieJar()
        else:
                try:
-                       jar = cookielib.MozillaCookieJar(opts.cookiefile)
+                       jar = compat_cookiejar.MozillaCookieJar(opts.cookiefile)
                        if os.path.isfile(opts.cookiefile) and os.access(opts.cookiefile, os.R_OK):
                                jar.load()
-               except (IOError, OSError), err:
+               except (IOError, OSError) as err:
                        sys.exit(u'ERROR: unable to open cookie file')
        # Set user agent
        if opts.user_agent is not None:
@@ -385,7 +392,7 @@ def _real_main():
 
        # Dump user agent
        if opts.dump_user_agent:
-               print std_headers['User-Agent']
+               print(std_headers['User-Agent'])
                sys.exit(0)
 
        # Batch file verification
@@ -402,13 +409,13 @@ def _real_main():
                except IOError:
                        sys.exit(u'ERROR: batch file could not be read')
        all_urls = batchurls + args
-       all_urls = map(lambda url: url.strip(), all_urls)
+       all_urls = [url.strip() for url in all_urls]
 
        # General configuration
-       cookie_processor = urllib2.HTTPCookieProcessor(jar)
-       proxy_handler = urllib2.ProxyHandler()
-       opener = urllib2.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
-       urllib2.install_opener(opener)
+       cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
+       proxy_handler = compat_urllib_request.ProxyHandler()
+       opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
+       compat_urllib_request.install_opener(opener)
        socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
 
        extractors = gen_extractors()
@@ -440,20 +447,25 @@ def _real_main():
                opts.ratelimit = numeric_limit
        if opts.retries is not None:
                try:
-                       opts.retries = long(opts.retries)
-               except (TypeError, ValueError), err:
+                       opts.retries = int(opts.retries)
+               except (TypeError, ValueError) as err:
                        parser.error(u'invalid retry count specified')
+       if opts.buffersize is not None:
+               numeric_buffersize = FileDownloader.parse_bytes(opts.buffersize)
+               if numeric_buffersize is None:
+                       parser.error(u'invalid buffer size specified')
+               opts.buffersize = numeric_buffersize
        try:
                opts.playliststart = int(opts.playliststart)
                if opts.playliststart <= 0:
                        raise ValueError(u'Playlist start must be positive')
-       except (TypeError, ValueError), err:
+       except (TypeError, ValueError) as err:
                parser.error(u'invalid playlist start number specified')
        try:
                opts.playlistend = int(opts.playlistend)
                if opts.playlistend != -1 and (opts.playlistend <= 0 or opts.playlistend < opts.playliststart):
                        raise ValueError(u'Playlist end must be greater than playlist start')
-       except (TypeError, ValueError), err:
+       except (TypeError, ValueError) as err:
                parser.error(u'invalid playlist end number specified')
        if opts.extractaudio:
                if opts.audioformat not in ['best', 'aac', 'mp3', 'vorbis', 'm4a', 'wav']:
@@ -493,6 +505,8 @@ def _real_main():
                'ratelimit': opts.ratelimit,
                'nooverwrites': opts.nooverwrites,
                'retries': opts.retries,
+               'buffersize': opts.buffersize,
+               'noresizebuffer': opts.noresizebuffer,
                'continuedl': opts.continue_dl,
                'noprogress': opts.noprogress,
                'playliststart': opts.playliststart,
@@ -543,7 +557,7 @@ def _real_main():
        if opts.cookiefile is not None:
                try:
                        jar.save()
-               except (IOError, OSError), err:
+               except (IOError, OSError) as err:
                        sys.exit(u'ERROR: unable to save cookie jar')
 
        sys.exit(retcode)