]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/youku.py
[ie/Canal1,CaracolTvPlay] Add extractors (#7151)
[yt-dlp.git] / yt_dlp / extractor / youku.py
1 import random
2 import re
3 import string
4 import time
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 clean_html,
10 get_element_by_class,
11 js_to_json,
12 str_or_none,
13 strip_jsonp,
14 )
15
16
17 class YoukuIE(InfoExtractor):
18 IE_NAME = 'youku'
19 IE_DESC = '优酷'
20 _VALID_URL = r'''(?x)
21 (?:
22 https?://(
23 (?:v|player)\.youku\.com/(?:v_show/id_|player\.php/sid/)|
24 video\.tudou\.com/v/)|
25 youku:)
26 (?P<id>[A-Za-z0-9]+)(?:\.html|/v\.swf|)
27 '''
28
29 _TESTS = [{
30 'url': 'http://player.youku.com/player.php/sid/XNDgyMDQ2NTQw/v.swf',
31 'only_matching': True,
32 }, {
33 'url': 'http://v.youku.com/v_show/id_XNjA1NzA2Njgw.html',
34 'note': 'Video protected with password',
35 'info_dict': {
36 'id': 'XNjA1NzA2Njgw',
37 'ext': 'mp4',
38 'title': '邢義田复旦讲座之想象中的胡人—从“左衽孔子”说起',
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,
45 'skip': '404',
46 },
47 'params': {
48 'videopassword': '100600',
49 },
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',
55 'ext': 'mp4',
56 'title': '我的世界☆明月庄主☆车震猎杀☆杀人艺术Minecraft',
57 'duration': 702.08,
58 'thumbnail': r're:^https?://.*',
59 'uploader': '明月庄主moon',
60 'uploader_id': '38465621',
61 'uploader_url': 'https://www.youku.com/profile/index/?uid=UMTUzODYyNDg0',
62 'tags': list,
63 },
64 }, {
65 'url': 'https://v.youku.com/v_show/id_XNTA2NTA0MjA1Mg==.html',
66 'info_dict': {
67 'id': 'XNTA2NTA0MjA1Mg',
68 'ext': 'mp4',
69 'title': 'Minecraft我的世界:建造超大巨型航空飞机,菜鸟vs高手vs黑客',
70 'duration': 542.13,
71 'thumbnail': r're:^https?://.*',
72 'uploader': '波哥游戏解说',
73 'uploader_id': '156688084',
74 'uploader_url': 'https://www.youku.com/profile/index/?uid=UNjI2NzUyMzM2',
75 'tags': list,
76 },
77 }, {
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 },
90 }]
91
92 @staticmethod
93 def get_ysuid():
94 return '%d%s' % (int(time.time()), ''.join(
95 random.choices(string.ascii_letters, k=3)))
96
97 def get_format_name(self, fm):
98 _dict = {
99 '3gp': 'h6',
100 '3gphd': 'h5',
101 'flv': 'h4',
102 'flvhd': 'h4',
103 'mp4': 'h3',
104 'mp4hd': 'h3',
105 'mp4hd2': 'h4',
106 'mp4hd3': 'h4',
107 'hd2': 'h2',
108 'hd3': 'h1',
109 }
110 return _dict.get(fm)
111
112 def _real_extract(self, url):
113 video_id = self._match_id(url)
114
115 self._set_cookie('youku.com', '__ysuid', self.get_ysuid())
116 self._set_cookie('youku.com', 'xreferrer', 'http://www.youku.com')
117
118 _, urlh = self._download_webpage_handle(
119 'https://log.mmstat.com/eg.js', video_id, 'Retrieving cna info')
120 # The etag header is '"foobar"'; let's remove the double quotes
121 cna = urlh.headers['etag'][1:-1]
122
123 # request basic data
124 basic_data_params = {
125 'vid': video_id,
126 'ccode': '0524',
127 'client_ip': '192.168.1.1',
128 'utid': cna,
129 'client_ts': time.time() / 1000,
130 }
131
132 video_password = self.get_param('videopassword')
133 if video_password:
134 basic_data_params['password'] = video_password
135
136 headers = {
137 'Referer': url,
138 }
139 headers.update(self.geo_verification_headers())
140 data = self._download_json(
141 'https://ups.youku.com/ups/get.json', video_id,
142 'Downloading JSON metadata',
143 query=basic_data_params, headers=headers)['data']
144
145 error = data.get('error')
146 if error:
147 error_note = error.get('note')
148 if error_note is not None and '因版权原因无法观看此视频' in error_note:
149 raise ExtractorError(
150 'Youku said: Sorry, this video is available in China only', expected=True)
151 elif error_note and '该视频被设为私密' in error_note:
152 raise ExtractorError(
153 'Youku said: Sorry, this video is private', expected=True)
154 else:
155 msg = 'Youku server reported error %i' % error.get('code')
156 if error_note is not None:
157 msg += ': ' + clean_html(error_note)
158 raise ExtractorError(msg)
159
160 # get video title
161 video_data = data['video']
162 title = video_data['title']
163
164 formats = [{
165 'url': stream['m3u8_url'],
166 'format_id': self.get_format_name(stream.get('stream_type')),
167 'ext': 'mp4',
168 'protocol': 'm3u8_native',
169 'filesize': int(stream.get('size')),
170 'width': stream.get('width'),
171 'height': stream.get('height'),
172 } for stream in data['stream'] if stream.get('channel_type') != 'tail']
173
174 return {
175 'id': video_id,
176 'title': title,
177 'formats': formats,
178 'duration': video_data.get('seconds'),
179 'thumbnail': video_data.get('logo'),
180 'uploader': video_data.get('username'),
181 'uploader_id': str_or_none(video_data.get('userid')),
182 'uploader_url': data.get('uploader', {}).get('homepage'),
183 'tags': video_data.get('tags'),
184 }
185
186
187 class YoukuShowIE(InfoExtractor):
188 _VALID_URL = r'https?://list\.youku\.com/show/id_(?P<id>[0-9a-z]+)\.html'
189 IE_NAME = 'youku:show'
190
191 _TESTS = [{
192 'url': 'http://list.youku.com/show/id_zc7c670be07ff11e48b3f.html',
193 'info_dict': {
194 'id': 'zc7c670be07ff11e48b3f',
195 'title': '花千骨 DVD版',
196 'description': 'md5:a1ae6f5618571bbeb5c9821f9c81b558',
197 },
198 'playlist_count': 50,
199 }, {
200 # Episode number not starting from 1
201 'url': 'http://list.youku.com/show/id_zefbfbd70efbfbd780bef.html',
202 'info_dict': {
203 'id': 'zefbfbd70efbfbd780bef',
204 'title': '超级飞侠3',
205 'description': 'md5:275715156abebe5ccc2a1992e9d56b98',
206 },
207 'playlist_count': 24,
208 }, {
209 # Ongoing playlist. The initial page is the last one
210 'url': 'http://list.youku.com/show/id_za7c275ecd7b411e1a19e.html',
211 'only_matching': True,
212 }, {
213 # No data-id value.
214 'url': 'http://list.youku.com/show/id_zefbfbd61237fefbfbdef.html',
215 'only_matching': True,
216 }, {
217 # Wrong number of reload_id.
218 'url': 'http://list.youku.com/show/id_z20eb4acaf5c211e3b2ad.html',
219 'only_matching': True,
220 }]
221
222 def _extract_entries(self, playlist_data_url, show_id, note, query):
223 query['callback'] = 'cb'
224 playlist_data = self._download_json(
225 playlist_data_url, show_id, query=query, note=note,
226 transform_source=lambda s: js_to_json(strip_jsonp(s))).get('html')
227 if playlist_data is None:
228 return [None, None]
229 drama_list = (get_element_by_class('p-drama-grid', playlist_data)
230 or get_element_by_class('p-drama-half-row', playlist_data))
231 if drama_list is None:
232 raise ExtractorError('No episodes found')
233 video_urls = re.findall(r'<a[^>]+href="([^"]+)"', drama_list)
234 return playlist_data, [
235 self.url_result(self._proto_relative_url(video_url, 'http:'), YoukuIE.ie_key())
236 for video_url in video_urls]
237
238 def _real_extract(self, url):
239 show_id = self._match_id(url)
240 webpage = self._download_webpage(url, show_id)
241
242 entries = []
243 page_config = self._parse_json(self._search_regex(
244 r'var\s+PageConfig\s*=\s*({.+});', webpage, 'page config'),
245 show_id, transform_source=js_to_json)
246 first_page, initial_entries = self._extract_entries(
247 'http://list.youku.com/show/module', show_id,
248 note='Downloading initial playlist data page',
249 query={
250 'id': page_config['showid'],
251 'tab': 'showInfo',
252 })
253 first_page_reload_id = self._html_search_regex(
254 r'<div[^>]+id="(reload_\d+)', first_page, 'first page reload id')
255 # The first reload_id has the same items as first_page
256 reload_ids = re.findall('<li[^>]+data-id="([^"]+)">', first_page)
257 entries.extend(initial_entries)
258 for idx, reload_id in enumerate(reload_ids):
259 if reload_id == first_page_reload_id:
260 continue
261 _, new_entries = self._extract_entries(
262 'http://list.youku.com/show/episode', show_id,
263 note='Downloading playlist data page %d' % (idx + 1),
264 query={
265 'id': page_config['showid'],
266 'stage': reload_id,
267 })
268 if new_entries is not None:
269 entries.extend(new_entries)
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
275
276 return self.playlist_result(
277 entries, show_id, playlist_title, playlist_description)