Woooohooo! python3 youtube_dl BaW_jenozKc -t works!
[youtube-dl] / youtube_dl / utils.py
index c4917012bbc8d329a31b1358ece863b86d3f8631..a5df62bf81ce0336ff4d641f8bcc93a1c27ff1c8 100644 (file)
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 import gzip
+import io
 import locale
 import os
 import re
@@ -10,11 +11,6 @@ import zlib
 import email.utils
 import json
 
-try:
-       import cStringIO as StringIO
-except ImportError:
-       import StringIO
-
 try:
        import urllib.request as compat_urllib_request
 except ImportError: # Python 2
@@ -37,14 +33,24 @@ except ImportError: # Python 2
 
 try:
        import html.entities as compat_html_entities
-except NameError: # Python 2
+except ImportError: # Python 2
        import htmlentitydefs as compat_html_entities
 
 try:
        import html.parser as compat_html_parser
-except NameError: # Python 2
+except ImportError: # Python 2
        import HTMLParser as compat_html_parser
 
+try:
+       import http.client as compat_http_client
+except ImportError: # Python 2
+       import httplib as compat_http_client
+
+try:
+       from urllib.parse import parse_qs as compat_parse_qs
+except ImportError: # Python 2
+       from urlparse import parse_qs as compat_parse_qs
+
 try:
        compat_str = unicode # Python 2
 except NameError:
@@ -55,7 +61,6 @@ try:
 except NameError:
        compat_chr = chr
 
-
 std_headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
@@ -77,6 +82,13 @@ def preferredencoding():
 
        return pref
 
+if sys.version_info < (3,0):
+       def compat_print(s):
+               print(s.encode(preferredencoding(), 'xmlcharrefreplace'))
+else:
+       def compat_print(s):
+               assert type(s) == type(u'')
+               print(s)
 
 def htmlentity_transform(matchobj):
        """Transforms an HTML entity to a character.
@@ -280,6 +292,10 @@ def encodeFilename(s):
 
        assert type(s) == type(u'')
 
+       # Python 3 has a Unicode API
+       if sys.version_info >= (3, 0):
+               return s
+
        if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
                # Pass u'' directly to use Unicode APIs on Windows 2000 and up
                # (Detecting Windows NT 4 is tricky because 'major >= 4' would
@@ -400,12 +416,12 @@ class YoutubeDLHandler(compat_urllib_request.HTTPHandler):
                old_resp = resp
                # gzip
                if resp.headers.get('Content-encoding', '') == 'gzip':
-                       gz = gzip.GzipFile(fileobj=StringIO.StringIO(resp.read()), mode='r')
+                       gz = gzip.GzipFile(fileobj=io.BytesIO(resp.read()), mode='r')
                        resp = self.addinfourl_wrapper(gz, old_resp.headers, old_resp.url, old_resp.code)
                        resp.msg = old_resp.msg
                # deflate
                if resp.headers.get('Content-encoding', '') == 'deflate':
-                       gz = StringIO.StringIO(self.deflate(resp.read()))
+                       gz = io.BytesIO(self.deflate(resp.read()))
                        resp = self.addinfourl_wrapper(gz, old_resp.headers, old_resp.url, old_resp.code)
                        resp.msg = old_resp.msg
                return resp