2 from __future__ import unicode_literals
10 from .common import InfoExtractor
13 get_element_by_attribute,
17 class YoukuIE(InfoExtractor):
22 http://(?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
24 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
29 'url': 'http://v.youku.com/v_show/id_XMTc1ODE5Njcy.html',
31 'id': 'XMTc1ODE5Njcy',
32 'title': '★Smile﹗♡ Git Fresh -Booty Music舞蹈.',
36 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
37 'only_matching': True,
39 'url': 'http://v.youku.com/v_show/id_XODgxNjg1Mzk2_ev_1.html',
41 'id': 'XODgxNjg1Mzk2',
46 'url': 'http://v.youku.com/v_show/id_XMTI1OTczNDM5Mg==.html',
48 'id': 'XMTI1OTczNDM5Mg',
53 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
54 'note': 'Video protected with password',
56 'id': 'XNjA1NzA2Njgw',
58 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
61 'videopassword': '100600',
64 # /play/get.json contains streams with "channel_type":"tail"
65 'url': 'http://v.youku.com/v_show/id_XOTUxMzg4NDMy.html',
67 'id': 'XOTUxMzg4NDMy',
69 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
75 return '%d%s' % (int(time.time()), ''.join([
76 random.choice(string.ascii_letters) for i in range(3)]))
78 def get_format_name(self, fm):
93 def _real_extract(self, url):
94 video_id = self._match_id(url)
96 self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
97 self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
99 _, urlh = self._download_webpage_handle(
100 'https://log.mmstat.com/eg.js', video_id, 'Retrieving cna info')
101 # The etag header is '"foobar"'; let's remove the double quotes
102 cna = urlh.headers['etag'][1:-1]
105 basic_data_params = {
108 'client_ip': '192.168.1.1',
110 'client_ts': time.time() / 1000,
113 video_password = self._downloader.params.get('videopassword')
115 basic_data_params['password'] = video_password
120 headers.update(self.geo_verification_headers())
121 data = self._download_json(
122 'https://ups.youku.com/ups/get.json', video_id,
123 'Downloading JSON metadata',
124 query=basic_data_params, headers=headers)['data']
126 error = data.get('error')
128 error_note = error.get('note')
129 if error_note is not None and '因版权原因无法观看此视频' in error_note:
130 raise ExtractorError(
131 'Youku said: Sorry, this video is available in China only', expected=True)
132 elif error_note and '该视频被设为私密' in error_note:
133 raise ExtractorError(
134 'Youku said: Sorry, this video is private', expected=True)
136 msg = 'Youku server reported error %i' % error.get('code')
137 if error_note is not None:
138 msg += ': ' + error_note
139 raise ExtractorError(msg)
142 title = data['video']['title']
145 'url': stream['m3u8_url'],
146 'format_id': self.get_format_name(stream.get('stream_type')),
148 'protocol': 'm3u8_native',
149 'filesize': int(stream.get('size')),
150 'width': stream.get('width'),
151 'height': stream.get('height'),
152 } for stream in data['stream'] if stream.get('channel_type') != 'tail']
153 self._sort_formats(formats)
162 class YoukuShowIE(InfoExtractor):
163 _VALID_URL = r'https?://(?:www\.)?youku\.com/show_page/id_(?P<id>[0-9a-z]+)\.html'
164 IE_NAME = 'youku:show'
167 'url': 'http://www.youku.com/show_page/id_zc7c670be07ff11e48b3f.html',
169 'id': 'zc7c670be07ff11e48b3f',
171 'description': 'md5:578d4f2145ae3f9128d9d4d863312910',
173 'playlist_count': 50,
178 def _find_videos_in_page(self, webpage):
180 r'<li><a[^>]+href="(?P<url>https?://v\.youku\.com/[^"]+)"[^>]+title="(?P<title>[^"]+)"', webpage)
182 self.url_result(video_url, YoukuIE.ie_key(), title)
183 for video_url, title in videos]
185 def _real_extract(self, url):
186 show_id = self._match_id(url)
187 webpage = self._download_webpage(url, show_id)
189 entries = self._find_videos_in_page(webpage)
191 playlist_title = self._html_search_regex(
192 r'<span[^>]+class="name">([^<]+)</span>', webpage, 'playlist title', fatal=False)
193 detail_div = get_element_by_attribute('class', 'detail', webpage) or ''
194 playlist_description = self._html_search_regex(
195 r'<span[^>]+style="display:none"[^>]*>([^<]+)</span>',
196 detail_div, 'playlist description', fatal=False)
198 for idx in itertools.count(1):
199 episodes_page = self._download_webpage(
200 'http://www.youku.com/show_episode/id_%s.html' % show_id,
201 show_id, query={'divid': 'reload_%d' % (idx * self._PAGE_SIZE + 1)},
202 note='Downloading episodes page %d' % idx)
203 new_entries = self._find_videos_in_page(episodes_page)
204 entries.extend(new_entries)
205 if len(new_entries) < self._PAGE_SIZE:
208 return self.playlist_result(entries, show_id, playlist_title, playlist_description)