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