]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tencent.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / tencent.py
CommitLineData
b2a4db42
E
1import functools
2import random
3import re
4import string
5import time
6
7from .common import InfoExtractor
8from ..aes import aes_cbc_encrypt_bytes
9from ..utils import (
10 ExtractorError,
11 determine_ext,
12 int_or_none,
13 js_to_json,
14 traverse_obj,
15 urljoin,
16)
17
18
19class TencentBaseIE(InfoExtractor):
20 """Subclasses must set _API_URL, _APP_VERSION, _PLATFORM, _HOST, _REFERER"""
21
22 def _get_ckey(self, video_id, url, guid):
23 ua = self.get_param('http_headers')['User-Agent']
24
25 payload = (f'{video_id}|{int(time.time())}|mg3c3b04ba|{self._APP_VERSION}|{guid}|'
26 f'{self._PLATFORM}|{url[:48]}|{ua.lower()[:48]}||Mozilla|Netscape|Windows x86_64|00|')
27
28 return aes_cbc_encrypt_bytes(
29 bytes(f'|{sum(map(ord, payload))}|{payload}', 'utf-8'),
30 b'Ok\xda\xa3\x9e/\x8c\xb0\x7f^r-\x9e\xde\xf3\x14',
31 b'\x01PJ\xf3V\xe6\x19\xcf.B\xbb\xa6\x8c?p\xf9',
32 padding_mode='whitespace').hex().upper()
33
34 def _get_video_api_response(self, video_url, video_id, series_id, subtitle_format, video_format, video_quality):
35 guid = ''.join([random.choice(string.digits + string.ascii_lowercase) for _ in range(16)])
36 ckey = self._get_ckey(video_id, video_url, guid)
37 query = {
38 'vid': video_id,
39 'cid': series_id,
40 'cKey': ckey,
41 'encryptVer': '8.1',
42 'spcaptiontype': '1' if subtitle_format == 'vtt' else '0',
43 'sphls': '2' if video_format == 'hls' else '0',
44 'dtype': '3' if video_format == 'hls' else '0',
45 'defn': video_quality,
46 'spsrt': '2', # Enable subtitles
47 'sphttps': '1', # Enable HTTPS
48 'otype': 'json',
49 'spwm': '1',
50 # For SHD
51 'host': self._HOST,
52 'referer': self._REFERER,
53 'ehost': video_url,
54 'appVer': self._APP_VERSION,
55 'platform': self._PLATFORM,
56 # For VQQ
57 'guid': guid,
58 'flowid': ''.join(random.choice(string.digits + string.ascii_lowercase) for _ in range(32)),
59 }
60
61 return self._search_json(r'QZOutputJson=', self._download_webpage(
62 self._API_URL, video_id, query=query), 'api_response', video_id)
63
64 def _extract_video_formats_and_subtitles(self, api_response, video_id):
65 video_response = api_response['vl']['vi'][0]
66 video_width, video_height = video_response.get('vw'), video_response.get('vh')
67
68 formats, subtitles = [], {}
69 for video_format in video_response['ul']['ui']:
0a4b2f41 70 if video_format.get('hls') or determine_ext(video_format['url']) == 'm3u8':
b2a4db42 71 fmts, subs = self._extract_m3u8_formats_and_subtitles(
0a4b2f41
E
72 video_format['url'] + traverse_obj(video_format, ('hls', 'pt'), default=''),
73 video_id, 'mp4', fatal=False)
b2a4db42
E
74 for f in fmts:
75 f.update({'width': video_width, 'height': video_height})
76
77 formats.extend(fmts)
78 self._merge_subtitles(subs, target=subtitles)
79 else:
80 formats.append({
81 'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
82 'width': video_width,
83 'height': video_height,
84 'ext': 'mp4',
85 })
86
87 return formats, subtitles
88
89 def _extract_video_native_subtitles(self, api_response, subtitles_format):
90 subtitles = {}
91 for subtitle in traverse_obj(api_response, ('sfl', 'fi')) or ():
92 subtitles.setdefault(subtitle['lang'].lower(), []).append({
93 'url': subtitle['url'],
94 'ext': subtitles_format,
95 'protocol': 'm3u8_native' if determine_ext(subtitle['url']) == 'm3u8' else 'http',
96 })
97
98 return subtitles
99
100 def _extract_all_video_formats_and_subtitles(self, url, video_id, series_id):
101 formats, subtitles = [], {}
102 for video_format, subtitle_format, video_quality in (
103 # '': 480p, 'shd': 720p, 'fhd': 1080p
104 ('mp4', 'srt', ''), ('hls', 'vtt', 'shd'), ('hls', 'vtt', 'fhd')):
105 api_response = self._get_video_api_response(
106 url, video_id, series_id, subtitle_format, video_format, video_quality)
107
108 if api_response.get('em') != 0 and api_response.get('exem') != 0:
109 if '您所在区域暂无此内容版权' in api_response.get('msg'):
110 self.raise_geo_restricted()
111 raise ExtractorError(f'Tencent said: {api_response.get("msg")}')
112
113 fmts, subs = self._extract_video_formats_and_subtitles(api_response, video_id)
114 native_subtitles = self._extract_video_native_subtitles(api_response, subtitle_format)
115
116 formats.extend(fmts)
117 self._merge_subtitles(subs, native_subtitles, target=subtitles)
118
b2a4db42
E
119 return formats, subtitles
120
121 def _get_clean_title(self, title):
122 return re.sub(
123 r'\s*[_\-]\s*(?:Watch online|腾讯视频|(?:高清)?1080P在线观看平台).*?$',
124 '', title or '').strip() or None
125
126
127class VQQBaseIE(TencentBaseIE):
128 _VALID_URL_BASE = r'https?://v\.qq\.com'
129
130 _API_URL = 'https://h5vv6.video.qq.com/getvinfo'
131 _APP_VERSION = '3.5.57'
132 _PLATFORM = '10901'
133 _HOST = 'v.qq.com'
134 _REFERER = 'v.qq.com'
135
136 def _get_webpage_metadata(self, webpage, video_id):
137 return self._parse_json(
138 self._search_regex(
139 r'(?s)<script[^>]*>[^<]*window\.__pinia\s*=\s*([^<]+)</script>',
140 webpage, 'pinia data', fatal=False),
141 video_id, transform_source=js_to_json, fatal=False)
142
143
144class VQQVideoIE(VQQBaseIE):
145 IE_NAME = 'vqq:video'
146 _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/(?:page|cover/(?P<series_id>\w+))/(?P<id>\w+)'
147
148 _TESTS = [{
149 'url': 'https://v.qq.com/x/page/q326831cny0.html',
150 'md5': '826ef93682df09e3deac4a6e6e8cdb6e',
151 'info_dict': {
152 'id': 'q326831cny0',
153 'ext': 'mp4',
154 'title': '我是选手:雷霆裂阵,终极时刻',
155 'description': 'md5:e7ed70be89244017dac2a835a10aeb1e',
156 'thumbnail': r're:^https?://[^?#]+q326831cny0',
157 },
158 }, {
159 'url': 'https://v.qq.com/x/page/o3013za7cse.html',
160 'md5': 'b91cbbeada22ef8cc4b06df53e36fa21',
161 'info_dict': {
162 'id': 'o3013za7cse',
163 'ext': 'mp4',
164 'title': '欧阳娜娜VLOG',
165 'description': 'md5:29fe847497a98e04a8c3826e499edd2e',
166 'thumbnail': r're:^https?://[^?#]+o3013za7cse',
167 },
168 }, {
169 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27/a00269ix3l8.html',
170 'md5': '71459c5375c617c265a22f083facce67',
171 'info_dict': {
172 'id': 'a00269ix3l8',
173 'ext': 'mp4',
174 'title': '鸡毛飞上天 第01集',
175 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
176 'thumbnail': r're:^https?://[^?#]+7ce5noezvafma27',
177 'series': '鸡毛飞上天',
178 },
179 }, {
180 'url': 'https://v.qq.com/x/cover/mzc00200p29k31e/s0043cwsgj0.html',
181 'md5': '96b9fd4a189fdd4078c111f21d7ac1bc',
182 'info_dict': {
183 'id': 's0043cwsgj0',
184 'ext': 'mp4',
185 'title': '第1集:如何快乐吃糖?',
186 'description': 'md5:1d8c3a0b8729ae3827fa5b2d3ebd5213',
187 'thumbnail': r're:^https?://[^?#]+s0043cwsgj0',
188 'series': '青年理工工作者生活研究所',
189 },
0a4b2f41
E
190 }, {
191 # Geo-restricted to China
192 'url': 'https://v.qq.com/x/cover/mcv8hkc8zk8lnov/x0036x5qqsr.html',
193 'only_matching': True,
b2a4db42
E
194 }]
195
196 def _real_extract(self, url):
197 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
198 webpage = self._download_webpage(url, video_id)
199 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
200
201 formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
202 return {
203 'id': video_id,
204 'title': self._get_clean_title(self._og_search_title(webpage)
205 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'title'))),
206 'description': (self._og_search_description(webpage)
207 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'desc'))),
208 'formats': formats,
209 'subtitles': subtitles,
210 'thumbnail': (self._og_search_thumbnail(webpage)
211 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'pic160x90'))),
212 'series': traverse_obj(webpage_metadata, ('global', 'coverInfo', 'title')),
213 }
214
215
216class VQQSeriesIE(VQQBaseIE):
217 IE_NAME = 'vqq:series'
218 _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/cover/(?P<id>\w+)\.html/?(?:[?#]|$)'
219
220 _TESTS = [{
221 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27.html',
222 'info_dict': {
223 'id': '7ce5noezvafma27',
224 'title': '鸡毛飞上天',
225 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
226 },
227 'playlist_count': 55,
228 }, {
229 'url': 'https://v.qq.com/x/cover/oshd7r0vy9sfq8e.html',
230 'info_dict': {
231 'id': 'oshd7r0vy9sfq8e',
232 'title': '恋爱细胞2',
233 'description': 'md5:9d8a2245679f71ca828534b0f95d2a03',
234 },
235 'playlist_count': 12,
236 }]
237
238 def _real_extract(self, url):
239 series_id = self._match_id(url)
240 webpage = self._download_webpage(url, series_id)
241 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
242
243 episode_paths = [f'/x/cover/{series_id}/{video_id}.html' for video_id in re.findall(
244 r'<div[^>]+data-vid="(?P<video_id>[^"]+)"[^>]+class="[^"]+episode-item-rect--number',
245 webpage)]
246
247 return self.playlist_from_matches(
248 episode_paths, series_id, ie=VQQVideoIE, getter=functools.partial(urljoin, url),
249 title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
250 or self._og_search_title(webpage)),
251 description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
252 or self._og_search_description(webpage)))
253
254
255class WeTvBaseIE(TencentBaseIE):
256 _VALID_URL_BASE = r'https?://(?:www\.)?wetv\.vip/(?:[^?#]+/)?play'
257
258 _API_URL = 'https://play.wetv.vip/getvinfo'
259 _APP_VERSION = '3.5.57'
260 _PLATFORM = '4830201'
261 _HOST = 'wetv.vip'
262 _REFERER = 'wetv.vip'
263
264 def _get_webpage_metadata(self, webpage, video_id):
265 return self._parse_json(
266 traverse_obj(self._search_nextjs_data(webpage, video_id), ('props', 'pageProps', 'data')),
267 video_id, fatal=False)
268
48f535f5
E
269 def _extract_episode(self, url):
270 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
271 webpage = self._download_webpage(url, video_id)
272 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
273
274 formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
275 return {
276 'id': video_id,
277 'title': self._get_clean_title(self._og_search_title(webpage)
278 or traverse_obj(webpage_metadata, ('coverInfo', 'title'))),
279 'description': (traverse_obj(webpage_metadata, ('coverInfo', 'description'))
280 or self._og_search_description(webpage)),
281 'formats': formats,
282 'subtitles': subtitles,
283 'thumbnail': self._og_search_thumbnail(webpage),
284 'duration': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'duration'))),
285 'series': traverse_obj(webpage_metadata, ('coverInfo', 'title')),
286 'episode_number': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'episode'))),
287 }
288
289 def _extract_series(self, url, ie):
290 series_id = self._match_id(url)
291 webpage = self._download_webpage(url, series_id)
292 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
293
294 episode_paths = ([f'/play/{series_id}/{episode["vid"]}' for episode in webpage_metadata.get('videoList')]
295 or re.findall(r'<a[^>]+class="play-video__link"[^>]+href="(?P<path>[^"]+)', webpage))
296
297 return self.playlist_from_matches(
298 episode_paths, series_id, ie=ie, getter=functools.partial(urljoin, url),
299 title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
300 or self._og_search_title(webpage)),
301 description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
302 or self._og_search_description(webpage)))
303
b2a4db42
E
304
305class WeTvEpisodeIE(WeTvBaseIE):
306 IE_NAME = 'wetv:episode'
307 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
308
309 _TESTS = [{
310 'url': 'https://wetv.vip/en/play/air11ooo2rdsdi3-Cute-Programmer/v0040pr89t9-EP1-Cute-Programmer',
311 'md5': '0c70fdfaa5011ab022eebc598e64bbbe',
312 'info_dict': {
313 'id': 'v0040pr89t9',
314 'ext': 'mp4',
315 'title': 'EP1: Cute Programmer',
316 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
317 'thumbnail': r're:^https?://[^?#]+air11ooo2rdsdi3',
318 'series': 'Cute Programmer',
319 'episode': 'Episode 1',
320 'episode_number': 1,
321 'duration': 2835,
322 },
323 }, {
324 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu/p0039b9nvik',
325 'md5': '3b3c15ca4b9a158d8d28d5aa9d7c0a49',
326 'info_dict': {
327 'id': 'p0039b9nvik',
328 'ext': 'mp4',
329 'title': 'EP1: You Are My Glory',
330 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
331 'thumbnail': r're:^https?://[^?#]+u37kgfnfzs73kiu',
332 'series': 'You Are My Glory',
333 'episode': 'Episode 1',
334 'episode_number': 1,
335 'duration': 2454,
336 },
337 }, {
338 'url': 'https://wetv.vip/en/play/lcxgwod5hapghvw-WeTV-PICK-A-BOO/i0042y00lxp-Zhao-Lusi-Describes-The-First-Experiences-She-Had-In-Who-Rules-The-World-%7C-WeTV-PICK-A-BOO',
339 'md5': '71133f5c2d5d6cad3427e1b010488280',
340 'info_dict': {
341 'id': 'i0042y00lxp',
342 'ext': 'mp4',
343 'title': 'md5:f7a0857dbe5fbbe2e7ad630b92b54e6a',
344 'description': 'md5:76260cb9cdc0ef76826d7ca9d92fadfa',
345 'thumbnail': r're:^https?://[^?#]+lcxgwod5hapghvw',
346 'series': 'WeTV PICK-A-BOO',
347 'episode': 'Episode 0',
348 'episode_number': 0,
349 'duration': 442,
350 },
351 }]
352
353 def _real_extract(self, url):
48f535f5 354 return self._extract_episode(url)
b2a4db42
E
355
356
357class WeTvSeriesIE(WeTvBaseIE):
358 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
359
360 _TESTS = [{
361 'url': 'https://wetv.vip/play/air11ooo2rdsdi3-Cute-Programmer',
362 'info_dict': {
363 'id': 'air11ooo2rdsdi3',
364 'title': 'Cute Programmer',
365 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
366 },
367 'playlist_count': 30,
368 }, {
369 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu-You-Are-My-Glory',
370 'info_dict': {
371 'id': 'u37kgfnfzs73kiu',
372 'title': 'You Are My Glory',
373 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
374 },
375 'playlist_count': 32,
376 }]
377
378 def _real_extract(self, url):
48f535f5 379 return self._extract_series(url, WeTvEpisodeIE)
b2a4db42 380
b2a4db42 381
48f535f5
E
382class IflixBaseIE(WeTvBaseIE):
383 _VALID_URL_BASE = r'https?://(?:www\.)?iflix\.com/(?:[^?#]+/)?play'
384
385 _API_URL = 'https://vplay.iflix.com/getvinfo'
386 _APP_VERSION = '3.5.57'
387 _PLATFORM = '330201'
388 _HOST = 'www.iflix.com'
389 _REFERER = 'www.iflix.com'
390
391
392class IflixEpisodeIE(IflixBaseIE):
393 IE_NAME = 'iflix:episode'
394 _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
395
396 _TESTS = [{
397 'url': 'https://www.iflix.com/en/play/daijrxu03yypu0s/a0040kvgaza',
398 'md5': '9740f9338c3a2105290d16b68fb3262f',
399 'info_dict': {
400 'id': 'a0040kvgaza',
401 'ext': 'mp4',
402 'title': 'EP1: Put Your Head On My Shoulder 2021',
403 'description': 'md5:c095a742d3b7da6dfedd0c8170727a42',
404 'thumbnail': r're:^https?://[^?#]+daijrxu03yypu0s',
405 'series': 'Put Your Head On My Shoulder 2021',
406 'episode': 'Episode 1',
407 'episode_number': 1,
408 'duration': 2639,
409 },
410 }, {
411 'url': 'https://www.iflix.com/en/play/fvvrcc3ra9lbtt1-Take-My-Brother-Away/i0029sd3gm1-EP1%EF%BC%9ATake-My-Brother-Away',
412 'md5': '375c9b8478fdedca062274b2c2f53681',
413 'info_dict': {
414 'id': 'i0029sd3gm1',
415 'ext': 'mp4',
416 'title': 'EP1:Take My Brother Away',
417 'description': 'md5:f0f7be1606af51cd94d5627de96b0c76',
418 'thumbnail': r're:^https?://[^?#]+fvvrcc3ra9lbtt1',
419 'series': 'Take My Brother Away',
420 'episode': 'Episode 1',
421 'episode_number': 1,
422 'duration': 228,
423 },
424 }]
425
426 def _real_extract(self, url):
427 return self._extract_episode(url)
428
429
430class IflixSeriesIE(IflixBaseIE):
431 _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
432
433 _TESTS = [{
434 'url': 'https://www.iflix.com/en/play/g21a6qk4u1s9x22-You-Are-My-Hero',
435 'info_dict': {
436 'id': 'g21a6qk4u1s9x22',
437 'title': 'You Are My Hero',
438 'description': 'md5:9c4d844bc0799cd3d2b5aed758a2050a',
439 },
440 'playlist_count': 40,
441 }, {
442 'url': 'https://www.iflix.com/play/0s682hc45t0ohll',
443 'info_dict': {
444 'id': '0s682hc45t0ohll',
445 'title': 'Miss Gu Who Is Silent',
446 'description': 'md5:a9651d0236f25af06435e845fa2f8c78',
447 },
448 'playlist_count': 20,
449 }]
450
451 def _real_extract(self, url):
452 return self._extract_series(url, IflixEpisodeIE)