2 from __future__ import unicode_literals
10 from .common import InfoExtractor
21 class YoukuIE(InfoExtractor):
27 (?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
28 video\.tudou\.com/v/)|
30 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
35 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
37 'id': 'XMTc1ODE5Njcy',
38 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
41 'thumbnail': r're:^https?://.*',
43 'uploader_id': '36017967',
44 'uploader_url': 'http://i.youku.com/u/UMTQ0MDcxODY4',
48 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
49 'only_matching': True,
51 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
53 'id': 'XODgxNjg1Mzk2',
57 'thumbnail': r're:^https?://.*',
59 'uploader_id': '62583473',
60 'uploader_url': 'http://i.youku.com/u/UMjUwMzMzODky',
64 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
66 'id': 'XMTI1OTczNDM5Mg',
70 'thumbnail': r're:^https?://.*',
71 'uploader': '放剧场-花千骨',
72 'uploader_id': '772849359',
73 'uploader_url': 'http://i.youku.com/u/UMzA5MTM5NzQzNg==',
77 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
78 'note': 'Video protected with password',
80 'id': 'XNjA1NzA2Njgw',
82 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
84 'thumbnail': r're:^https?://.*',
85 'uploader': 'FoxJin1006',
86 'uploader_id': '322014285',
87 'uploader_url': 'http://i.youku.com/u/UMTI4ODA1NzE0MA==',
91 'videopassword': '100600',
94 # /play/get.json contains streams with "channel_type":"tail"
95 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
97 'id': 'XOTUxMzg4NDMy',
99 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
101 'thumbnail': r're:^https?://.*',
102 'uploader': '明月庄主moon',
103 'uploader_id': '38465621',
104 'uploader_url': 'http://i.youku.com/u/UMTUzODYyNDg0',
108 'url': 'http://video.tudou.com/v/XMjIyNzAzMTQ4NA==.html?f=46177805',
110 'id': 'XMjIyNzAzMTQ4NA',
112 'title': '卡马乔国足开大脚长传冲吊集锦',
114 'thumbnail': r're:^https?://.*',
115 'uploader': '阿卜杜拉之星',
116 'uploader_id': '2382249',
117 'uploader_url': 'http://i.youku.com/u/UOTUyODk5Ng==',
121 'url': 'http://video.tudou.com/v/XMjE4ODI3OTg2MA==.html',
122 'only_matching': True,
127 return '%d%s' % (int(time.time()), ''.join([
128 random.choice(string.ascii_letters) for i in range(3)]))
130 def get_format_name(self, fm):
145 def _real_extract(self, url):
146 video_id = self._match_id(url)
148 self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
149 self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
151 _, urlh = self._download_webpage_handle(
152 'https://log.mmstat.com/eg.js', video_id, 'Retrieving cna info')
153 # The etag header is '"foobar"'; let's remove the double quotes
154 cna = urlh.headers['etag'][1:-1]
157 basic_data_params = {
159 'ccode': '0402' if 'tudou.com' in url else '0401',
160 'client_ip': '192.168.1.1',
162 'client_ts': time.time() / 1000,
165 video_password = self._downloader.params.get('videopassword')
167 basic_data_params['password'] = video_password
172 headers.update(self.geo_verification_headers())
173 data = self._download_json(
174 'https://ups.youku.com/ups/get.json', video_id,
175 'Downloading JSON metadata',
176 query=basic_data_params, headers=headers)['data']
178 error = data.get('error')
180 error_note = error.get('note')
181 if error_note is not None and '因版权原因无法观看此视频' in error_note:
182 raise ExtractorError(
183 'Youku said: Sorry, this video is available in China only', expected=True)
184 elif error_note and '该视频被设为私密' in error_note:
185 raise ExtractorError(
186 'Youku said: Sorry, this video is private', expected=True)
188 msg = 'Youku server reported error %i' % error.get('code')
189 if error_note is not None:
190 msg += ': ' + error_note
191 raise ExtractorError(msg)
194 video_data = data['video']
195 title = video_data['title']
198 'url': stream['m3u8_url'],
199 'format_id': self.get_format_name(stream.get('stream_type')),
201 'protocol': 'm3u8_native',
202 'filesize': int(stream.get('size')),
203 'width': stream.get('width'),
204 'height': stream.get('height'),
205 } for stream in data['stream'] if stream.get('channel_type') != 'tail']
206 self._sort_formats(formats)
212 'duration': video_data.get('seconds'),
213 'thumbnail': video_data.get('logo'),
214 'uploader': video_data.get('username'),
215 'uploader_id': str_or_none(video_data.get('userid')),
216 'uploader_url': data.get('uploader', {}).get('homepage'),
217 'tags': video_data.get('tags'),
221 class YoukuShowIE(InfoExtractor):
222 _VALID_URL = r'https?://list\.youku\.com/show/id_(?P<id>[0-9a-z]+)\.html'
223 IE_NAME = 'youku:show'
226 'url': 'http://list.youku.com/show/id_zc7c670be07ff11e48b3f.html',
228 'id': 'zc7c670be07ff11e48b3f',
230 'description': 'md5:a1ae6f5618571bbeb5c9821f9c81b558',
232 'playlist_count': 50,
237 def _real_extract(self, url):
238 show_id = self._match_id(url)
239 webpage = self._download_webpage(url, show_id)
242 page_config = self._parse_json(self._search_regex(
243 r'var\s+PageConfig\s*=\s*({.+});', webpage, 'page config'),
244 show_id, transform_source=js_to_json)
245 for idx in itertools.count(0):
247 playlist_data_url = 'http://list.youku.com/show/module'
248 query = {'id': page_config['showid'], 'tab': 'point'}
250 playlist_data_url = 'http://list.youku.com/show/point'
252 'id': page_config['showid'],
253 'stage': 'reload_%d' % (self._PAGE_SIZE * idx + 1),
255 query['callback'] = 'cb'
256 playlist_data = self._download_json(
257 playlist_data_url, show_id, query=query,
258 note='Downloading playlist data page %d' % (idx + 1),
259 transform_source=lambda s: js_to_json(strip_jsonp(s)))['html']
260 video_urls = re.findall(
261 r'<div[^>]+class="p-thumb"[^<]+<a[^>]+href="([^"]+)"',
264 self.url_result(urljoin(url, video_url), YoukuIE.ie_key())
265 for video_url in video_urls]
266 entries.extend(new_entries)
267 if len(new_entries) < self._PAGE_SIZE:
270 desc = self._html_search_meta('description', webpage, fatal=False)
271 playlist_title = desc.split(',')[0] if desc else None
272 detail_li = get_element_by_class('p-intro', webpage)
273 playlist_description = get_element_by_class(
274 'intro-more', detail_li) if detail_li else None
276 return self.playlist_result(
277 entries, show_id, playlist_title, playlist_description)