]>
Commit | Line | Data |
---|---|---|
034caf70 | 1 | import random |
411c590a | 2 | import re |
034caf70 YCH |
3 | import string |
4 | import time | |
9c286cfa PH |
5 | |
6 | from .common import InfoExtractor | |
5c2266df S |
7 | from ..utils import ( |
8 | ExtractorError, | |
d3d4ba7f YCH |
9 | get_element_by_class, |
10 | js_to_json, | |
d16c0121 | 11 | str_or_none, |
d3d4ba7f | 12 | strip_jsonp, |
c203be3f | 13 | ) |
1498940b | 14 | |
aed473cc | 15 | |
9c286cfa | 16 | class YoukuIE(InfoExtractor): |
f9355dc9 | 17 | IE_NAME = 'youku' |
246995db | 18 | IE_DESC = '优酷' |
8a32b82e PH |
19 | _VALID_URL = r'''(?x) |
20 | (?: | |
c130f0a3 YCH |
21 | https?://( |
22 | (?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)| | |
23 | video\.tudou\.com/v/)| | |
8a32b82e PH |
24 | youku:) |
25 | (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|) | |
26 | ''' | |
f9355dc9 | 27 | |
ee697992 | 28 | _TESTS = [{ |
c683454e | 29 | # MD5 is unstable |
aed473cc | 30 | 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html', |
aed473cc | 31 | 'info_dict': { |
59ed87cb | 32 | 'id': 'XMTc1ODE5Njcy', |
aed473cc | 33 | 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.', |
59ed87cb | 34 | 'ext': 'mp4', |
d16c0121 YCH |
35 | 'duration': 74.73, |
36 | 'thumbnail': r're:^https?://.*', | |
37 | 'uploader': '。躲猫猫、', | |
38 | 'uploader_id': '36017967', | |
39 | 'uploader_url': 'http://i.youku.com/u/UMTQ0MDcxODY4', | |
40 | 'tags': list, | |
aed473cc | 41 | } |
ee697992 YCH |
42 | }, { |
43 | 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf', | |
44 | 'only_matching': True, | |
f1e66cb2 YCH |
45 | }, { |
46 | 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html', | |
47 | 'info_dict': { | |
48 | 'id': 'XODgxNjg1Mzk2', | |
59ed87cb | 49 | 'ext': 'mp4', |
f1e66cb2 | 50 | 'title': '武媚娘传奇 85', |
d16c0121 YCH |
51 | 'duration': 1999.61, |
52 | 'thumbnail': r're:^https?://.*', | |
53 | 'uploader': '疯狂豆花', | |
54 | 'uploader_id': '62583473', | |
55 | 'uploader_url': 'http://i.youku.com/u/UMjUwMzMzODky', | |
56 | 'tags': list, | |
f1e66cb2 | 57 | }, |
5228b756 YCH |
58 | }, { |
59 | 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html', | |
60 | 'info_dict': { | |
61 | 'id': 'XMTI1OTczNDM5Mg', | |
59ed87cb | 62 | 'ext': 'mp4', |
5228b756 | 63 | 'title': '花千骨 04', |
d16c0121 YCH |
64 | 'duration': 2363, |
65 | 'thumbnail': r're:^https?://.*', | |
66 | 'uploader': '放剧场-花千骨', | |
67 | 'uploader_id': '772849359', | |
68 | 'uploader_url': 'http://i.youku.com/u/UMzA5MTM5NzQzNg==', | |
69 | 'tags': list, | |
5228b756 | 70 | }, |
33eae08f P |
71 | }, { |
72 | 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html', | |
73 | 'note': 'Video protected with password', | |
74 | 'info_dict': { | |
75 | 'id': 'XNjA1NzA2Njgw', | |
59ed87cb | 76 | 'ext': 'mp4', |
5ddc127d | 77 | 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起', |
d16c0121 YCH |
78 | 'duration': 7264.5, |
79 | 'thumbnail': r're:^https?://.*', | |
80 | 'uploader': 'FoxJin1006', | |
81 | 'uploader_id': '322014285', | |
82 | 'uploader_url': 'http://i.youku.com/u/UMTI4ODA1NzE0MA==', | |
83 | 'tags': list, | |
33eae08f P |
84 | }, |
85 | 'params': { | |
86 | 'videopassword': '100600', | |
87 | }, | |
eb01e97e YCH |
88 | }, { |
89 | # /play/get.json contains streams with "channel_type":"tail" | |
90 | 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html', | |
91 | 'info_dict': { | |
92 | 'id': 'XOTUxMzg4NDMy', | |
59ed87cb | 93 | 'ext': 'mp4', |
eb01e97e | 94 | 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft', |
d16c0121 YCH |
95 | 'duration': 702.08, |
96 | 'thumbnail': r're:^https?://.*', | |
97 | 'uploader': '明月庄主moon', | |
98 | 'uploader_id': '38465621', | |
99 | 'uploader_url': 'http://i.youku.com/u/UMTUzODYyNDg0', | |
100 | 'tags': list, | |
eb01e97e | 101 | }, |
c130f0a3 YCH |
102 | }, { |
103 | 'url': 'http://video.tudou.com/v/XMjIyNzAzMTQ4NA==.html?f=46177805', | |
104 | 'info_dict': { | |
105 | 'id': 'XMjIyNzAzMTQ4NA', | |
106 | 'ext': 'mp4', | |
107 | 'title': '卡马乔国足开大脚长传冲吊集锦', | |
d16c0121 YCH |
108 | 'duration': 289, |
109 | 'thumbnail': r're:^https?://.*', | |
110 | 'uploader': '阿卜杜拉之星', | |
111 | 'uploader_id': '2382249', | |
112 | 'uploader_url': 'http://i.youku.com/u/UOTUyODk5Ng==', | |
113 | 'tags': list, | |
c130f0a3 YCH |
114 | }, |
115 | }, { | |
116 | 'url': 'http://video.tudou.com/v/XMjE4ODI3OTg2MA==.html', | |
117 | 'only_matching': True, | |
ee697992 | 118 | }] |
67f51b3d | 119 | |
034caf70 YCH |
120 | @staticmethod |
121 | def get_ysuid(): | |
122 | return '%d%s' % (int(time.time()), ''.join([ | |
123 | random.choice(string.ascii_letters) for i in range(3)])) | |
124 | ||
08f7db20 P |
125 | def get_format_name(self, fm): |
126 | _dict = { | |
aed473cc YCH |
127 | '3gp': 'h6', |
128 | '3gphd': 'h5', | |
129 | 'flv': 'h4', | |
dbb7d7e2 | 130 | 'flvhd': 'h4', |
aed473cc | 131 | 'mp4': 'h3', |
8696a7fd | 132 | 'mp4hd': 'h3', |
dbb7d7e2 | 133 | 'mp4hd2': 'h4', |
8696a7fd | 134 | 'mp4hd3': 'h4', |
dbb7d7e2 YCH |
135 | 'hd2': 'h2', |
136 | 'hd3': 'h1', | |
08f7db20 | 137 | } |
59ed87cb | 138 | return _dict.get(fm) |
08f7db20 | 139 | |
9c286cfa | 140 | def _real_extract(self, url): |
9383e66f | 141 | video_id = self._match_id(url) |
9c286cfa | 142 | |
034caf70 | 143 | self._set_cookie('youku.com', '__ysuid', self.get_ysuid()) |
59ed87cb | 144 | self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com') |
034caf70 | 145 | |
59ed87cb YCH |
146 | _, urlh = self._download_webpage_handle( |
147 | 'https://log.mmstat.com/eg.js', video_id, 'Retrieving cna info') | |
148 | # The etag header is '"foobar"'; let's remove the double quotes | |
149 | cna = urlh.headers['etag'][1:-1] | |
51094b1b | 150 | |
59ed87cb YCH |
151 | # request basic data |
152 | basic_data_params = { | |
153 | 'vid': video_id, | |
f7ad7160 | 154 | 'ccode': '0532', |
59ed87cb YCH |
155 | 'client_ip': '192.168.1.1', |
156 | 'utid': cna, | |
157 | 'client_ts': time.time() / 1000, | |
158 | } | |
51094b1b | 159 | |
a06916d9 | 160 | video_password = self.get_param('videopassword') |
33eae08f | 161 | if video_password: |
59ed87cb | 162 | basic_data_params['password'] = video_password |
cb3d2eb9 | 163 | |
59ed87cb YCH |
164 | headers = { |
165 | 'Referer': url, | |
166 | } | |
167 | headers.update(self.geo_verification_headers()) | |
168 | data = self._download_json( | |
169 | 'https://ups.youku.com/ups/get.json', video_id, | |
170 | 'Downloading JSON metadata', | |
171 | query=basic_data_params, headers=headers)['data'] | |
8a32b82e | 172 | |
7e37c394 | 173 | error = data.get('error') |
14c17caf C |
174 | if error: |
175 | error_note = error.get('note') | |
176 | if error_note is not None and '因版权原因无法观看此视频' in error_note: | |
04e75966 YCH |
177 | raise ExtractorError( |
178 | 'Youku said: Sorry, this video is available in China only', expected=True) | |
cc799437 S |
179 | elif error_note and '该视频被设为私密' in error_note: |
180 | raise ExtractorError( | |
181 | 'Youku said: Sorry, this video is private', expected=True) | |
04e75966 | 182 | else: |
14c17caf | 183 | msg = 'Youku server reported error %i' % error.get('code') |
35e22b6b | 184 | if error_note is not None: |
14c17caf | 185 | msg += ': ' + error_note |
04e75966 | 186 | raise ExtractorError(msg) |
f9355dc9 | 187 | |
f133fd32 | 188 | # get video title |
d16c0121 YCH |
189 | video_data = data['video'] |
190 | title = video_data['title'] | |
f9355dc9 | 191 | |
59ed87cb YCH |
192 | formats = [{ |
193 | 'url': stream['m3u8_url'], | |
194 | 'format_id': self.get_format_name(stream.get('stream_type')), | |
195 | 'ext': 'mp4', | |
196 | 'protocol': 'm3u8_native', | |
197 | 'filesize': int(stream.get('size')), | |
198 | 'width': stream.get('width'), | |
199 | 'height': stream.get('height'), | |
200 | } for stream in data['stream'] if stream.get('channel_type') != 'tail'] | |
201 | self._sort_formats(formats) | |
f9355dc9 | 202 | |
f1e66cb2 | 203 | return { |
f1e66cb2 YCH |
204 | 'id': video_id, |
205 | 'title': title, | |
59ed87cb | 206 | 'formats': formats, |
d16c0121 YCH |
207 | 'duration': video_data.get('seconds'), |
208 | 'thumbnail': video_data.get('logo'), | |
209 | 'uploader': video_data.get('username'), | |
210 | 'uploader_id': str_or_none(video_data.get('userid')), | |
211 | 'uploader_url': data.get('uploader', {}).get('homepage'), | |
212 | 'tags': video_data.get('tags'), | |
f1e66cb2 | 213 | } |
411c590a YCH |
214 | |
215 | ||
216 | class YoukuShowIE(InfoExtractor): | |
d3d4ba7f | 217 | _VALID_URL = r'https?://list\.youku\.com/show/id_(?P<id>[0-9a-z]+)\.html' |
411c590a YCH |
218 | IE_NAME = 'youku:show' |
219 | ||
fa63cf6c | 220 | _TESTS = [{ |
d3d4ba7f | 221 | 'url': 'http://list.youku.com/show/id_zc7c670be07ff11e48b3f.html', |
411c590a YCH |
222 | 'info_dict': { |
223 | 'id': 'zc7c670be07ff11e48b3f', | |
fa63cf6c | 224 | 'title': '花千骨 DVD版', |
d3d4ba7f | 225 | 'description': 'md5:a1ae6f5618571bbeb5c9821f9c81b558', |
411c590a YCH |
226 | }, |
227 | 'playlist_count': 50, | |
fa63cf6c YCH |
228 | }, { |
229 | # Episode number not starting from 1 | |
230 | 'url': 'http://list.youku.com/show/id_zefbfbd70efbfbd780bef.html', | |
231 | 'info_dict': { | |
232 | 'id': 'zefbfbd70efbfbd780bef', | |
233 | 'title': '超级飞侠3', | |
234 | 'description': 'md5:275715156abebe5ccc2a1992e9d56b98', | |
235 | }, | |
236 | 'playlist_count': 24, | |
3fcf346a YCH |
237 | }, { |
238 | # Ongoing playlist. The initial page is the last one | |
239 | 'url': 'http://list.youku.com/show/id_za7c275ecd7b411e1a19e.html', | |
7512aa98 | 240 | 'only_matching': True, |
d3ca2832 J |
241 | }, { |
242 | # No data-id value. | |
243 | 'url': 'http://list.youku.com/show/id_zefbfbd61237fefbfbdef.html', | |
244 | 'only_matching': True, | |
da35331c J |
245 | }, { |
246 | # Wrong number of reload_id. | |
247 | 'url': 'http://list.youku.com/show/id_z20eb4acaf5c211e3b2ad.html', | |
248 | 'only_matching': True, | |
fa63cf6c | 249 | }] |
411c590a | 250 | |
3fcf346a | 251 | def _extract_entries(self, playlist_data_url, show_id, note, query): |
fa63cf6c YCH |
252 | query['callback'] = 'cb' |
253 | playlist_data = self._download_json( | |
3fcf346a | 254 | playlist_data_url, show_id, query=query, note=note, |
da35331c J |
255 | transform_source=lambda s: js_to_json(strip_jsonp(s))).get('html') |
256 | if playlist_data is None: | |
257 | return [None, None] | |
3089bc74 S |
258 | drama_list = (get_element_by_class('p-drama-grid', playlist_data) |
259 | or get_element_by_class('p-drama-half-row', playlist_data)) | |
fa63cf6c YCH |
260 | if drama_list is None: |
261 | raise ExtractorError('No episodes found') | |
262 | video_urls = re.findall(r'<a[^>]+href="([^"]+)"', drama_list) | |
263 | return playlist_data, [ | |
3fcf346a | 264 | self.url_result(self._proto_relative_url(video_url, 'http:'), YoukuIE.ie_key()) |
fa63cf6c | 265 | for video_url in video_urls] |
411c590a | 266 | |
411c590a YCH |
267 | def _real_extract(self, url): |
268 | show_id = self._match_id(url) | |
269 | webpage = self._download_webpage(url, show_id) | |
270 | ||
3fcf346a | 271 | entries = [] |
d3d4ba7f YCH |
272 | page_config = self._parse_json(self._search_regex( |
273 | r'var\s+PageConfig\s*=\s*({.+});', webpage, 'page config'), | |
274 | show_id, transform_source=js_to_json) | |
3fcf346a YCH |
275 | first_page, initial_entries = self._extract_entries( |
276 | 'http://list.youku.com/show/module', show_id, | |
277 | note='Downloading initial playlist data page', | |
278 | query={ | |
fa63cf6c YCH |
279 | 'id': page_config['showid'], |
280 | 'tab': 'showInfo', | |
3fcf346a YCH |
281 | }) |
282 | first_page_reload_id = self._html_search_regex( | |
283 | r'<div[^>]+id="(reload_\d+)', first_page, 'first page reload id') | |
fa63cf6c | 284 | # The first reload_id has the same items as first_page |
3fcf346a | 285 | reload_ids = re.findall('<li[^>]+data-id="([^"]+)">', first_page) |
d99a1000 | 286 | entries.extend(initial_entries) |
fa63cf6c | 287 | for idx, reload_id in enumerate(reload_ids): |
3fcf346a | 288 | if reload_id == first_page_reload_id: |
3fcf346a | 289 | continue |
fa63cf6c | 290 | _, new_entries = self._extract_entries( |
3fcf346a YCH |
291 | 'http://list.youku.com/show/episode', show_id, |
292 | note='Downloading playlist data page %d' % (idx + 1), | |
293 | query={ | |
d3d4ba7f | 294 | 'id': page_config['showid'], |
fa63cf6c | 295 | 'stage': reload_id, |
3fcf346a | 296 | }) |
da35331c J |
297 | if new_entries is not None: |
298 | entries.extend(new_entries) | |
d3d4ba7f YCH |
299 | desc = self._html_search_meta('description', webpage, fatal=False) |
300 | playlist_title = desc.split(',')[0] if desc else None | |
301 | detail_li = get_element_by_class('p-intro', webpage) | |
302 | playlist_description = get_element_by_class( | |
303 | 'intro-more', detail_li) if detail_li else None | |
304 | ||
305 | return self.playlist_result( | |
306 | entries, show_id, playlist_title, playlist_description) |