[youku] Better handling for Python 2/3 compatibility

This commit is contained in:
Yen Chi Hsuan 2015-06-15 23:28:59 +08:00
parent aed473ccf9
commit c203be3fb4

View file

@ -7,9 +7,10 @@
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ExtractorError from ..utils import ExtractorError
from ..compat import compat_urllib_parse from ..compat import (
compat_urllib_parse,
bytes_is_str = (bytes == str) # for compatible compat_ord,
)
class YoukuIE(InfoExtractor): class YoukuIE(InfoExtractor):
@ -37,26 +38,20 @@ def yk_t(s1, s2):
ls = list(range(256)) ls = list(range(256))
t = 0 t = 0
for i in range(256): 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] ls[i], ls[t] = ls[t], ls[i]
s = '' if not bytes_is_str else b'' s = bytearray()
x, y = 0, 0 x, y = 0, 0
for i in range(len(s2)): for i in range(len(s2)):
y = (y + 1) % 256 y = (y + 1) % 256
x = (x + ls[y]) % 256 x = (x + ls[y]) % 256
ls[x], ls[y] = ls[y], ls[x] ls[x], ls[y] = ls[y], ls[x]
if isinstance(s2[i], int): s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
s += chr(s2[i] ^ ls[(ls[x] + ls[y]) % 256]) return bytes(s)
else:
s += chr(ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
return s
sid, token = yk_t( sid, token = yk_t(
'becaf9be', b'becaf9be', base64.b64decode(data2['ep'].encode('ascii'))
base64.b64decode(bytes(data2['ep'], 'ascii')) ).decode('ascii').split('_')
if not bytes_is_str
else base64.b64decode(data2['ep'])
).split('_')
# get oip # get oip
oip = data2['ip'] oip = data2['ip']
@ -89,16 +84,10 @@ def get_fileid(format, n):
def generate_ep(format, n): def generate_ep(format, n):
fileid = get_fileid(format, n) fileid = get_fileid(format, n)
ep_t = yk_t( ep_t = yk_t(
'bf7e5f01', b'bf7e5f01',
bytes('%s_%s_%s' % (sid, fileid, token), 'ascii') ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
if not bytes_is_str
else ('%s_%s_%s' % (sid, fileid, token))
) )
ep = base64.b64encode( ep = base64.b64encode(ep_t).decode('ascii')
bytes(ep_t, 'latin')
if not bytes_is_str
else ep_t
).decode()
return ep return ep
# generate video_urls # generate video_urls