Release 2024.12.03
[yt-dlp3.git] / yt_dlp / extractor / tencent.py
blobb281ad1a9f52adbe3c18f2ee27df27e2096e29f8
1 import random
2 import re
3 import string
4 import time
6 from .common import InfoExtractor
7 from ..aes import aes_cbc_encrypt_bytes
8 from ..utils import (
9 ExtractorError,
10 determine_ext,
11 float_or_none,
12 int_or_none,
13 js_to_json,
14 traverse_obj,
15 urljoin,
19 class TencentBaseIE(InfoExtractor):
20 """Subclasses must set _API_URL, _APP_VERSION, _PLATFORM, _HOST, _REFERER"""
22 def _check_api_response(self, api_response):
23 msg = api_response.get('msg')
24 if api_response.get('code') != '0.0' and msg is not None:
25 if msg in (
26 '您所在区域暂无此内容版权(如设置VPN请关闭后重试)',
27 'This content is not available in your area due to copyright restrictions. Please choose other videos.',
29 self.raise_geo_restricted()
30 raise ExtractorError(f'Tencent said: {msg}')
32 def _get_ckey(self, video_id, url, guid):
33 ua = self.get_param('http_headers')['User-Agent']
35 payload = (f'{video_id}|{int(time.time())}|mg3c3b04ba|{self._APP_VERSION}|{guid}|'
36 f'{self._PLATFORM}|{url[:48]}|{ua.lower()[:48]}||Mozilla|Netscape|Windows x86_64|00|')
38 return aes_cbc_encrypt_bytes(
39 bytes(f'|{sum(map(ord, payload))}|{payload}', 'utf-8'),
40 b'Ok\xda\xa3\x9e/\x8c\xb0\x7f^r-\x9e\xde\xf3\x14',
41 b'\x01PJ\xf3V\xe6\x19\xcf.B\xbb\xa6\x8c?p\xf9',
42 padding_mode='whitespace').hex().upper()
44 def _get_video_api_response(self, video_url, video_id, series_id, subtitle_format, video_format, video_quality):
45 guid = ''.join(random.choices(string.digits + string.ascii_lowercase, k=16))
46 ckey = self._get_ckey(video_id, video_url, guid)
47 query = {
48 'vid': video_id,
49 'cid': series_id,
50 'cKey': ckey,
51 'encryptVer': '8.1',
52 'spcaptiontype': '1' if subtitle_format == 'vtt' else '0',
53 'sphls': '2' if video_format == 'hls' else '0',
54 'dtype': '3' if video_format == 'hls' else '0',
55 'defn': video_quality,
56 'spsrt': '2', # Enable subtitles
57 'sphttps': '1', # Enable HTTPS
58 'otype': 'json',
59 'spwm': '1',
60 'hevclv': '28', # Enable HEVC
61 'drm': '40', # Enable DRM
62 # For HDR
63 'spvideo': '4',
64 'spsfrhdr': '100',
65 # For SHD
66 'host': self._HOST,
67 'referer': self._REFERER,
68 'ehost': video_url,
69 'appVer': self._APP_VERSION,
70 'platform': self._PLATFORM,
71 # For VQQ
72 'guid': guid,
73 'flowid': ''.join(random.choices(string.digits + string.ascii_lowercase, k=32)),
76 return self._search_json(r'QZOutputJson=', self._download_webpage(
77 self._API_URL, video_id, query=query), 'api_response', video_id)
79 def _extract_video_formats_and_subtitles(self, api_response, video_id):
80 video_response = api_response['vl']['vi'][0]
82 formats, subtitles = [], {}
83 for video_format in video_response['ul']['ui']:
84 if video_format.get('hls') or determine_ext(video_format['url']) == 'm3u8':
85 fmts, subs = self._extract_m3u8_formats_and_subtitles(
86 video_format['url'] + traverse_obj(video_format, ('hls', 'pt'), default=''),
87 video_id, 'mp4', fatal=False)
89 formats.extend(fmts)
90 self._merge_subtitles(subs, target=subtitles)
91 else:
92 formats.append({
93 'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
94 'ext': 'mp4',
97 identifier = video_response.get('br')
98 format_response = traverse_obj(
99 api_response, ('fl', 'fi', lambda _, v: v['br'] == identifier),
100 expected_type=dict, get_all=False) or {}
101 common_info = {
102 'width': video_response.get('vw'),
103 'height': video_response.get('vh'),
104 'abr': float_or_none(format_response.get('audiobandwidth'), scale=1000),
105 'vbr': float_or_none(format_response.get('bandwidth'), scale=1000),
106 'fps': format_response.get('vfps'),
107 'format': format_response.get('sname'),
108 'format_id': format_response.get('name'),
109 'format_note': format_response.get('resolution'),
110 'dynamic_range': {'hdr10': 'hdr10'}.get(format_response.get('name'), 'sdr'),
111 'has_drm': format_response.get('drm', 0) != 0,
113 for f in formats:
114 f.update(common_info)
116 return formats, subtitles
118 def _extract_video_native_subtitles(self, api_response):
119 subtitles = {}
120 for subtitle in traverse_obj(api_response, ('sfl', 'fi')) or ():
121 subtitles.setdefault(subtitle['lang'].lower(), []).append({
122 'url': subtitle['url'],
123 'ext': 'srt' if subtitle.get('captionType') == 1 else 'vtt',
124 'protocol': 'm3u8_native' if determine_ext(subtitle['url']) == 'm3u8' else 'http',
127 return subtitles
129 def _extract_all_video_formats_and_subtitles(self, url, video_id, series_id):
130 api_responses = [self._get_video_api_response(url, video_id, series_id, 'srt', 'hls', 'hd')]
131 self._check_api_response(api_responses[0])
132 qualities = traverse_obj(api_responses, (0, 'fl', 'fi', ..., 'name')) or ('shd', 'fhd')
133 for q in qualities:
134 if q not in ('ld', 'sd', 'hd'):
135 api_responses.append(self._get_video_api_response(
136 url, video_id, series_id, 'vtt', 'hls', q))
137 self._check_api_response(api_responses[-1])
139 formats, subtitles = [], {}
140 for api_response in api_responses:
141 fmts, subs = self._extract_video_formats_and_subtitles(api_response, video_id)
142 native_subtitles = self._extract_video_native_subtitles(api_response)
144 formats.extend(fmts)
145 self._merge_subtitles(subs, native_subtitles, target=subtitles)
147 return formats, subtitles
149 def _get_clean_title(self, title):
150 return re.sub(
151 r'\s*[_\-]\s*(?:Watch online|Watch HD Video Online|WeTV|腾讯视频|(?:高清)?1080P在线观看平台).*?$',
152 '', title or '').strip() or None
155 class VQQBaseIE(TencentBaseIE):
156 _VALID_URL_BASE = r'https?://v\.qq\.com'
158 _API_URL = 'https://h5vv6.video.qq.com/getvinfo'
159 _APP_VERSION = '3.5.57'
160 _PLATFORM = '10901'
161 _HOST = 'v.qq.com'
162 _REFERER = 'v.qq.com'
164 def _get_webpage_metadata(self, webpage, video_id):
165 return self._search_json(
166 r'<script[^>]*>[^<]*window\.__(?:pinia|PINIA__)\s*=',
167 webpage, 'pinia data', video_id, transform_source=js_to_json, fatal=False)
170 class VQQVideoIE(VQQBaseIE):
171 IE_NAME = 'vqq:video'
172 _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/(?:page|cover/(?P<series_id>\w+))/(?P<id>\w+)'
174 _TESTS = [{
175 'url': 'https://v.qq.com/x/page/q326831cny0.html',
176 'md5': 'b11c9cb781df710d686b950376676e2a',
177 'info_dict': {
178 'id': 'q326831cny0',
179 'ext': 'mp4',
180 'title': '我是选手:雷霆裂阵,终极时刻',
181 'description': 'md5:e7ed70be89244017dac2a835a10aeb1e',
182 'thumbnail': r're:^https?://[^?#]+q326831cny0',
183 'format_id': r're:^shd',
185 }, {
186 'url': 'https://v.qq.com/x/page/o3013za7cse.html',
187 'md5': 'a1bcf42c6d28c189bd2fe2d468abb287',
188 'info_dict': {
189 'id': 'o3013za7cse',
190 'ext': 'mp4',
191 'title': '欧阳娜娜VLOG',
192 'description': 'md5:29fe847497a98e04a8c3826e499edd2e',
193 'thumbnail': r're:^https?://[^?#]+o3013za7cse',
194 'format_id': r're:^shd',
196 }, {
197 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27/a00269ix3l8.html',
198 'md5': '87968df6238a65d2478f19c25adf850b',
199 'info_dict': {
200 'id': 'a00269ix3l8',
201 'ext': 'mp4',
202 'title': '鸡毛飞上天 第01集',
203 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
204 'thumbnail': r're:^https?://[^?#]+7ce5noezvafma27',
205 'series': '鸡毛飞上天',
206 'format_id': r're:^shd',
208 'skip': '404',
209 }, {
210 'url': 'https://v.qq.com/x/cover/mzc00200p29k31e/s0043cwsgj0.html',
211 'md5': 'fadd10bf88aec3420f06f19ee1d24c5b',
212 'info_dict': {
213 'id': 's0043cwsgj0',
214 'ext': 'mp4',
215 'title': '第1集:如何快乐吃糖?',
216 'description': 'md5:1d8c3a0b8729ae3827fa5b2d3ebd5213',
217 'thumbnail': r're:^https?://[^?#]+s0043cwsgj0',
218 'series': '青年理工工作者生活研究所',
219 'format_id': r're:^shd',
221 'params': {'skip_download': 'm3u8'},
222 }, {
223 # Geo-restricted to China
224 'url': 'https://v.qq.com/x/cover/mcv8hkc8zk8lnov/x0036x5qqsr.html',
225 'only_matching': True,
228 def _real_extract(self, url):
229 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
230 webpage = self._download_webpage(url, video_id)
231 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
233 formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
234 return {
235 'id': video_id,
236 'title': self._get_clean_title(self._og_search_title(webpage)
237 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'title'))),
238 'description': (self._og_search_description(webpage)
239 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'desc'))),
240 'formats': formats,
241 'subtitles': subtitles,
242 'thumbnail': (self._og_search_thumbnail(webpage)
243 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'pic160x90'))),
244 'series': traverse_obj(webpage_metadata, ('global', 'coverInfo', 'title')),
248 class VQQSeriesIE(VQQBaseIE):
249 IE_NAME = 'vqq:series'
250 _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/cover/(?P<id>\w+)\.html/?(?:[?#]|$)'
252 _TESTS = [{
253 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27.html',
254 'info_dict': {
255 'id': '7ce5noezvafma27',
256 'title': '鸡毛飞上天',
257 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
259 'playlist_count': 55,
260 }, {
261 'url': 'https://v.qq.com/x/cover/oshd7r0vy9sfq8e.html',
262 'info_dict': {
263 'id': 'oshd7r0vy9sfq8e',
264 'title': '恋爱细胞2',
265 'description': 'md5:9d8a2245679f71ca828534b0f95d2a03',
267 'playlist_count': 12,
270 def _real_extract(self, url):
271 series_id = self._match_id(url)
272 webpage = self._download_webpage(url, series_id)
273 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
275 episode_paths = [f'/x/cover/{series_id}/{video_id}.html' for video_id in re.findall(
276 r'<div[^>]+data-vid="(?P<video_id>[^"]+)"[^>]+class="[^"]+episode-item-rect--number',
277 webpage)]
279 return self.playlist_from_matches(
280 episode_paths, series_id, ie=VQQVideoIE, getter=urljoin(url),
281 title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
282 or self._og_search_title(webpage)),
283 description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
284 or self._og_search_description(webpage)))
287 class WeTvBaseIE(TencentBaseIE):
288 _VALID_URL_BASE = r'https?://(?:www\.)?wetv\.vip/(?:[^?#]+/)?play'
290 _API_URL = 'https://play.wetv.vip/getvinfo'
291 _APP_VERSION = '3.5.57'
292 _PLATFORM = '4830201'
293 _HOST = 'wetv.vip'
294 _REFERER = 'wetv.vip'
296 def _get_webpage_metadata(self, webpage, video_id):
297 return self._parse_json(
298 traverse_obj(self._search_nextjs_data(webpage, video_id), ('props', 'pageProps', 'data')),
299 video_id, fatal=False)
301 def _extract_episode(self, url):
302 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
303 webpage = self._download_webpage(url, video_id)
304 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
306 formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
307 return {
308 'id': video_id,
309 'title': self._get_clean_title(self._og_search_title(webpage)
310 or traverse_obj(webpage_metadata, ('coverInfo', 'title'))),
311 'description': (traverse_obj(webpage_metadata, ('coverInfo', 'description'))
312 or self._og_search_description(webpage)),
313 'formats': formats,
314 'subtitles': subtitles,
315 'thumbnail': self._og_search_thumbnail(webpage),
316 'duration': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'duration'))),
317 'series': traverse_obj(webpage_metadata, ('coverInfo', 'title')),
318 'episode_number': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'episode'))),
321 def _extract_series(self, url, ie):
322 series_id = self._match_id(url)
323 webpage = self._download_webpage(url, series_id)
324 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
326 episode_paths = ([f'/play/{series_id}/{episode["vid"]}' for episode in webpage_metadata.get('videoList')]
327 or re.findall(r'<a[^>]+class="play-video__link"[^>]+href="(?P<path>[^"]+)', webpage))
329 return self.playlist_from_matches(
330 episode_paths, series_id, ie=ie, getter=urljoin(url),
331 title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
332 or self._og_search_title(webpage)),
333 description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
334 or self._og_search_description(webpage)))
337 class WeTvEpisodeIE(WeTvBaseIE):
338 IE_NAME = 'wetv:episode'
339 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
341 _TESTS = [{
342 'url': 'https://wetv.vip/en/play/air11ooo2rdsdi3-Cute-Programmer/v0040pr89t9-EP1-Cute-Programmer',
343 'md5': '0c70fdfaa5011ab022eebc598e64bbbe',
344 'info_dict': {
345 'id': 'v0040pr89t9',
346 'ext': 'mp4',
347 'title': 'EP1: Cute Programmer',
348 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
349 'thumbnail': r're:^https?://[^?#]+air11ooo2rdsdi3',
350 'series': 'Cute Programmer',
351 'episode': 'Episode 1',
352 'episode_number': 1,
353 'duration': 2835,
354 'format_id': r're:^shd',
356 }, {
357 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu/p0039b9nvik',
358 'md5': '3b3c15ca4b9a158d8d28d5aa9d7c0a49',
359 'info_dict': {
360 'id': 'p0039b9nvik',
361 'ext': 'mp4',
362 'title': 'EP1: You Are My Glory',
363 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
364 'thumbnail': r're:^https?://[^?#]+u37kgfnfzs73kiu',
365 'series': 'You Are My Glory',
366 'episode': 'Episode 1',
367 'episode_number': 1,
368 'duration': 2454,
369 'format_id': r're:^shd',
371 }, {
372 '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',
373 'md5': '71133f5c2d5d6cad3427e1b010488280',
374 'info_dict': {
375 'id': 'i0042y00lxp',
376 'ext': 'mp4',
377 'title': 'md5:f7a0857dbe5fbbe2e7ad630b92b54e6a',
378 'description': 'md5:76260cb9cdc0ef76826d7ca9d92fadfa',
379 'thumbnail': r're:^https?://[^?#]+i0042y00lxp',
380 'series': 'WeTV PICK-A-BOO',
381 'episode': 'Episode 0',
382 'episode_number': 0,
383 'duration': 442,
384 'format_id': r're:^shd',
388 def _real_extract(self, url):
389 return self._extract_episode(url)
392 class WeTvSeriesIE(WeTvBaseIE):
393 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
395 _TESTS = [{
396 'url': 'https://wetv.vip/play/air11ooo2rdsdi3-Cute-Programmer',
397 'info_dict': {
398 'id': 'air11ooo2rdsdi3',
399 'title': 'Cute Programmer',
400 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
402 'playlist_count': 30,
403 }, {
404 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu-You-Are-My-Glory',
405 'info_dict': {
406 'id': 'u37kgfnfzs73kiu',
407 'title': 'You Are My Glory',
408 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
410 'playlist_count': 32,
413 def _real_extract(self, url):
414 return self._extract_series(url, WeTvEpisodeIE)
417 class IflixBaseIE(WeTvBaseIE):
418 _VALID_URL_BASE = r'https?://(?:www\.)?iflix\.com/(?:[^?#]+/)?play'
420 _API_URL = 'https://vplay.iflix.com/getvinfo'
421 _APP_VERSION = '3.5.57'
422 _PLATFORM = '330201'
423 _HOST = 'www.iflix.com'
424 _REFERER = 'www.iflix.com'
427 class IflixEpisodeIE(IflixBaseIE):
428 IE_NAME = 'iflix:episode'
429 _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
431 _TESTS = [{
432 'url': 'https://www.iflix.com/en/play/daijrxu03yypu0s/a0040kvgaza',
433 'md5': '9740f9338c3a2105290d16b68fb3262f',
434 'info_dict': {
435 'id': 'a0040kvgaza',
436 'ext': 'mp4',
437 'title': 'EP1: Put Your Head On My Shoulder 2021',
438 'description': 'md5:c095a742d3b7da6dfedd0c8170727a42',
439 'thumbnail': r're:^https?://[^?#]+daijrxu03yypu0s',
440 'series': 'Put Your Head On My Shoulder 2021',
441 'episode': 'Episode 1',
442 'episode_number': 1,
443 'duration': 2639,
444 'format_id': r're:^shd',
446 }, {
447 'url': 'https://www.iflix.com/en/play/fvvrcc3ra9lbtt1-Take-My-Brother-Away/i0029sd3gm1-EP1%EF%BC%9ATake-My-Brother-Away',
448 'md5': '375c9b8478fdedca062274b2c2f53681',
449 'info_dict': {
450 'id': 'i0029sd3gm1',
451 'ext': 'mp4',
452 'title': 'EP1:Take My Brother Away',
453 'description': 'md5:f0f7be1606af51cd94d5627de96b0c76',
454 'thumbnail': r're:^https?://[^?#]+fvvrcc3ra9lbtt1',
455 'series': 'Take My Brother Away',
456 'episode': 'Episode 1',
457 'episode_number': 1,
458 'duration': 228,
459 'format_id': r're:^shd',
463 def _real_extract(self, url):
464 return self._extract_episode(url)
467 class IflixSeriesIE(IflixBaseIE):
468 _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
470 _TESTS = [{
471 'url': 'https://www.iflix.com/en/play/g21a6qk4u1s9x22-You-Are-My-Hero',
472 'info_dict': {
473 'id': 'g21a6qk4u1s9x22',
474 'title': 'You Are My Hero',
475 'description': 'md5:9c4d844bc0799cd3d2b5aed758a2050a',
477 'playlist_count': 40,
478 }, {
479 'url': 'https://www.iflix.com/play/0s682hc45t0ohll',
480 'info_dict': {
481 'id': '0s682hc45t0ohll',
482 'title': 'Miss Gu Who Is Silent',
483 'description': 'md5:a9651d0236f25af06435e845fa2f8c78',
485 'playlist_count': 20,
488 def _real_extract(self, url):
489 return self._extract_series(url, IflixEpisodeIE)