Fix Python 2.4 compatibility
[youtube-dl] / youtube-dl
index 578f473b88a484126d9666a2d26d9cbae3ffb7a9..81dd4b83bfcca01d5c502856db47a015ae487530 100755 (executable)
@@ -9,12 +9,8 @@
 # Author: Gergely Imreh
 # Author: Philipp Hagemeister <phihag@phihag.de>
 # License: Public domain code
-from __future__ import with_statement
-import contextlib
 import cookielib
-import ctypes
 import datetime
-import email.utils
 import gzip
 import htmlentitydefs
 import httplib
@@ -34,6 +30,13 @@ import urllib2
 import warnings
 import zlib
 
+if os.name == 'nt':
+       import ctypes
+
+try:
+       import email.utils
+except ImportError: # Python 2.4
+       import email.Utils
 try:
        import cStringIO as StringIO
 except ImportError:
@@ -62,7 +65,7 @@ simple_title_chars = string.ascii_letters.decode('ascii') + string.digits.decode
 
 try:
        import json
-except ImportError: # Python <2.5, use trivialjson (https://github.com/phihag/trivialjson):
+except ImportError: # Python <2.6, use trivialjson (https://github.com/phihag/trivialjson):
        import re
        class json(object):
                @staticmethod
@@ -707,13 +710,15 @@ class FileDownloader(object):
                        try:
                                descfn = filename + '.description'
                                self.report_writedescription(descfn)
-                               with contextlib.closing(open(descfn, 'wb')) as descfile:
+                               descfile = open(descfn, 'wb')
+                               try:
                                        descfile.write(info_dict['description'].encode('utf-8'))
+                               finally:
+                                       descfile.close()
                        except (OSError, IOError):
                                self.trouble(u'ERROR: Cannot write description file: %s' % str(descfn))
                                return
 
-               print(repr(self.params))
                if self.params.get('writeinfojson', False):
                        infofn = filename + '.info.json'
                        self.report_writeinfojson(infofn)
@@ -723,8 +728,11 @@ class FileDownloader(object):
                                self.trouble(u'ERROR: No JSON encoder found. Update to Python 2.6+, setup a json module, or leave out --write-info-json.')
                                return
                        try:
-                               with contextlib.closing(open(infofn, 'wb')) as infof:
+                               infof = open(infofn, 'wb')
+                               try:
                                        json.dump(info_dict, infof)
+                               finally:
+                                       infof.close()
                        except (OSError, IOError):
                                self.trouble(u'ERROR: Cannot write metadata to JSON file: %s' % str(infofn))
                                return
@@ -1234,7 +1242,6 @@ class YoutubeIE(InfoExtractor):
                except NameError:
                        video_description = u'No description available.'
                        if self._downloader.params.get('forcedescription', False) or self._downloader.params.get('writedescription', False):
-                               warnings.warn(u'You are using an old Python version, install Python 2.6+ or lxml. Falling back to old video description extractor.')
                                mobj = re.search(r'<meta name="description" content="(.*)"(?:\s*/)?>', video_webpage)
                                if mobj is not None:
                                        video_description = mobj.group(1).decode('utf-8')
@@ -1242,6 +1249,7 @@ class YoutubeIE(InfoExtractor):
                        html_parser = lxml.etree.HTMLParser(encoding='utf-8')
                        vwebpage_doc = lxml.etree.parse(StringIO.StringIO(video_webpage), html_parser)
                        video_description = u''.join(vwebpage_doc.xpath('id("eow-description")//text()'))
+                       # TODO use another parser
 
                # token
                video_token = urllib.unquote_plus(video_info['token'][0])
@@ -1249,8 +1257,15 @@ class YoutubeIE(InfoExtractor):
                # Decide which formats to download
                req_format = self._downloader.params.get('format', None)
 
-               if 'fmt_url_map' in video_info and len(video_info['fmt_url_map']) >= 1 and ',' in video_info['fmt_url_map'][0]:
-                       url_map = dict(tuple(pair.split('|')) for pair in video_info['fmt_url_map'][0].split(','))
+               if 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
+                       self.report_rtmp_download()
+                       video_url_list = [(None, video_info['conn'][0])]
+               elif 'url_encoded_fmt_stream_map' in video_info and len(video_info['url_encoded_fmt_stream_map']) >= 1:
+                       url_data_strs = video_info['url_encoded_fmt_stream_map'][0].split(',')
+                       url_data = [dict(pairStr.split('=') for pairStr in uds.split('&')) for uds in url_data_strs]
+                       url_data = filter(lambda ud: 'itag' in ud and 'url' in ud, url_data)
+                       url_map = dict((ud['itag'], urllib.unquote(ud['url'])) for ud in url_data)
+                       
                        format_limit = self._downloader.params.get('format_limit', None)
                        if format_limit is not None and format_limit in self._available_formats:
                                format_list = self._available_formats[self._available_formats.index(format_limit):]
@@ -1270,11 +1285,6 @@ class YoutubeIE(InfoExtractor):
                                        self._downloader.trouble(u'ERROR: requested format not available')
                                        return
                                video_url_list = [(req_format, url_map[req_format])] # Specific format
-
-               elif 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
-                       self.report_rtmp_download()
-                       video_url_list = [(None, video_info['conn'][0])]
-
                else:
                        self._downloader.trouble(u'ERROR: no fmt_url_map or conn information found in video info')
                        return
@@ -2755,7 +2765,11 @@ class BlipTVIE(InfoExtractor):
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
                        return
 
-               json_url = url + ('&' if '?' in url else '?') + 'skin=json&version=2&no_wrap=1'
+               if '?' in url:
+                       cchar = '&'
+               else:
+                       cchar = '?'
+               json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
                request = urllib2.Request(json_url)
                self.report_extraction(mobj.group(1))
                try:
@@ -2765,7 +2779,10 @@ class BlipTVIE(InfoExtractor):
                        return
                try:
                        json_data = json.loads(json_code)
-                       data = json_data['Post'] if 'Post' in json_data else json_data
+                       if 'Post' in json_data:
+                               data = json_data['Post']
+                       else:
+                               data = json_data
 
                        upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
                        video_url = data['media']['url']