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