2 from __future__ import unicode_literals
7 from .common import InfoExtractor
12 compat_urllib_request,
21 class YoukuIE(InfoExtractor):
26 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
28 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
32 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
33 'md5': '5f3af4192eabacc4501508d54a8cabd7',
35 'id': 'XMTc1ODE5Njcy_part1',
36 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
40 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
41 'only_matching': True,
43 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
45 'id': 'XODgxNjg1Mzk2',
50 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
52 'id': 'XMTI1OTczNDM5Mg',
56 'skip': 'Available in China only',
58 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
59 'note': 'Video protected with password',
61 'id': 'XNjA1NzA2Njgw',
62 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
66 'videopassword': '100600',
70 def construct_video_urls(self, data1, data2):
76 t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
77 ls[i], ls[t] = ls[t], ls[i]
80 for i in range(len(s2)):
83 ls[x], ls[y] = ls[y], ls[x]
84 s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
88 b'becaf9be', base64.b64decode(data2['security']['encrypt_string'].encode('ascii'))
89 ).decode('ascii').split('_')
92 oip = data2['security']['ip']
96 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\:._-1234567890')
97 shuffled_string_ls = []
98 seed = data1.get('seed')
102 seed = (seed * 0xd3 + 0x754f) % 0x10000
103 idx = seed * len(string_ls) // 0x10000
104 shuffled_string_ls.append(string_ls[idx])
108 for stream in data1['stream']:
109 format = stream.get('stream_type')
111 # int(i) for i in data1['stream']['streamfileids']#[format].strip('*').split('*')]
113 # [shuffled_string_ls[i] for i in streamfileid])
114 fileid = stream['stream_fileid']
115 fileid_dict[format] = fileid
116 #fileid_dict[format] = fileid[:8] + '%s' + fileid[10:]
118 def get_fileid(format, n):
119 #fileid = fileid_dict[format] % hex(int(n))[2:].upper().zfill(2)
120 fileid = fileid_dict[format]
124 def generate_ep(format, n):
125 fileid = get_fileid(format, n)
128 ('%s_%s_%s' % (sid, fileid, token)).encode('ascii')
130 ep = base64.b64encode(ep_t).decode('ascii')
133 # generate video_urls
135 for stream in data1['stream']:
136 format = stream.get('stream_type')
138 for dt in stream['segs']:
139 n = str(int(dt['size']))
142 'hd': self.get_hd(format),
144 'ts': dt['total_milliseconds_video'],
150 'ep': generate_ep(format, n)
153 'http://k.youku.com/player/getFlvPath/' + \
155 '_' + str(int(n) + 1).zfill(2) + \
156 '/st/' + self.parse_ext_l(format) + \
157 '/fileid/' + get_fileid(format, n) + '?' + \
158 compat_urllib_parse.urlencode(param)
159 video_urls.append(video_url)
160 video_urls_dict[format] = video_urls
162 return video_urls_dict
164 def get_hd(self, fm):
174 return hd_id_dict[fm]
176 def parse_ext_l(self, fm):
188 def get_format_name(self, fm):
200 def _real_extract(self, url):
201 video_id = self._match_id(url)
203 def retrieve_data(req_url, note):
209 self._set_cookie('youku.com','xreferrer','http://www.youku.com')
210 req = sanitized_Request(req_url,headers=headers)
212 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
213 if cn_verification_proxy:
214 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
216 raw_data = self._download_json(req, video_id, note=note)
217 jsonDumpIn = json.dumps(raw_data,indent = 1)
219 return raw_data['data']
222 video_password = self._downloader.params.get('videopassword', None)
225 #basic_data_url = 'http://v.youku.com/player/getPlayList/VideoIDS/%s' % video_id
226 basic_data_url = "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id
228 basic_data_url += '?password=%s' % video_password
230 data1 = retrieve_data(
232 'Downloading JSON metadata 1')
233 data2 = retrieve_data(
234 #'http://v.youku.com/player/getPlayList/VideoIDS/%s/Pf/4/ctype/12/ev/1' % video_id,
235 "http://play.youku.com/play/get.json?vid=%s&ct=12" % video_id,
236 'Downloading JSON metadata 2')
238 error_code = data1.get('error_code')
240 error = data1.get('error')
241 if error is not None and '因版权原因无法观看此视频' in error:
242 raise ExtractorError(
243 'Youku said: Sorry, this video is available in China only', expected=True)
245 msg = 'Youku server reported error %i' % error_code
246 if error is not None:
248 raise ExtractorError(msg)
250 #title = data1['title']
251 title = data1['video']['title']
253 # generate video_urls_dict
254 video_urls_dict = self.construct_video_urls(data1, data2)
258 'id': '%s_part%d' % (video_id, i + 1),
261 # some formats are not available for all parts, we have to detect
263 } for i in range(max(len(v) for v in data1['stream']))]
264 for stream in data1['stream']:
265 fm = stream.get('stream_type')
266 video_urls = video_urls_dict[fm]
267 for video_url, seg, entry in zip(video_urls, stream['segs'], entries):
268 entry['formats'].append({
270 'format_id': self.get_format_name(fm),
271 'ext': self.parse_ext_l(fm),
272 'filesize': int(seg['size']),
276 '_type': 'multi_video',