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 number = hex(int(str(n), 10))[2:].upper()
117 number = '0' + number
118 streamfileids = fileid_dict[format]
119 fileid = streamfileids[0:8] + number + streamfileids[10:]
120 #fileid = fileid_dict[format] % hex(int(n))[2:].upper().zfill(2)
121 #fileid = fileid_dict[format]
125 def generate_ep(format, n):
126 fileid = get_fileid(format, n)
129 ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
131 ep = base64.b64encode(ep_t).decode('ascii')
134 # generate video_urls
136 for stream in data1['stream']:
137 format = stream.get('stream_type')
139 for dt in stream['segs']:
140 #n = str(int(dt['size']))
141 n = str(stream['segs'].index(dt))
144 'hd': self.get_hd(format),
146 #'ts': dt['total_milliseconds_video'],
152 'ep': generate_ep(format, n)
155 'http://k.youku.com/player/getFlvPath/' + \
158 '/st/' + self.parse_ext_l(format) + \
159 '/fileid/' + get_fileid(format, n) + '?' + \
160 compat_urllib_parse.urlencode(param)
161 video_urls.append(video_url)
162 video_urls_dict[format] = video_urls
164 return video_urls_dict
166 def get_hd(self, fm):
176 return hd_id_dict[fm]
178 def parse_ext_l(self, fm):
192 def get_format_name(self, fm):
204 def _real_extract(self, url):
205 video_id = self._match_id(url)
207 def retrieve_data(req_url, note):
213 self._set_cookie('youku.com','xreferrer','http://www.youku.com')
214 req = sanitized_Request(req_url,headers=headers)
216 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
217 if cn_verification_proxy:
218 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
220 raw_data = self._download_json(req, video_id, note=note)
222 return raw_data['data']
225 video_password = self._downloader.params.get('videopassword', None)
228 #basic_data_url = 'http://v.youku.com/player/getPlayList/VideoIDS/%s' % video_id
229 basic_data_url = "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id
231 basic_data_url += '?password=%s' % video_password
233 data1 = retrieve_data(
235 'Downloading JSON metadata 1')
236 data2 = retrieve_data(
237 #'http://v.youku.com/player/getPlayList/VideoIDS/%s/Pf/4/ctype/12/ev/1' % video_id,
238 "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id,
239 'Downloading JSON metadata 2')
241 error_code = data1.get('error_code')
243 error = data1.get('error')
244 if error is not None and '因版权原因无法观看此视频' in error:
245 raise ExtractorError(
246 'Youku said: Sorry, this video is available in China only', expected=True)
248 msg = 'Youku server reported error %i' % error_code
249 if error is not None:
251 raise ExtractorError(msg)
253 #title = data1['title']
254 title = data1['video']['title']
256 # generate video_urls_dict
257 video_urls_dict = self.construct_video_urls(data1, data2)
261 'id': '%s_part%d' % (video_id, i + 1),
264 # some formats are not available for all parts, we have to detect
266 } for i in range(max(len(v.get('segs')) for v in data1['stream']))]
267 for stream in data1['stream']:
268 fm = stream.get('stream_type')
269 video_urls = video_urls_dict[fm]
270 for video_url, seg, entry in zip(video_urls, stream['segs'], entries):
271 entry['formats'].append({
273 'format_id': self.get_format_name(fm),
274 'ext': self.parse_ext_l(fm),
275 'filesize': int(seg['size']),
279 '_type': 'multi_video',