2 from __future__ import unicode_literals
6 from .common import InfoExtractor
17 class YoukuIE(InfoExtractor):
22 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
24 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
28 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
29 'md5': '5f3af4192eabacc4501508d54a8cabd7',
31 'id': 'XMTc1ODE5Njcy_part1',
32 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
36 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
37 'only_matching': True,
39 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
41 'id': 'XODgxNjg1Mzk2',
46 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
48 'id': 'XMTI1OTczNDM5Mg',
52 'skip': 'Available in China only',
54 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
55 'note': 'Video protected with password',
57 'id': 'XNjA1NzA2Njgw',
58 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
62 'videopassword': '100600',
66 def construct_video_urls(self, data1, data2):
72 t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
73 ls[i], ls[t] = ls[t], ls[i]
76 for i in range(len(s2)):
79 ls[x], ls[y] = ls[y], ls[x]
80 s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
84 b'becaf9be', base64.b64decode(data2['security']['encrypt_string'].encode('ascii'))
85 ).decode('ascii').split('_')
88 oip = data1['security']['ip']
92 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890')
93 shuffled_string_ls = []
94 seed = data1.get('seed')
98 seed = (seed * 0xd3 + 0x754f) % 0x10000
99 idx = seed * len(string_ls) // 0x10000
100 shuffled_string_ls.append(string_ls[idx])
104 for stream in data1['stream']:
105 format = stream.get('stream_type')
107 # int(i) for i in data1['stream']['streamfileids']#[format].strip('*').split('*')]
109 # [shuffled_string_ls[i] for i in streamfileid])
110 fileid = stream['stream_fileid']
111 fileid_dict[format] = fileid
112 #fileid_dict[format] = fileid[:8] + '%s' + fileid[10:]
114 def get_fileid(format, n):
115 #fileid = fileid_dict[format] % hex(int(n))[2:].upper().zfill(2)
116 fileid = fileid_dict[format]
120 def generate_ep(format, n):
121 fileid = get_fileid(format, n)
124 ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
126 ep = base64.b64encode(ep_t).decode('ascii')
129 # generate video_urls
131 for stream in data1['stream']:
132 format = stream.get('stream_type')
134 for dt in stream['segs']:
135 #n = str(int(dt['size']))
136 n = str(stream['segs'].index(dt))
139 'hd': self.get_hd(format),
141 'ts': dt['total_milliseconds_video'],
147 'ep': generate_ep(format, n)
150 'http://k.youku.com/player/getFlvPath/' + \
152 '_' + str(int(n) + 1).zfill(2) + \
153 '/st/' + self.parse_ext_l(format) + \
154 '/fileid/' + get_fileid(format, n) + '?' + \
155 compat_urllib_parse.urlencode(param)
156 video_urls.append(video_url)
157 video_urls_dict[format] = video_urls
159 return video_urls_dict
161 def get_hd(self, fm):
171 return hd_id_dict[fm]
173 def parse_ext_l(self, fm):
187 def get_format_name(self, fm):
199 def _real_extract(self, url):
200 video_id = self._match_id(url)
202 def retrieve_data(req_url, note):
208 self._set_cookie('youku.com','xreferrer','http://www.youku.com')
209 req = sanitized_Request(req_url,headers=headers)
211 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
212 if cn_verification_proxy:
213 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
215 raw_data = self._download_json(req, video_id, note=note)
217 return raw_data['data']
220 video_password = self._downloader.params.get('videopassword', None)
223 #basic_data_url = 'http://v.youku.com/player/getPlayList/VideoIDS/%s' % video_id
224 basic_data_url = "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id
226 basic_data_url += '?password=%s' % video_password
228 data1 = retrieve_data(
230 'Downloading JSON metadata 1')
231 data2 = retrieve_data(
232 #'http://v.youku.com/player/getPlayList/VideoIDS/%s/Pf/4/ctype/12/ev/1' % video_id,
233 "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id,
234 'Downloading JSON metadata 2')
236 error_code = data1.get('error_code')
238 error = data1.get('error')
239 if error is not None and '因版权原因无法观看此视频' in error:
240 raise ExtractorError(
241 'Youku said: Sorry, this video is available in China only', expected=True)
243 msg = 'Youku server reported error %i' % error_code
244 if error is not None:
246 raise ExtractorError(msg)
248 #title = data1['title']
249 title = data1['video']['title']
251 # generate video_urls_dict
252 video_urls_dict = self.construct_video_urls(data1, data2)
256 'id': '%s_part%d' % (video_id, i + 1),
259 # some formats are not available for all parts, we have to detect
261 } for i in range(max(len(v.get('segs')) for v in data1['stream']))]
262 for stream in data1['stream']:
263 fm = stream.get('stream_type')
264 video_urls = video_urls_dict[fm]
265 for video_url, seg, entry in zip(video_urls, stream['segs'], entries):
266 entry['formats'].append({
268 'format_id': self.get_format_name(fm),
269 'ext': self.parse_ext_l(fm),
270 'filesize': int(seg['size']),
274 '_type': 'multi_video',