2 from __future__ import unicode_literals
9 from .common import InfoExtractor
10 from ..compat import (
11 compat_urllib_parse_urlencode,
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|)
32 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
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',
48 'skip': 'Available in China only',
50 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
52 'id': 'XMTI1OTczNDM5Mg',
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',
68 # /play/get.json contains streams with "channel_type":"tail"
69 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
71 'id': 'XOTUxMzg4NDMy',
72 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
77 def construct_video_urls(self, data):
83 t = (t + ls[i] + compat_ord(s1[i % len(s1)])) % 256
84 ls[i], ls[t] = ls[t], ls[i]
87 for i in range(len(s2)):
90 ls[x], ls[y] = ls[y], ls[x]
91 s.append(compat_ord(s2[i]) ^ ls[(ls[x] + ls[y]) % 256])
95 b'becaf9be', base64.b64decode(data['security']['encrypt_string'].encode('ascii'))
96 ).decode('ascii').split('_')
99 oip = data['security']['ip']
102 for stream in data['stream']:
103 if stream.get('channel_type') == 'tail':
105 format = stream.get('stream_type')
106 fileid = stream['stream_fileid']
107 fileid_dict[format] = fileid
109 def get_fileid(format, n):
110 number = hex(int(str(n), 10))[2:].upper()
112 number = '0' + number
113 streamfileids = fileid_dict[format]
114 fileid = streamfileids[0:8] + number + streamfileids[10:]
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 stream in data['stream']:
130 if stream.get('channel_type') == 'tail':
132 format = stream.get('stream_type')
134 for dt in stream['segs']:
135 n = str(stream['segs'].index(dt))
138 'hd': self.get_hd(format),
145 'ep': generate_ep(format, n)
148 'http://k.youku.com/player/getFlvPath/' + \
151 '/st/' + self.parse_ext_l(format) + \
152 '/fileid/' + get_fileid(format, n) + '?' + \
153 compat_urllib_parse_urlencode(param)
154 video_urls.append(video_url)
155 video_urls_dict[format] = video_urls
157 return video_urls_dict
161 return '%d%s' % (int(time.time()), ''.join([
162 random.choice(string.ascii_letters) for i in range(3)]))
164 def get_hd(self, fm):
177 return hd_id_dict[fm]
179 def parse_ext_l(self, fm):
194 def get_format_name(self, fm):
209 def _real_extract(self, url):
210 video_id = self._match_id(url)
212 self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
214 def retrieve_data(req_url, note):
218 self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
219 req = sanitized_Request(req_url, headers=headers)
221 cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
222 if cn_verification_proxy:
223 req.add_header('Ytdl-request-proxy', cn_verification_proxy)
225 raw_data = self._download_json(req, video_id, note=note)
227 return raw_data['data']
229 video_password = self._downloader.params.get('videopassword')
232 basic_data_url = 'http://play.youku.com/play/get.json?vid=%s&ct=12' % video_id
234 basic_data_url += '&pwd=%s' % video_password
236 data = retrieve_data(basic_data_url, 'Downloading JSON metadata')
238 error = data.get('error')
240 error_note = error.get('note')
241 if error_note is not None and '因版权原因无法观看此视频' in error_note:
242 raise ExtractorError(
243 'Youku said: Sorry, this video is available in China only', expected=True)
244 elif error_note and '该视频被设为私密' in error_note:
245 raise ExtractorError(
246 'Youku said: Sorry, this video is private', expected=True)
248 msg = 'Youku server reported error %i' % error.get('code')
249 if error_note is not None:
250 msg += ': ' + error_note
251 raise ExtractorError(msg)
254 title = data['video']['title']
256 # generate video_urls_dict
257 video_urls_dict = self.construct_video_urls(data)
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 data['stream']))]
267 for stream in data['stream']:
268 if stream.get('channel_type') == 'tail':
270 fm = stream.get('stream_type')
271 video_urls = video_urls_dict[fm]
272 for video_url, seg, entry in zip(video_urls, stream['segs'], entries):
273 entry['formats'].append({
275 'format_id': self.get_format_name(fm),
276 'ext': self.parse_ext_l(fm),
277 'filesize': int(seg['size']),
281 '_type': 'multi_video',