Fix Python 2.4 compatibility
[youtube-dl] / youtube-dl
index 0d77585a80b931a17c08e0ada94b301e448ac5db..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:
@@ -707,8 +710,11 @@ 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
@@ -722,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
@@ -1248,9 +1257,13 @@ class YoutubeIE(InfoExtractor):
                # Decide which formats to download
                req_format = self._downloader.params.get('format', None)
 
-               if 'url_encoded_fmt_stream_map' in video_info and len(video_info['url_encoded_fmt_stream_map']) >= 1:
+               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)
@@ -1272,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
@@ -2757,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:
@@ -2767,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']