diff options
author | Philipp Hagemeister <phihag@phihag.de> | 2012-11-27 23:54:09 +0100 |
---|---|---|
committer | Philipp Hagemeister <phihag@phihag.de> | 2012-11-27 23:54:09 +0100 |
commit | 01ba00ca42899436c13439226ec61651a6ea6af0 (patch) | |
tree | 42354609b8e268082563a6eb01a77fcb6f40612b /youtube_dl/utils.py | |
parent | e08bee320e7c2933590d108ff4f8546b4dff935f (diff) |
Prepare urllib references for 2/3 compatibility
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r-- | youtube_dl/utils.py | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 29e1b0e97..12e32be98 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -9,7 +9,6 @@ import os import re import sys import zlib -import urllib2 import email.utils import json @@ -31,6 +30,26 @@ try: except NameError: compat_str = str +try: + import urllib.request as compat_urllib_request +except ImportError: # Python 2 + import urllib2 as compat_urllib_request + +try: + import urllib.error as compat_urllib_error +except ImportError: # Python 2 + import urllib2 as compat_urllib_error + +try: + import urllib.parse as compat_urllib_parse +except ImportError: # Python 2 + import urllib2 as compat_urllib_parse + +try: + import http.cookiejar as compat_cookiejar +except ImportError: # Python 2 + import cookielib as compat_cookiejar + def preferredencoding(): """Get preferred encoding. @@ -320,7 +339,7 @@ class Trouble(Exception): FileDownloader.trouble """ -class YoutubeDLHandler(urllib2.HTTPHandler): +class YoutubeDLHandler(compat_urllib_request.HTTPHandler): """Handler for HTTP requests and responses. This class, when installed with an OpenerDirector, automatically adds @@ -347,9 +366,9 @@ class YoutubeDLHandler(urllib2.HTTPHandler): @staticmethod def addinfourl_wrapper(stream, headers, url, code): - if hasattr(urllib2.addinfourl, 'getcode'): - return urllib2.addinfourl(stream, headers, url, code) - ret = urllib2.addinfourl(stream, headers, url) + if hasattr(compat_urllib_request.addinfourl, 'getcode'): + return compat_urllib_request.addinfourl(stream, headers, url, code) + ret = compat_urllib_request.addinfourl(stream, headers, url) ret.code = code return ret |