]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/wetv.py
[extractor/wetv] Add extractors (#4330)
[yt-dlp.git] / yt_dlp / extractor / wetv.py
1 import re
2 import time
3
4 from .common import InfoExtractor
5 from ..aes import aes_cbc_encrypt
6 from ..utils import bytes_to_intlist, determine_ext, intlist_to_bytes, int_or_none, traverse_obj
7
8
9 class WeTvBaseIE(InfoExtractor):
10 _VALID_URL_BASE = r'https?://(?:www\.)?wetv\.vip/(?:[^?#]+/)?play'
11
12 def _get_ckey(self, video_id, url, app_version, platform):
13 ua = self.get_param('http_headers')['User-Agent']
14
15 payload = (f'{video_id}|{int(time.time())}|mg3c3b04ba|{app_version}|0000000000000000|'
16 f'{platform}|{url[:48]}|{ua.lower()[:48]}||Mozilla|Netscape|Win32|00|')
17
18 ciphertext_int_bytes = aes_cbc_encrypt(
19 bytes_to_intlist(bytes(f'|{sum(map(ord, payload))}|{payload}', 'utf-8')),
20 bytes_to_intlist(b'Ok\xda\xa3\x9e/\x8c\xb0\x7f^r-\x9e\xde\xf3\x14'),
21 bytes_to_intlist(b'\x01PJ\xf3V\xe6\x19\xcf.B\xbb\xa6\x8c?p\xf9'),
22 'whitespace')
23
24 return intlist_to_bytes(ciphertext_int_bytes).hex()
25
26 def _get_video_api_response(self, video_url, video_id, series_id, subtitle_format, video_format, video_quality):
27 app_version = '3.5.57'
28 platform = '4830201'
29
30 ckey = self._get_ckey(video_id, video_url, app_version, platform)
31 query = {
32 'vid': video_id,
33 'cid': series_id,
34 'cKey': ckey,
35 'encryptVer': '8.1',
36 'spcaptiontype': '1' if subtitle_format == 'vtt' else '0', # 0 - SRT, 1 - VTT
37 'sphls': '1' if video_format == 'hls' else '0', # 0 - MP4, 1 - HLS
38 'defn': video_quality, # '': 480p, 'shd': 720p, 'fhd': 1080p
39 'spsrt': '1', # Enable subtitles
40 'sphttps': '1', # Enable HTTPS
41 'otype': 'json', # Response format: xml, json,
42 'dtype': '1',
43 'spwm': '1',
44 'host': 'wetv.vip', # These three values are needed for SHD
45 'referer': 'wetv.vip',
46 'ehost': video_url,
47 'appVer': app_version,
48 'platform': platform,
49 }
50
51 return self._search_json(r'QZOutputJson=', self._download_webpage(
52 'https://play.wetv.vip/getvinfo', video_id, query=query), 'api_response', video_id)
53
54 def _get_webpage_metadata(self, webpage, video_id):
55 return self._parse_json(
56 traverse_obj(self._search_nextjs_data(webpage, video_id), ('props', 'pageProps', 'data')),
57 video_id, fatal=False)
58
59
60 class WeTvEpisodeIE(WeTvBaseIE):
61 IE_NAME = 'wetv:episode'
62 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<series_id>\w+)(?:-[^?#]+)?/(?P<id>\w+)(?:-[^?#]+)?'
63
64 _TESTS = [{
65 'url': 'https://wetv.vip/en/play/air11ooo2rdsdi3-Cute-Programmer/v0040pr89t9-EP1-Cute-Programmer',
66 'md5': 'a046f565c9dce9b263a0465a422cd7bf',
67 'info_dict': {
68 'id': 'v0040pr89t9',
69 'ext': 'mp4',
70 'title': 'EP1: Cute Programmer',
71 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
72 'thumbnail': r're:^https?://[^?#]+air11ooo2rdsdi3',
73 'series': 'Cute Programmer',
74 'episode': 'Episode 1',
75 'episode_number': 1,
76 'duration': 2835,
77 },
78 }, {
79 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu/p0039b9nvik',
80 'md5': '4d9d69bcfd11da61f4aae64fc6b316b3',
81 'info_dict': {
82 'id': 'p0039b9nvik',
83 'ext': 'mp4',
84 'title': 'EP1: You Are My Glory',
85 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
86 'thumbnail': r're:^https?://[^?#]+u37kgfnfzs73kiu',
87 'series': 'You Are My Glory',
88 'episode': 'Episode 1',
89 'episode_number': 1,
90 'duration': 2454,
91 },
92 }, {
93 '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',
94 'md5': '71133f5c2d5d6cad3427e1b010488280',
95 'info_dict': {
96 'id': 'i0042y00lxp',
97 'ext': 'mp4',
98 'title': 'md5:f7a0857dbe5fbbe2e7ad630b92b54e6a',
99 'description': 'md5:76260cb9cdc0ef76826d7ca9d92fadfa',
100 'thumbnail': r're:^https?://[^?#]+lcxgwod5hapghvw',
101 'series': 'WeTV PICK-A-BOO',
102 'episode': 'Episode 0',
103 'episode_number': 0,
104 'duration': 442,
105 },
106 }]
107
108 def _extract_video_formats_and_subtitles(self, api_response, video_id, video_quality):
109 video_response = api_response['vl']['vi'][0]
110 video_width = video_response.get('vw')
111 video_height = video_response.get('vh')
112
113 formats, subtitles = [], {}
114 for video_format in video_response['ul']['ui']:
115 if video_format.get('hls'):
116 fmts, subs = self._extract_m3u8_formats_and_subtitles(
117 video_format['url'] + video_format['hls']['pname'], video_id, 'mp4', fatal=False)
118 for f in fmts:
119 f['width'] = video_width
120 f['height'] = video_height
121
122 formats.extend(fmts)
123 self._merge_subtitles(subs, target=subtitles)
124 else:
125 formats.append({
126 'url': f'{video_format["url"]}{video_response["fn"]}?vkey={video_response["fvkey"]}',
127 'width': video_width,
128 'height': video_height,
129 'ext': 'mp4',
130 })
131
132 return formats, subtitles
133
134 def _extract_video_subtitles(self, api_response, subtitles_format):
135 subtitles = {}
136 for subtitle in traverse_obj(api_response, ('sfl', 'fi')):
137 subtitles.setdefault(subtitle['lang'].lower(), []).append({
138 'url': subtitle['url'],
139 'ext': subtitles_format,
140 'protocol': 'm3u8_native' if determine_ext(subtitle['url']) == 'm3u8' else 'http',
141 })
142
143 return subtitles
144
145 def _real_extract(self, url):
146 video_id, series_id = self._match_valid_url(url).group('id', 'series_id')
147 webpage = self._download_webpage(url, video_id)
148
149 formats, subtitles = [], {}
150 for video_format, subtitle_format, video_quality in (('mp4', 'srt', ''), ('hls', 'vtt', 'shd'), ('hls', 'vtt', 'fhd')):
151 api_response = self._get_video_api_response(url, video_id, series_id, subtitle_format, video_format, video_quality)
152
153 fmts, subs = self._extract_video_formats_and_subtitles(api_response, video_id, video_quality)
154 native_subtitles = self._extract_video_subtitles(api_response, subtitle_format)
155
156 formats.extend(fmts)
157 self._merge_subtitles(subs, native_subtitles, target=subtitles)
158
159 self._sort_formats(formats)
160 webpage_metadata = self._get_webpage_metadata(webpage, video_id)
161
162 return {
163 'id': video_id,
164 'title': (self._og_search_title(webpage)
165 or traverse_obj(webpage_metadata, ('coverInfo', 'description'))),
166 'description': (self._og_search_description(webpage)
167 or traverse_obj(webpage_metadata, ('coverInfo', 'description'))),
168 'formats': formats,
169 'subtitles': subtitles,
170 'thumbnail': self._og_search_thumbnail(webpage),
171 'duration': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'duration'))),
172 'series': traverse_obj(webpage_metadata, ('coverInfo', 'title')),
173 'episode_number': int_or_none(traverse_obj(webpage_metadata, ('videoInfo', 'episode'))),
174 }
175
176
177 class WeTvSeriesIE(WeTvBaseIE):
178 _VALID_URL = WeTvBaseIE._VALID_URL_BASE + r'/(?P<id>\w+)(?:-[^/?#]+)?/?(?:[?#]|$)'
179
180 _TESTS = [{
181 'url': 'https://wetv.vip/play/air11ooo2rdsdi3-Cute-Programmer',
182 'info_dict': {
183 'id': 'air11ooo2rdsdi3',
184 'title': 'Cute Programmer',
185 'description': 'md5:e87beab3bf9f392d6b9e541a63286343',
186 },
187 'playlist_count': 30,
188 }, {
189 'url': 'https://wetv.vip/en/play/u37kgfnfzs73kiu-You-Are-My-Glory',
190 'info_dict': {
191 'id': 'u37kgfnfzs73kiu',
192 'title': 'You Are My Glory',
193 'description': 'md5:831363a4c3b4d7615e1f3854be3a123b',
194 },
195 'playlist_count': 32,
196 }]
197
198 def _real_extract(self, url):
199 series_id = self._match_id(url)
200 webpage = self._download_webpage(url, series_id)
201 webpage_metadata = self._get_webpage_metadata(webpage, series_id)
202
203 episode_paths = (re.findall(r'<a[^>]+class="play-video__link"[^>]+href="(?P<path>[^"]+)', webpage)
204 or [f'/{series_id}/{episode["vid"]}' for episode in webpage_metadata.get('videoList')])
205
206 return self.playlist_from_matches(
207 episode_paths, series_id, ie=WeTvEpisodeIE,
208 title=traverse_obj(webpage_metadata, ('coverInfo', 'title')) or self._og_search_title(webpage),
209 description=traverse_obj(webpage_metadata, ('coverInfo', 'description')) or self._og_search_description(webpage))