2 from __future__
import unicode_literals
11 from .common
import InfoExtractor
12 from ..compat
import (
13 compat_urllib_parse_urlencode
,
18 get_element_by_attribute
,
23 class YoukuIE(InfoExtractor
):
28 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
30 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
35 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
37 'id': 'XMTc1ODE5Njcy_part1',
38 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
42 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
43 'only_matching': True,
45 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
47 'id': 'XODgxNjg1Mzk2',
51 'skip': 'Available in China only',
53 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
55 'id': 'XMTI1OTczNDM5Mg',
60 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
61 'note': 'Video protected with password',
63 'id': 'XNjA1NzA2Njgw',
64 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
68 'videopassword': '100600',
71 # /play/get.json contains streams with "channel_type":"tail"
72 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
74 'id': 'XOTUxMzg4NDMy',
75 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
80 def construct_video_urls(self
, data
):
86 t
= (t
+ ls
[i
] + compat_ord(s1
[i
% len(s1
)])) % 256
87 ls
[i
], ls
[t
] = ls
[t
], ls
[i
]
90 for i
in range(len(s2
)):
93 ls
[x
], ls
[y
] = ls
[y
], ls
[x
]
94 s
.append(compat_ord(s2
[i
]) ^ ls
[(ls
[x
] + ls
[y
]) % 256])
98 b
'becaf9be', base64
.b64decode(data
['security']['encrypt_string'].encode('ascii'))
99 ).decode('ascii').split('_')
102 oip
= data
['security']['ip']
105 for stream
in data
['stream']:
106 if stream
.get('channel_type') == 'tail':
108 format
= stream
.get('stream_type')
109 fileid
= stream
['stream_fileid']
110 fileid_dict
[format
] = fileid
112 def get_fileid(format
, n
):
113 number
= hex(int(str(n
), 10))[2:].upper()
115 number
= '0' + number
116 streamfileids
= fileid_dict
[format
]
117 fileid
= streamfileids
[0:8] + number
+ streamfileids
[10:]
121 def generate_ep(format
, n
):
122 fileid
= get_fileid(format
, n
)
125 ('%s_%s_%s' % (sid
, fileid
, token
)).encode('ascii')
127 ep
= base64
.b64encode(ep_t
).decode('ascii')
130 # generate video_urls
132 for stream
in data
['stream']:
133 if stream
.get('channel_type') == 'tail':
135 format
= stream
.get('stream_type')
137 for dt
in stream
['segs']:
138 n
= str(stream
['segs'].index(dt
))
141 'hd': self
.get_hd(format
),
148 'ep': generate_ep(format
, n
)
151 'http://k.youku.com/player/getFlvPath/' + \
154 '/st/' + self
.parse_ext_l(format
) + \
155 '/fileid/' + get_fileid(format
, n
) + '?' + \
156 compat_urllib_parse_urlencode(param
)
157 video_urls
.append(video_url
)
158 video_urls_dict
[format
] = video_urls
160 return video_urls_dict
164 return '%d%s' % (int(time
.time()), ''.join([
165 random
.choice(string
.ascii_letters
) for i
in range(3)]))
167 def get_hd(self
, fm
):
180 return hd_id_dict
[fm
]
182 def parse_ext_l(self
, fm
):
197 def get_format_name(self
, fm
):
212 def _real_extract(self
, url
):
213 video_id
= self
._match
_id
(url
)
215 self
._set
_cookie
('youku.com', '__ysuid', self
.get_ysuid())
217 def retrieve_data(req_url
, note
):
221 self
._set
_cookie
('youku.com', 'xreferrer', 'http://www.youku.com')
222 req
= sanitized_Request(req_url
, headers
=headers
)
224 cn_verification_proxy
= self
._downloader
.params
.get('cn_verification_proxy')
225 if cn_verification_proxy
:
226 req
.add_header('Ytdl-request-proxy', cn_verification_proxy
)
228 raw_data
= self
._download
_json
(req
, video_id
, note
=note
)
230 return raw_data
['data']
232 video_password
= self
._downloader
.params
.get('videopassword')
235 basic_data_url
= 'http://play.youku.com/play/get.json?vid=%s&ct=12' % video_id
237 basic_data_url
+= '&pwd=%s' % video_password
239 data
= retrieve_data(basic_data_url
, 'Downloading JSON metadata')
241 error
= data
.get('error')
243 error_note
= error
.get('note')
244 if error_note
is not None and '因版权原因无法观看此视频' in error_note
:
245 raise ExtractorError(
246 'Youku said: Sorry, this video is available in China only', expected
=True)
247 elif error_note
and '该视频被设为私密' in error_note
:
248 raise ExtractorError(
249 'Youku said: Sorry, this video is private', expected
=True)
251 msg
= 'Youku server reported error %i' % error
.get('code')
252 if error_note
is not None:
253 msg
+= ': ' + error_note
254 raise ExtractorError(msg
)
257 title
= data
['video']['title']
259 # generate video_urls_dict
260 video_urls_dict
= self
.construct_video_urls(data
)
264 'id': '%s_part%d' % (video_id
, i
+ 1),
267 # some formats are not available for all parts, we have to detect
269 } for i
in range(max(len(v
.get('segs')) for v
in data
['stream']))]
270 for stream
in data
['stream']:
271 if stream
.get('channel_type') == 'tail':
273 fm
= stream
.get('stream_type')
274 video_urls
= video_urls_dict
[fm
]
275 for video_url
, seg
, entry
in zip(video_urls
, stream
['segs'], entries
):
276 entry
['formats'].append({
278 'format_id': self
.get_format_name(fm
),
279 'ext': self
.parse_ext_l(fm
),
280 'filesize': int(seg
['size']),
281 'width': stream
.get('width'),
282 'height': stream
.get('height'),
286 '_type': 'multi_video',
293 class YoukuShowIE(InfoExtractor
):
294 _VALID_URL
= r
'https?://(?:www\.)?youku\.com/show_page/id_(?P<id>[0-9a-z]+)\.html'
295 IE_NAME
= 'youku:show'
298 'url': 'http://www.youku.com/show_page/id_zc7c670be07ff11e48b3f.html',
300 'id': 'zc7c670be07ff11e48b3f',
302 'description': 'md5:578d4f2145ae3f9128d9d4d863312910',
304 'playlist_count': 50,
309 def _find_videos_in_page(self
, webpage
):
311 r
'<li><a[^>]+href="(?P<url>https?://v\.youku\.com/[^"]+)"[^>]+title="(?P<title>[^"]+)"', webpage
)
313 self
.url_result(video_url
, YoukuIE
.ie_key(), title
)
314 for video_url
, title
in videos
]
316 def _real_extract(self
, url
):
317 show_id
= self
._match
_id
(url
)
318 webpage
= self
._download
_webpage
(url
, show_id
)
320 entries
= self
._find
_videos
_in
_page
(webpage
)
322 playlist_title
= self
._html
_search
_regex
(
323 r
'<span[^>]+class="name">([^<]+)</span>', webpage
, 'playlist title', fatal
=False)
324 detail_div
= get_element_by_attribute('class', 'detail', webpage
) or ''
325 playlist_description
= self
._html
_search
_regex
(
326 r
'<span[^>]+style="display:none"[^>]*>([^<]+)</span>',
327 detail_div
, 'playlist description', fatal
=False)
329 for idx
in itertools
.count(1):
330 episodes_page
= self
._download
_webpage
(
331 'http://www.youku.com/show_episode/id_%s.html' % show_id
,
332 show_id
, query
={'divid': 'reload_%d' % (idx
* self
._PAGE
_SIZE
+ 1)},
333 note
='Downloading episodes page %d' % idx
)
334 new_entries
= self
._find
_videos
_in
_page
(episodes_page
)
335 entries
.extend(new_entries
)
336 if len(new_entries
) < self
._PAGE
_SIZE
:
339 return self
.playlist_result(entries
, show_id
, playlist_title
, playlist_description
)