]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tencent.py
[extractor/youtube] Fix bug in handling of music URLs
[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']:
70 if video_format.get('hls'):
71 fmts, subs = self._extract_m3u8_formats_and_subtitles(
72 video_format['url'] + video_format['hls']['pt'], video_id, 'mp4', fatal=False)
73 for f in fmts:
74 f.update({'width': video_width, 'height': video_height})
75
76 formats.extend(fmts)
77 self._merge_subtitles(subs, target=subtitles)
78 else:
79 formats.append({
80 'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
81 'width': video_width,
82 'height': video_height,
83 'ext': 'mp4',
84 })
85
86 return formats, subtitles
87
88 def _extract_video_native_subtitles(self, api_response, subtitles_format):
89 subtitles = {}
90 for subtitle in traverse_obj(api_response, ('sfl', 'fi')) or ():
91 subtitles.setdefault(subtitle['lang'].lower(), []).append({
92 'url': subtitle['url'],
93 'ext': subtitles_format,
94 'protocol': 'm3u8_native' if determine_ext(subtitle['url']) == 'm3u8' else 'http',
95 })
96
97 return subtitles
98
99 def _extract_all_video_formats_and_subtitles(self, url, video_id, series_id):
100 formats, subtitles = [], {}
101 for video_format, subtitle_format, video_quality in (
102 # '': 480p, 'shd': 720p, 'fhd': 1080p
103 ('mp4', 'srt', ''), ('hls', 'vtt', 'shd'), ('hls', 'vtt', 'fhd')):
104 api_response = self._get_video_api_response(
105 url, video_id, series_id, subtitle_format, video_format, video_quality)
106
107 if api_response.get('em') != 0 and api_response.get('exem') != 0:
108 if '您所在区域暂无此内容版权' in api_response.get('msg'):
109 self.raise_geo_restricted()
110 raise ExtractorError(f'Tencent said: {api_response.get("msg")}')
111
112 fmts, subs = self._extract_video_formats_and_subtitles(api_response, video_id)
113 native_subtitles = self._extract_video_native_subtitles(api_response, subtitle_format)
114
115 formats.extend(fmts)
116 self._merge_subtitles(subs, native_subtitles, target=subtitles)
117
118 self._sort_formats(formats)
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 },
190 }]
191
192 def _real_extract(self, url):
193 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
194 webpage = self._download_webpage(url, video_id)
195 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
196
197 formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
198 return {
199 'id': video_id,
200 'title': self._get_clean_title(self._og_search_title(webpage)
201 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'title'))),
202 'description': (self._og_search_description(webpage)
203 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'desc'))),
204 'formats': formats,
205 'subtitles': subtitles,
206 'thumbnail': (self._og_search_thumbnail(webpage)
207 or traverse_obj(webpage_metadata, ('global', 'videoInfo', 'pic160x90'))),
208 'series': traverse_obj(webpage_metadata, ('global', 'coverInfo', 'title')),
209 }
210
211
212class VQQSeriesIE(VQQBaseIE):
213 IE_NAME = 'vqq:series'
214 _VALID_URL = VQQBaseIE._VALID_URL_BASE + r'/x/cover/(?P<id>\w+)\.html/?(?:[?#]|$)'
215
216 _TESTS = [{
217 'url': 'https://v.qq.com/x/cover/7ce5noezvafma27.html',
218 'info_dict': {
219 'id': '7ce5noezvafma27',
220 'title': '鸡毛飞上天',
221 'description': 'md5:8cae3534327315b3872fbef5e51b5c5b',
222 },
223 'playlist_count': 55,
224 }, {
225 'url': 'https://v.qq.com/x/cover/oshd7r0vy9sfq8e.html',
226 'info_dict': {
227 'id': 'oshd7r0vy9sfq8e',
228 'title': '恋爱细胞2',
229 'description': 'md5:9d8a2245679f71ca828534b0f95d2a03',
230 },
231 'playlist_count': 12,
232 }]
233
234 def _real_extract(self, url):
235 series_id = self._match_id(url)
236 webpage = self._download_webpage(url, series_id)
237 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
238
239 episode_paths = [f'/x/cover/{series_id}/{video_id}.html' for video_id in re.findall(
240 r'<div[^>]+data-vid="(?P<video_id>[^"]+)"[^>]+class="[^"]+episode-item-rect--number',
241 webpage)]
242
243 return self.playlist_from_matches(
244 episode_paths, series_id, ie=VQQVideoIE, getter=functools.partial(urljoin, url),
245 title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
246 or self._og_search_title(webpage)),
247 description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
248 or self._og_search_description(webpage)))
249
250
251class WeTvBaseIE(TencentBaseIE):
252 _VALID_URL_BASE = r'https?://(?:www\.)?wetv\.vip/(?:[^?#]+/)?play'
253
254 _API_URL = 'https://play.wetv.vip/getvinfo'
255 _APP_VERSION = '3.5.57'
256 _PLATFORM = '4830201'
257 _HOST = 'wetv.vip'
258 _REFERER = 'wetv.vip'
259
260 def _get_webpage_metadata(self, webpage, video_id):
261 return self._parse_json(
262 traverse_obj(self._search_nextjs_data(webpage, video_id), ('props', 'pageProps', 'data')),
263 video_id, fatal=False)
264
48f535f5
E
265 def _extract_episode(self, url):
266 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
267 webpage = self._download_webpage(url, video_id)
268 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
269
270 formats, subtitles = self._extract_all_video_formats_and_subtitles(url, video_id, series_id)
271 return {
272 'id': video_id,
273 'title': self._get_clean_title(self._og_search_title(webpage)
274 or traverse_obj(webpage_metadata, ('coverInfo', 'title'))),
275 'description': (traverse_obj(webpage_metadata, ('coverInfo', 'description'))
276 or self._og_search_description(webpage)),
277 'formats': formats,
278 'subtitles': subtitles,
279 'thumbnail': self._og_search_thumbnail(webpage),
280 'duration': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'duration'))),
281 'series': traverse_obj(webpage_metadata, ('coverInfo', 'title')),
282 'episode_number': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'episode'))),
283 }
284
285 def _extract_series(self, url, ie):
286 series_id = self._match_id(url)
287 webpage = self._download_webpage(url, series_id)
288 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
289
290 episode_paths = ([f'/play/{series_id}/{episode["vid"]}' for episode in webpage_metadata.get('videoList')]
291 or re.findall(r'<a[^>]+class="play-video__link"[^>]+href="(?P<path>[^"]+)', webpage))
292
293 return self.playlist_from_matches(
294 episode_paths, series_id, ie=ie, getter=functools.partial(urljoin, url),
295 title=self._get_clean_title(traverse_obj(webpage_metadata, ('coverInfo', 'title'))
296 or self._og_search_title(webpage)),
297 description=(traverse_obj(webpage_metadata, ('coverInfo', 'description'))
298 or self._og_search_description(webpage)))
299
b2a4db42
E
300
301class WeTvEpisodeIE(WeTvBaseIE):
302 IE_NAME = 'wetv:episode'
303 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
304
305 _TESTS = [{
306 'url': 'https://wetv.vip/en/play/air11ooo2rdsdi3-Cute-Programmer/v0040pr89t9-EP1-Cute-Programmer',
307 'md5': '0c70fdfaa5011ab022eebc598e64bbbe',
308 'info_dict': {
309 'id': 'v0040pr89t9',
310 'ext': 'mp4',
311 'title': 'EP1: Cute Programmer',
312 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
313 'thumbnail': r're:^https?://[^?#]+air11ooo2rdsdi3',
314 'series': 'Cute Programmer',
315 'episode': 'Episode 1',
316 'episode_number': 1,
317 'duration': 2835,
318 },
319 }, {
320 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu/p0039b9nvik',
321 'md5': '3b3c15ca4b9a158d8d28d5aa9d7c0a49',
322 'info_dict': {
323 'id': 'p0039b9nvik',
324 'ext': 'mp4',
325 'title': 'EP1: You Are My Glory',
326 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
327 'thumbnail': r're:^https?://[^?#]+u37kgfnfzs73kiu',
328 'series': 'You Are My Glory',
329 'episode': 'Episode 1',
330 'episode_number': 1,
331 'duration': 2454,
332 },
333 }, {
334 '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',
335 'md5': '71133f5c2d5d6cad3427e1b010488280',
336 'info_dict': {
337 'id': 'i0042y00lxp',
338 'ext': 'mp4',
339 'title': 'md5:f7a0857dbe5fbbe2e7ad630b92b54e6a',
340 'description': 'md5:76260cb9cdc0ef76826d7ca9d92fadfa',
341 'thumbnail': r're:^https?://[^?#]+lcxgwod5hapghvw',
342 'series': 'WeTV PICK-A-BOO',
343 'episode': 'Episode 0',
344 'episode_number': 0,
345 'duration': 442,
346 },
347 }]
348
349 def _real_extract(self, url):
48f535f5 350 return self._extract_episode(url)
b2a4db42
E
351
352
353class WeTvSeriesIE(WeTvBaseIE):
354 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
355
356 _TESTS = [{
357 'url': 'https://wetv.vip/play/air11ooo2rdsdi3-Cute-Programmer',
358 'info_dict': {
359 'id': 'air11ooo2rdsdi3',
360 'title': 'Cute Programmer',
361 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
362 },
363 'playlist_count': 30,
364 }, {
365 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu-You-Are-My-Glory',
366 'info_dict': {
367 'id': 'u37kgfnfzs73kiu',
368 'title': 'You Are My Glory',
369 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
370 },
371 'playlist_count': 32,
372 }]
373
374 def _real_extract(self, url):
48f535f5 375 return self._extract_series(url, WeTvEpisodeIE)
b2a4db42 376
b2a4db42 377
48f535f5
E
378class IflixBaseIE(WeTvBaseIE):
379 _VALID_URL_BASE = r'https?://(?:www\.)?iflix\.com/(?:[^?#]+/)?play'
380
381 _API_URL = 'https://vplay.iflix.com/getvinfo'
382 _APP_VERSION = '3.5.57'
383 _PLATFORM = '330201'
384 _HOST = 'www.iflix.com'
385 _REFERER = 'www.iflix.com'
386
387
388class IflixEpisodeIE(IflixBaseIE):
389 IE_NAME = 'iflix:episode'
390 _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
391
392 _TESTS = [{
393 'url': 'https://www.iflix.com/en/play/daijrxu03yypu0s/a0040kvgaza',
394 'md5': '9740f9338c3a2105290d16b68fb3262f',
395 'info_dict': {
396 'id': 'a0040kvgaza',
397 'ext': 'mp4',
398 'title': 'EP1: Put Your Head On My Shoulder 2021',
399 'description': 'md5:c095a742d3b7da6dfedd0c8170727a42',
400 'thumbnail': r're:^https?://[^?#]+daijrxu03yypu0s',
401 'series': 'Put Your Head On My Shoulder 2021',
402 'episode': 'Episode 1',
403 'episode_number': 1,
404 'duration': 2639,
405 },
406 }, {
407 'url': 'https://www.iflix.com/en/play/fvvrcc3ra9lbtt1-Take-My-Brother-Away/i0029sd3gm1-EP1%EF%BC%9ATake-My-Brother-Away',
408 'md5': '375c9b8478fdedca062274b2c2f53681',
409 'info_dict': {
410 'id': 'i0029sd3gm1',
411 'ext': 'mp4',
412 'title': 'EP1:Take My Brother Away',
413 'description': 'md5:f0f7be1606af51cd94d5627de96b0c76',
414 'thumbnail': r're:^https?://[^?#]+fvvrcc3ra9lbtt1',
415 'series': 'Take My Brother Away',
416 'episode': 'Episode 1',
417 'episode_number': 1,
418 'duration': 228,
419 },
420 }]
421
422 def _real_extract(self, url):
423 return self._extract_episode(url)
424
425
426class IflixSeriesIE(IflixBaseIE):
427 _VALID_URL = IflixBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
428
429 _TESTS = [{
430 'url': 'https://www.iflix.com/en/play/g21a6qk4u1s9x22-You-Are-My-Hero',
431 'info_dict': {
432 'id': 'g21a6qk4u1s9x22',
433 'title': 'You Are My Hero',
434 'description': 'md5:9c4d844bc0799cd3d2b5aed758a2050a',
435 },
436 'playlist_count': 40,
437 }, {
438 'url': 'https://www.iflix.com/play/0s682hc45t0ohll',
439 'info_dict': {
440 'id': '0s682hc45t0ohll',
441 'title': 'Miss Gu Who Is Silent',
442 'description': 'md5:a9651d0236f25af06435e845fa2f8c78',
443 },
444 'playlist_count': 20,
445 }]
446
447 def _real_extract(self, url):
448 return self._extract_series(url, IflixEpisodeIE)