From c203be3fb4f00388c81564dc0c85ff8a10ff4553 Mon Sep 17 00:00:00 2001 From: Yen Chi Hsuan Date: Mon, 15 Jun 2015 23:28:59 +0800 Subject: [PATCH] [youku] Better handling for Python 2/3 compatibility --- youtube_dl/extractor/youku.py | 37 ++++++++++++----------------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/youtube_dl/extractor/youku.py b/youtube_dl/extractor/youku.py index 4e47fca8a..26e5baadc 100644 --- a/youtube_dl/extractor/youku.py +++ b/youtube_dl/extractor/youku.py @@ -7,9 +7,10 @@ import base64 from .common import InfoExtractor from ..utils import ExtractorError -from ..compat import compat_urllib_parse - -bytes_is_str = (bytes == str) # for compatible +from ..compat import ( + compat_urllib_parse, + compat_ord, +) class YoukuIE(InfoExtractor): @@ -37,26 +38,20 @@ class YoukuIE(InfoExtractor): ls = list(range(256)) t = 0 for i in range(256): - t = (t + ls[i] + ord(s1[i % len(s1)])) % 256 + t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256 ls[i], ls[t] = ls[t], ls[i] - s = '' if not bytes_is_str else b'' + s = bytearray() x, y = 0, 0 for i in range(len(s2)): y = (y + 1) % 256 x = (x + ls[y]) % 256 ls[x], ls[y] = ls[y], ls[x] - if isinstance(s2[i], int): - s += chr(s2[i] ^ ls[(ls[x] + ls[y]) % 256]) - else: - s += chr(ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256]) - return s + s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256]) + return bytes(s) sid, token = yk_t( - 'becaf9be', - base64.b64decode(bytes(data2['ep'], 'ascii')) - if not bytes_is_str - else base64.b64decode(data2['ep']) - ).split('_') + b'becaf9be', base64.b64decode(data2['ep'].encode('ascii')) + ).decode('ascii').split('_') # get oip oip = data2['ip'] @@ -89,16 +84,10 @@ class YoukuIE(InfoExtractor): def generate_ep(format, n): fileid = get_fileid(format, n) ep_t = yk_t( - 'bf7e5f01', - bytes('%s_%s_%s' % (sid, fileid, token), 'ascii') - if not bytes_is_str - else ('%s_%s_%s' % (sid, fileid, token)) + b'bf7e5f01', + ('%s_%s_%s' % (sid, fileid, token)).encode('ascii') ) - ep = base64.b64encode( - bytes(ep_t, 'latin') - if not bytes_is_str - else ep_t - ).decode() + ep = base64.b64encode(ep_t).decode('ascii') return ep # generate video_urls -- 2.22.2