]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/vlive.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / vlive.py
CommitLineData
b92d3c53 1import itertools
38d70284 2import json
9d186afa 3
c88debff 4from .naver import NaverBaseIE
38d70284 5from ..compat import (
6 compat_HTTPError,
7 compat_str,
8)
061f62da 9from ..utils import (
9d186afa 10 ExtractorError,
38d70284 11 int_or_none,
c586f9e8 12 LazyList,
c88debff 13 merge_dicts,
38d70284 14 str_or_none,
15 strip_or_none,
5da08bde 16 traverse_obj,
661cc229 17 try_get,
89c63cc5 18 urlencode_postdata,
457f6d68 19 url_or_none,
061f62da 20)
061f62da 21
22
38d70284 23class VLiveBaseIE(NaverBaseIE):
457f6d68 24 _NETRC_MACHINE = 'vlive'
25 _logged_in = False
26
52efa4b3 27 def _perform_login(self, username, password):
28 if self._logged_in:
29 return
457f6d68 30 LOGIN_URL = 'https://www.vlive.tv/auth/email/login'
31 self._request_webpage(
32 LOGIN_URL, None, note='Downloading login cookies')
33
34 self._download_webpage(
35 LOGIN_URL, None, note='Logging in',
52efa4b3 36 data=urlencode_postdata({'email': username, 'pwd': password}),
457f6d68 37 headers={
38 'Referer': LOGIN_URL,
39 'Content-Type': 'application/x-www-form-urlencoded'
40 })
41
42 login_info = self._download_json(
43 'https://www.vlive.tv/auth/loginInfo', None,
44 note='Checking login status',
45 headers={'Referer': 'https://www.vlive.tv/home'})
46
47 if not try_get(login_info, lambda x: x['message']['login'], bool):
48 raise ExtractorError('Unable to log in', expected=True)
52efa4b3 49 VLiveBaseIE._logged_in = True
457f6d68 50
51 def _call_api(self, path_template, video_id, fields=None, query_add={}, note=None):
52 if note is None:
53 note = 'Downloading %s JSON metadata' % path_template.split('/')[-1].split('-')[0]
54 query = {'appId': '8c6cc7b45d2568fb668be6e05b6e5a3b', 'gcc': 'KR', 'platformType': 'PC'}
55 if fields:
56 query['fields'] = fields
57 if query_add:
58 query.update(query_add)
59 try:
60 return self._download_json(
61 'https://www.vlive.tv/globalv-web/vam-web/' + path_template % video_id, video_id,
62 note, headers={'Referer': 'https://www.vlive.tv/'}, query=query)
63 except ExtractorError as e:
64 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
65 self.raise_login_required(json.loads(e.cause.read().decode('utf-8'))['message'])
66 raise
38d70284 67
68
69class VLiveIE(VLiveBaseIE):
061f62da 70 IE_NAME = 'vlive'
38d70284 71 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/(?:video|embed)/(?P<id>[0-9]+)'
58355a3b 72 _TESTS = [{
38d70284 73 'url': 'http://www.vlive.tv/video/1326',
5dcfd250 74 'md5': 'cc7314812855ce56de70a06a27314983',
75 'info_dict': {
76 'id': '1326',
77 'ext': 'mp4',
38d70284 78 'title': "Girl's Day's Broadcast",
5dcfd250 79 'creator': "Girl's Day",
80 'view_count': int,
81 'uploader_id': 'muploader_a',
652fb0d4
AG
82 'upload_date': '20150817',
83 'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
84 'timestamp': 1439816449,
5da08bde 85 'like_count': int,
86 'channel': 'Girl\'s Day',
87 'channel_id': 'FDF27',
88 'comment_count': int,
89 'release_timestamp': 1439818140,
90 'release_date': '20150817',
91 'duration': 1014,
652fb0d4
AG
92 },
93 'params': {
94 'skip_download': True,
5dcfd250 95 },
38d70284 96 }, {
97 'url': 'http://www.vlive.tv/video/16937',
58355a3b
S
98 'info_dict': {
99 'id': '16937',
100 'ext': 'mp4',
38d70284 101 'title': '첸백시 걍방',
58355a3b
S
102 'creator': 'EXO',
103 'view_count': int,
104 'subtitles': 'mincount:12',
c88debff 105 'uploader_id': 'muploader_j',
652fb0d4
AG
106 'upload_date': '20161112',
107 'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
108 'timestamp': 1478923074,
5da08bde 109 'like_count': int,
110 'channel': 'EXO',
111 'channel_id': 'F94BD',
112 'comment_count': int,
113 'release_timestamp': 1478924280,
114 'release_date': '20161112',
115 'duration': 906,
58355a3b
S
116 },
117 'params': {
118 'skip_download': True,
119 },
01b517a2 120 }, {
121 'url': 'https://www.vlive.tv/video/129100',
122 'md5': 'ca2569453b79d66e5b919e5d308bff6b',
123 'info_dict': {
124 'id': '129100',
125 'ext': 'mp4',
4831ef7f
S
126 'title': '[V LIVE] [BTS+] Run BTS! 2019 - EP.71 :: Behind the scene',
127 'creator': 'BTS+',
01b517a2 128 'view_count': int,
129 'subtitles': 'mincount:10',
130 },
131 'skip': 'This video is only available for CH+ subscribers',
38d70284 132 }, {
133 'url': 'https://www.vlive.tv/embed/1326',
134 'only_matching': True,
135 }, {
136 # works only with gcc=KR
137 'url': 'https://www.vlive.tv/video/225019',
138 'only_matching': True,
3d54ebd4
KYK
139 }, {
140 'url': 'https://www.vlive.tv/video/223906',
141 'info_dict': {
142 'id': '58',
143 'title': 'RUN BTS!'
144 },
145 'playlist_mincount': 120
58355a3b 146 }]
061f62da 147
38d70284 148 def _real_extract(self, url):
149 video_id = self._match_id(url)
150
151 post = self._call_api(
152 'post/v1.0/officialVideoPost-%s', video_id,
3d54ebd4
KYK
153 'author{nickname},channel{channelCode,channelName},officialVideo{commentCount,exposeStatus,likeCount,playCount,playTime,status,title,type,vodId},playlist{playlistSeq,totalCount,name}')
154
f40ee5e9 155 playlist_id = str_or_none(try_get(post, lambda x: x['playlist']['playlistSeq']))
156 if not self._yes_playlist(playlist_id, video_id):
3d54ebd4
KYK
157 video = post['officialVideo']
158 return self._get_vlive_info(post, video, video_id)
3d54ebd4 159
f40ee5e9 160 playlist_name = str_or_none(try_get(post, lambda x: x['playlist']['name']))
161 playlist_count = str_or_none(try_get(post, lambda x: x['playlist']['totalCount']))
162
163 playlist = self._call_api(
164 'playlist/v1.0/playlist-%s/posts', playlist_id, 'data', {'limit': playlist_count})
3d54ebd4 165
f40ee5e9 166 entries = []
167 for video_data in playlist['data']:
168 video = video_data.get('officialVideo')
169 video_id = str_or_none(video.get('videoSeq'))
170 entries.append(self._get_vlive_info(video_data, video, video_id))
3d54ebd4 171
f40ee5e9 172 return self.playlist_result(entries, playlist_id, playlist_name)
3d54ebd4
KYK
173
174 def _get_vlive_info(self, post, video, video_id):
38d70284 175 def get_common_fields():
176 channel = post.get('channel') or {}
177 return {
178 'title': video.get('title'),
179 'creator': post.get('author', {}).get('nickname'),
180 'channel': channel.get('channelName'),
181 'channel_id': channel.get('channelCode'),
182 'duration': int_or_none(video.get('playTime')),
183 'view_count': int_or_none(video.get('playCount')),
184 'like_count': int_or_none(video.get('likeCount')),
185 'comment_count': int_or_none(video.get('commentCount')),
652fb0d4 186 'timestamp': int_or_none(video.get('createdAt'), scale=1000),
5da08bde 187 'release_timestamp': int_or_none(traverse_obj(video, 'onAirStartAt', 'willStartAt'), scale=1000),
652fb0d4 188 'thumbnail': video.get('thumb'),
38d70284 189 }
190
191 video_type = video.get('type')
192 if video_type == 'VOD':
193 inkey = self._call_api('video/v1.0/vod/%s/inkey', video_id)['inkey']
194 vod_id = video['vodId']
f0ff9979 195 info_dict = merge_dicts(
38d70284 196 get_common_fields(),
197 self._extract_video_info(video_id, vod_id, inkey))
f0ff9979 198 thumbnail = video.get('thumb')
199 if thumbnail:
200 if not info_dict.get('thumbnails') and info_dict.get('thumbnail'):
201 info_dict['thumbnails'] = [{'url': info_dict.pop('thumbnail')}]
202 info_dict.setdefault('thumbnails', []).append({'url': thumbnail, 'preference': 1})
203 return info_dict
38d70284 204 elif video_type == 'LIVE':
205 status = video.get('status')
206 if status == 'ON_AIR':
207 stream_url = self._call_api(
208 'old/v3/live/%s/playInfo',
209 video_id)['result']['adaptiveStreamUrl']
210 formats = self._extract_m3u8_formats(stream_url, video_id, 'mp4')
211 info = get_common_fields()
212 info.update({
39ca3b5c 213 'title': video['title'],
38d70284 214 'id': video_id,
215 'formats': formats,
216 'is_live': True,
217 })
218 return info
219 elif status == 'ENDED':
220 raise ExtractorError(
221 'Uploading for replay. Please wait...', expected=True)
222 elif status == 'RESERVED':
0536e60b 223 raise ExtractorError('Coming soon!', expected=True)
38d70284 224 elif video.get('exposeStatus') == 'CANCEL':
225 raise ExtractorError(
226 'We are sorry, but the live broadcast has been canceled.',
227 expected=True)
0536e60b 228 else:
38d70284 229 raise ExtractorError('Unknown status ' + status)
57774807 230
57774807 231
457f6d68 232class VLivePostIE(VLiveBaseIE):
38d70284 233 IE_NAME = 'vlive:post'
234 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/post/(?P<id>\d-\d+)'
235 _TESTS = [{
236 # uploadType = SOS
237 'url': 'https://www.vlive.tv/post/1-20088044',
238 'info_dict': {
239 'id': '1-20088044',
240 'title': 'Hola estrellitas la tierra les dice hola (si era así no?) Ha...',
241 'description': 'md5:fab8a1e50e6e51608907f46c7fa4b407',
242 },
243 'playlist_count': 3,
244 }, {
245 # uploadType = V
246 'url': 'https://www.vlive.tv/post/1-20087926',
247 'info_dict': {
248 'id': '1-20087926',
249 'title': 'James Corden: And so, the baby becamos the Papa💜😭💪😭',
250 },
251 'playlist_count': 1,
252 }]
253 _FVIDEO_TMPL = 'fvideo/v1.0/fvideo-%%s/%s'
d3260f40 254
38d70284 255 def _real_extract(self, url):
256 post_id = self._match_id(url)
d3260f40 257
38d70284 258 post = self._call_api(
259 'post/v1.0/post-%s', post_id,
260 'attachments{video},officialVideo{videoSeq},plainBody,title')
d3260f40 261
38d70284 262 video_seq = str_or_none(try_get(
263 post, lambda x: x['officialVideo']['videoSeq']))
264 if video_seq:
265 return self.url_result(
266 'http://www.vlive.tv/video/' + video_seq,
267 VLiveIE.ie_key(), video_seq)
d3260f40 268
38d70284 269 title = post['title']
270 entries = []
271 for idx, video in enumerate(post['attachments']['video'].values()):
272 video_id = video.get('videoId')
273 if not video_id:
274 continue
275 upload_type = video.get('uploadType')
276 upload_info = video.get('uploadInfo') or {}
277 entry = None
278 if upload_type == 'SOS':
279 download = self._call_api(
457f6d68 280 self._FVIDEO_TMPL % 'sosPlayInfo', video_id)['videoUrl']['download']
38d70284 281 formats = []
282 for f_id, f_url in download.items():
283 formats.append({
284 'format_id': f_id,
285 'url': f_url,
286 'height': int_or_none(f_id[:-1]),
287 })
38d70284 288 entry = {
289 'formats': formats,
290 'id': video_id,
291 'thumbnail': upload_info.get('imageUrl'),
292 }
293 elif upload_type == 'V':
294 vod_id = upload_info.get('videoId')
295 if not vod_id:
296 continue
457f6d68 297 inkey = self._call_api(self._FVIDEO_TMPL % 'inKey', video_id)['inKey']
38d70284 298 entry = self._extract_video_info(video_id, vod_id, inkey)
299 if entry:
300 entry['title'] = '%s_part%s' % (title, idx)
301 entries.append(entry)
302 return self.playlist_result(
303 entries, post_id, title, strip_or_none(post.get('plainBody')))
d3260f40 304
305
38d70284 306class VLiveChannelIE(VLiveBaseIE):
b92d3c53 307 IE_NAME = 'vlive:channel'
457f6d68 308 _VALID_URL = r'https?://(?:channels\.vlive\.tv|(?:(?:www|m)\.)?vlive\.tv/channel)/(?P<channel_id>[0-9A-Z]+)(?:/board/(?P<posts_id>\d+))?'
1923b146 309 _TESTS = [{
38d70284 310 'url': 'http://channels.vlive.tv/FCD4B',
1923b146 311 'info_dict': {
312 'id': 'FCD4B',
313 'title': 'MAMAMOO',
314 },
315 'playlist_mincount': 110
316 }, {
317 'url': 'https://www.vlive.tv/channel/FCD4B',
38d70284 318 'only_matching': True,
457f6d68 319 }, {
320 'url': 'https://www.vlive.tv/channel/FCD4B/board/3546',
321 'info_dict': {
322 'id': 'FCD4B-3546',
323 'title': 'MAMAMOO - Star Board',
324 },
325 'playlist_mincount': 880
1923b146 326 }]
38d70284 327
457f6d68 328 def _entries(self, posts_id, board_name):
329 if board_name:
330 posts_path = 'post/v1.0/board-%s/posts'
331 query_add = {'limit': 100, 'sortType': 'LATEST'}
332 else:
333 posts_path = 'post/v1.0/channel-%s/starPosts'
334 query_add = {'limit': 100}
b92d3c53 335
336 for page_num in itertools.count(1):
38d70284 337 video_list = self._call_api(
457f6d68 338 posts_path, posts_id, 'channel{channelName},contentType,postId,title,url', query_add,
339 note=f'Downloading playlist page {page_num}')
340
341 for video in try_get(video_list, lambda x: x['data'], list) or []:
342 video_id = str(video.get('postId'))
343 video_title = str_or_none(video.get('title'))
344 video_url = url_or_none(video.get('url'))
345 if not all((video_id, video_title, video_url)) or video.get('contentType') != 'VIDEO':
346 continue
347 channel_name = try_get(video, lambda x: x['channel']['channelName'], compat_str)
348 yield self.url_result(video_url, VLivePostIE.ie_key(), video_id, video_title, channel=channel_name)
661cc229 349
457f6d68 350 after = try_get(video_list, lambda x: x['paging']['nextParams']['after'], compat_str)
351 if not after:
b92d3c53 352 break
457f6d68 353 query_add['after'] = after
354
355 def _real_extract(self, url):
356 channel_id, posts_id = self._match_valid_url(url).groups()
b92d3c53 357
457f6d68 358 board_name = None
359 if posts_id:
360 board = self._call_api(
361 'board/v1.0/board-%s', posts_id, 'title,boardType')
362 board_name = board.get('title') or 'Unknown'
363 if board.get('boardType') not in ('STAR', 'VLIVE_PLUS'):
364 raise ExtractorError(f'Board {board_name!r} is not supported', expected=True)
d02f1210 365
c586f9e8 366 entries = LazyList(self._entries(posts_id or channel_id, board_name))
367 channel_name = entries[0]['channel']
b92d3c53 368
369 return self.playlist_result(
c586f9e8 370 entries,
457f6d68 371 f'{channel_id}-{posts_id}' if posts_id else channel_id,
372 f'{channel_name} - {board_name}' if channel_name and board_name else channel_name)