2 from __future__ import unicode_literals
6 from .common import InfoExtractor
11 compat_urllib_request,
20 class YoukuIE(InfoExtractor):
25 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
27 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
31 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
32 'md5': '5f3af4192eabacc4501508d54a8cabd7',
34 'id': 'XMTc1ODE5Njcy_part1',
35 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
39 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
40 'only_matching': True,
42 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
44 'id': 'XODgxNjg1Mzk2',
49 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
51 'id': 'XMTI1OTczNDM5Mg',
55 'skip': 'Available in China only',
57 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
58 'note': 'Video protected with password',
60 'id': 'XNjA1NzA2Njgw',
61 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
65 'videopassword': '100600',
69 def construct_video_urls(self, data1, data2):
75 t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
76 ls[i], ls[t] = ls[t], ls[i]
79 for i in range(len(s2)):
82 ls[x], ls[y] = ls[y], ls[x]
83 s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
87 b'becaf9be', base64.b64decode(data2['ep'].encode('ascii'))
88 ).decode('ascii').split('_')
95 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890')
96 shuffled_string_ls = []
100 seed = (seed * 0xd3 + 0x754f) % 0x10000
101 idx = seed * len(string_ls) // 0x10000
102 shuffled_string_ls.append(string_ls[idx])
106 for format in data1['streamtypes']:
108 int(i) for i in data1['streamfileids'][format].strip('*').split('*')]
110 [shuffled_string_ls[i] for i in streamfileid])
111 fileid_dict[format] = fileid[:8] + '%s' + fileid[10:]
113 def get_fileid(format, n):
114 fileid = fileid_dict[format] % hex(int(n))[2:].upper().zfill(2)
118 def generate_ep(format, n):
119 fileid = get_fileid(format, n)
122 ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
124 ep = base64.b64encode(ep_t).decode('ascii')
127 # generate video_urls
129 for format in data1['streamtypes']:
131 for dt in data1['segs'][format]:
132 n = str(int(dt['no']))
135 'hd': self.get_hd(format),
143 'ep': generate_ep(format, n)
146 'http://k.youku.com/player/getFlvPath/' + \
148 '_' + str(int(n) + 1).zfill(2) + \
149 '/st/' + self.parse_ext_l(format) + \
150 '/fileid/' + get_fileid(format, n) + '?' + \
151 compat_urllib_parse.urlencode(param)
152 video_urls.append(video_url)
153 video_urls_dict[format] = video_urls
155 return video_urls_dict
157 def get_hd(self, fm):
166 return hd_id_dict[fm]
168 def parse_ext_l(self, fm):
179 def get_format_name(self, fm):
190 def _real_extract(self, url):
191 video_id = self._match_id(url)
193 def retrieve_data(req_url, note):
199 self._set_cookie('youku.com','xreferrer','http://www.youku.com')
200 req = sanitized_Request(req_url,headers=headers)
202 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
203 if cn_verification_proxy:
204 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
206 raw_data = self._download_json(req, video_id, note=note)
208 return raw_data['data']['security']
211 video_password = self._downloader.params.get('videopassword', None)
214 #basic_data_url = 'http://v.youku.com/player/getPlayList/VideoIDS/%s' % video_id
215 basic_data_url = "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id
217 basic_data_url += '?password=%s' % video_password
219 data1 = retrieve_data(
221 'Downloading JSON metadata 1')
222 data2 = retrieve_data(
223 #'http://v.youku.com/player/getPlayList/VideoIDS/%s/Pf/4/ctype/12/ev/1' % video_id,
224 "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id,
225 'Downloading JSON metadata 2')
227 error_code = data1.get('error_code')
229 error = data1.get('error')
230 if error is not None and '因版权原因无法观看此视频' in error:
231 raise ExtractorError(
232 'Youku said: Sorry, this video is available in China only', expected=True)
234 msg = 'Youku server reported error %i' % error_code
235 if error is not None:
237 raise ExtractorError(msg)
239 title = data1['title']
241 # generate video_urls_dict
242 video_urls_dict = self.construct_video_urls(data1, data2)
246 'id': '%s_part%d' % (video_id, i + 1),
249 # some formats are not available for all parts, we have to detect
251 } for i in range(max(len(v) for v in data1['segs'].values()))]
252 for fm in data1['streamtypes']:
253 video_urls = video_urls_dict[fm]
254 for video_url, seg, entry in zip(video_urls, data1['segs'][fm], entries):
255 entry['formats'].append({
257 'format_id': self.get_format_name(fm),
258 'ext': self.parse_ext_l(fm),
259 'filesize': int(seg['size']),
263 '_type': 'multi_video',