2 from __future__
import unicode_literals
11 from .common
import InfoExtractor
12 from ..compat
import (
15 compat_urllib_parse_urlencode
,
19 get_element_by_attribute
,
24 class YoukuIE(InfoExtractor
):
29 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
31 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
36 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
38 'id': 'XMTc1ODE5Njcy_part1',
39 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
43 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
44 'only_matching': True,
46 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
48 'id': 'XODgxNjg1Mzk2',
52 'skip': 'Available in China only',
54 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
56 'id': 'XMTI1OTczNDM5Mg',
61 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
62 'note': 'Video protected with password',
64 'id': 'XNjA1NzA2Njgw',
65 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
69 'videopassword': '100600',
72 # /play/get.json contains streams with "channel_type":"tail"
73 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
75 'id': 'XOTUxMzg4NDMy',
76 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
81 def construct_video_urls(self
, data
):
87 t
= (t
+ ls
[i
] + compat_ord(s1
[i
% len(s1
)])) % 256
88 ls
[i
], ls
[t
] = ls
[t
], ls
[i
]
91 for i
in range(len(s2
)):
94 ls
[x
], ls
[y
] = ls
[y
], ls
[x
]
95 s
.append(compat_ord(s2
[i
]) ^ ls
[(ls
[x
] + ls
[y
]) % 256])
99 b
'becaf9be', base64
.b64decode(data
['security']['encrypt_string'].encode('ascii'))
100 ).decode('ascii').split('_')
103 oip
= data
['security']['ip']
106 for stream
in data
['stream']:
107 if stream
.get('channel_type') == 'tail':
109 format
= stream
.get('stream_type')
111 stream
, lambda x
: x
['segs'][0]['fileid'],
112 compat_str
) or stream
['stream_fileid']
113 fileid_dict
[format
] = fileid
115 def get_fileid(format
, n
):
116 number
= hex(int(str(n
), 10))[2:].upper()
118 number
= '0' + number
119 streamfileids
= fileid_dict
[format
]
120 fileid
= streamfileids
[0:8] + number
+ streamfileids
[10:]
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 data
['stream']:
136 if stream
.get('channel_type') == 'tail':
138 format
= stream
.get('stream_type')
140 for dt
in stream
['segs']:
141 n
= str(stream
['segs'].index(dt
))
144 'hd': self
.get_hd(format
),
151 'ep': generate_ep(format
, n
)
154 'http://k.youku.com/player/getFlvPath/' + \
157 '/st/' + self
.parse_ext_l(format
) + \
158 '/fileid/' + get_fileid(format
, n
) + '?' + \
159 compat_urllib_parse_urlencode(param
)
160 video_urls
.append(video_url
)
161 video_urls_dict
[format
] = video_urls
163 return video_urls_dict
167 return '%d%s' % (int(time
.time()), ''.join([
168 random
.choice(string
.ascii_letters
) for i
in range(3)]))
170 def get_hd(self
, fm
):
183 return hd_id_dict
[fm
]
185 def parse_ext_l(self
, fm
):
200 def get_format_name(self
, fm
):
215 def _real_extract(self
, url
):
216 video_id
= self
._match
_id
(url
)
218 self
._set
_cookie
('youku.com', '__ysuid', self
.get_ysuid())
220 def retrieve_data(req_url
, note
):
224 headers
.update(self
.geo_verification_headers())
225 self
._set
_cookie
('youku.com', 'xreferrer', 'http://www.youku.com')
227 raw_data
= self
._download
_json
(req_url
, video_id
, note
=note
, headers
=headers
)
229 return raw_data
['data']
231 video_password
= self
._downloader
.params
.get('videopassword')
234 basic_data_url
= 'http://play.youku.com/play/get.json?vid=%s&ct=12' % video_id
236 basic_data_url
+= '&pwd=%s' % video_password
238 data
= retrieve_data(basic_data_url
, 'Downloading JSON metadata')
240 error
= data
.get('error')
242 error_note
= error
.get('note')
243 if error_note
is not None and '因版权原因无法观看此视频' in error_note
:
244 raise ExtractorError(
245 'Youku said: Sorry, this video is available in China only', expected
=True)
246 elif error_note
and '该视频被设为私密' in error_note
:
247 raise ExtractorError(
248 'Youku said: Sorry, this video is private', expected
=True)
250 msg
= 'Youku server reported error %i' % error
.get('code')
251 if error_note
is not None:
252 msg
+= ': ' + error_note
253 raise ExtractorError(msg
)
256 title
= data
['video']['title']
258 # generate video_urls_dict
259 video_urls_dict
= self
.construct_video_urls(data
)
263 'id': '%s_part%d' % (video_id
, i
+ 1),
266 # some formats are not available for all parts, we have to detect
268 } for i
in range(max(len(v
.get('segs')) for v
in data
['stream']))]
269 for stream
in data
['stream']:
270 if stream
.get('channel_type') == 'tail':
272 fm
= stream
.get('stream_type')
273 video_urls
= video_urls_dict
[fm
]
274 for video_url
, seg
, entry
in zip(video_urls
, stream
['segs'], entries
):
275 entry
['formats'].append({
277 'format_id': self
.get_format_name(fm
),
278 'ext': self
.parse_ext_l(fm
),
279 'filesize': int(seg
['size']),
280 'width': stream
.get('width'),
281 'height': stream
.get('height'),
285 '_type': 'multi_video',
292 class YoukuShowIE(InfoExtractor
):
293 _VALID_URL
= r
'https?://(?:www\.)?youku\.com/show_page/id_(?P<id>[0-9a-z]+)\.html'
294 IE_NAME
= 'youku:show'
297 'url': 'http://www.youku.com/show_page/id_zc7c670be07ff11e48b3f.html',
299 'id': 'zc7c670be07ff11e48b3f',
301 'description': 'md5:578d4f2145ae3f9128d9d4d863312910',
303 'playlist_count': 50,
308 def _find_videos_in_page(self
, webpage
):
310 r
'<li><a[^>]+href="(?P<url>https?://v\.youku\.com/[^"]+)"[^>]+title="(?P<title>[^"]+)"', webpage
)
312 self
.url_result(video_url
, YoukuIE
.ie_key(), title
)
313 for video_url
, title
in videos
]
315 def _real_extract(self
, url
):
316 show_id
= self
._match
_id
(url
)
317 webpage
= self
._download
_webpage
(url
, show_id
)
319 entries
= self
._find
_videos
_in
_page
(webpage
)
321 playlist_title
= self
._html
_search
_regex
(
322 r
'<span[^>]+class="name">([^<]+)</span>', webpage
, 'playlist title', fatal
=False)
323 detail_div
= get_element_by_attribute('class', 'detail', webpage
) or ''
324 playlist_description
= self
._html
_search
_regex
(
325 r
'<span[^>]+style="display:none"[^>]*>([^<]+)</span>',
326 detail_div
, 'playlist description', fatal
=False)
328 for idx
in itertools
.count(1):
329 episodes_page
= self
._download
_webpage
(
330 'http://www.youku.com/show_episode/id_%s.html' % show_id
,
331 show_id
, query
={'divid': 'reload_%d' % (idx
* self
._PAGE
_SIZE
+ 1)},
332 note
='Downloading episodes page %d' % idx
)
333 new_entries
= self
._find
_videos
_in
_page
(episodes_page
)
334 entries
.extend(new_entries
)
335 if len(new_entries
) < self
._PAGE
_SIZE
:
338 return self
.playlist_result(entries
, show_id
, playlist_title
, playlist_description
)