2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from ..utils import ExtractorError
12 compat_urllib_request,
16 class YoukuIE(InfoExtractor):
20 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
22 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
26 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
27 'md5': '5f3af4192eabacc4501508d54a8cabd7',
29 'id': 'XMTc1ODE5Njcy_part1',
30 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
34 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
35 'only_matching': True,
37 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
39 'id': 'XODgxNjg1Mzk2',
44 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
46 'id': 'XMTI1OTczNDM5Mg',
50 'skip': 'Available in China only',
53 def construct_video_urls(self, data1, data2):
59 t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
60 ls[i], ls[t] = ls[t], ls[i]
63 for i in range(len(s2)):
66 ls[x], ls[y] = ls[y], ls[x]
67 s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
71 b'becaf9be', base64.b64decode(data2['ep'].encode('ascii'))
72 ).decode('ascii').split('_')
79 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890')
80 shuffled_string_ls = []
84 seed = (seed * 0xd3 + 0x754f) % 0x10000
85 idx = seed * len(string_ls) // 0x10000
86 shuffled_string_ls.append(string_ls[idx])
90 for format in data1['streamtypes']:
92 int(i) for i in data1['streamfileids'][format].strip('*').split('*')]
94 [shuffled_string_ls[i] for i in streamfileid])
95 fileid_dict[format] = fileid[:8] + '%s' + fileid[10:]
97 def get_fileid(format, n):
98 fileid = fileid_dict[format] % hex(int(n))[2:].upper().zfill(2)
102 def generate_ep(format, n):
103 fileid = get_fileid(format, n)
106 ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
108 ep = base64.b64encode(ep_t).decode('ascii')
111 # generate video_urls
113 for format in data1['streamtypes']:
115 for dt in data1['segs'][format]:
116 n = str(int(dt['no']))
119 'hd': self.get_hd(format),
127 'ep': generate_ep(format, n)
130 'http://k.youku.com/player/getFlvPath/' + \
132 '_' + str(int(n) + 1).zfill(2) + \
133 '/st/' + self.parse_ext_l(format) + \
134 '/fileid/' + get_fileid(format, n) + '?' + \
135 compat_urllib_parse.urlencode(param)
136 video_urls.append(video_url)
137 video_urls_dict[format] = video_urls
139 return video_urls_dict
141 def get_hd(self, fm):
150 return hd_id_dict[fm]
152 def parse_ext_l(self, fm):
163 def get_format_name(self, fm):
174 def _real_extract(self, url):
175 video_id = self._match_id(url)
177 def retrieve_data(req_url, note):
178 req = compat_urllib_request.Request(req_url)
180 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
181 if cn_verification_proxy:
182 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
184 raw_data = self._download_json(req, video_id, note=note)
185 return raw_data['data'][0]
188 data1 = retrieve_data(
189 'http://v.youku.com/player/getPlayList/VideoIDS/%s' % video_id,
190 'Downloading JSON metadata 1')
191 data2 = retrieve_data(
192 'http://v.youku.com/player/getPlayList/VideoIDS/%s/Pf/4/ctype/12/ev/1' % video_id,
193 'Downloading JSON metadata 2')
195 error_code = data1.get('error_code')
197 error = data1.get('error')
198 if error is not None and '因版权原因无法观看此视频' in error:
199 raise ExtractorError(
200 'Youku said: Sorry, this video is available in China only', expected=True)
202 msg = 'Youku server reported error %i' % error_code
203 if error is not None:
205 raise ExtractorError(msg)
207 title = data1['title']
209 # generate video_urls_dict
210 video_urls_dict = self.construct_video_urls(data1, data2)
214 'id': '%s_part%d' % (video_id, i + 1),
217 # some formats are not available for all parts, we have to detect
219 } for i in range(max(len(v) for v in data1['segs'].values()))]
220 for fm in data1['streamtypes']:
221 video_urls = video_urls_dict[fm]
222 for video_url, seg, entry in zip(video_urls, data1['segs'][fm], entries):
223 entry['formats'].append({
225 'format_id': self.get_format_name(fm),
226 'ext': self.parse_ext_l(fm),
227 'filesize': int(seg['size']),
231 '_type': 'multi_video',