X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Futils.py;h=95bd948438830ca7f98ad82decb722de69319bdf;hb=61e40c88a989d31b6f06d7001f614d62f06941a5;hp=08be9e6373f6d4129327a6125b7d3b8c863273c0;hpb=3446dfb7cb84025f67250be069e44020d3606b84;p=youtube-dl diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index 08be9e637..95bd94843 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -280,6 +280,12 @@ class AttrParser(compat_html_parser.HTMLParser): lines[-1] = lines[-1][:self.result[2][1]-self.result[1][1]] lines[-1] = lines[-1][:self.result[2][1]] return '\n'.join(lines).strip() +# Hack for https://github.com/rg3/youtube-dl/issues/662 +if sys.version_info < (2, 7, 3): + AttrParser.parse_endtag = (lambda self, i: + i + len("") + if self.rawdata[i:].startswith("") + else compat_html_parser.HTMLParser.parse_endtag(self, i)) def get_element_by_id(id, html): """Return the content of the tag with the specified ID in the passed HTML document""" @@ -409,8 +415,19 @@ def encodeFilename(s): # match Windows 9x series as well. Besides, NT 4 is obsolete.) return s else: - return s.encode(sys.getfilesystemencoding(), 'ignore') + encoding = sys.getfilesystemencoding() + if encoding is None: + encoding = 'utf-8' + return s.encode(encoding, 'ignore') + +def decodeOption(optval): + if optval is None: + return optval + if isinstance(optval, bytes): + optval = optval.decode(preferredencoding()) + assert isinstance(optval, compat_str) + return optval class ExtractorError(Exception): """Error during info extraction.""" @@ -516,18 +533,18 @@ class YoutubeDLHandler(compat_urllib_request.HTTPHandler): return ret def http_request(self, req): - for h in std_headers: + for h,v in std_headers.items(): if h in req.headers: del req.headers[h] - req.add_header(h, std_headers[h]) + req.add_header(h, v) if 'Youtubedl-no-compression' in req.headers: if 'Accept-encoding' in req.headers: del req.headers['Accept-encoding'] del req.headers['Youtubedl-no-compression'] if 'Youtubedl-user-agent' in req.headers: - if 'User-Agent' in req.headers: - del req.headers['User-Agent'] - req.headers['User-Agent'] = req.headers['Youtubedl-user-agent'] + if 'User-agent' in req.headers: + del req.headers['User-agent'] + req.headers['User-agent'] = req.headers['Youtubedl-user-agent'] del req.headers['Youtubedl-user-agent'] return req