]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/vlive.py
Merge pull request #143 from nixxo/gedi
[yt-dlp.git] / youtube_dlc / extractor / vlive.py
CommitLineData
061f62da 1# coding: utf-8
25bcd355 2from __future__ import unicode_literals
061f62da 3
b92d3c53 4import itertools
38d70284 5import json
9d186afa 6
c88debff 7from .naver import NaverBaseIE
38d70284 8from ..compat import (
9 compat_HTTPError,
10 compat_str,
11)
061f62da 12from ..utils import (
9d186afa 13 ExtractorError,
38d70284 14 int_or_none,
c88debff 15 merge_dicts,
38d70284 16 str_or_none,
17 strip_or_none,
661cc229 18 try_get,
89c63cc5 19 urlencode_postdata,
061f62da 20)
061f62da 21
22
38d70284 23class VLiveBaseIE(NaverBaseIE):
24 _APP_ID = '8c6cc7b45d2568fb668be6e05b6e5a3b'
25
26
27class VLiveIE(VLiveBaseIE):
061f62da 28 IE_NAME = 'vlive'
38d70284 29 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/(?:video|embed)/(?P<id>[0-9]+)'
01b517a2 30 _NETRC_MACHINE = 'vlive'
58355a3b 31 _TESTS = [{
38d70284 32 'url': 'http://www.vlive.tv/video/1326',
5dcfd250 33 'md5': 'cc7314812855ce56de70a06a27314983',
34 'info_dict': {
35 'id': '1326',
36 'ext': 'mp4',
38d70284 37 'title': "Girl's Day's Broadcast",
5dcfd250 38 'creator': "Girl's Day",
39 'view_count': int,
40 'uploader_id': 'muploader_a',
41 },
38d70284 42 }, {
43 'url': 'http://www.vlive.tv/video/16937',
58355a3b
S
44 'info_dict': {
45 'id': '16937',
46 'ext': 'mp4',
38d70284 47 'title': '첸백시 걍방',
58355a3b
S
48 'creator': 'EXO',
49 'view_count': int,
50 'subtitles': 'mincount:12',
c88debff 51 'uploader_id': 'muploader_j',
58355a3b
S
52 },
53 'params': {
54 'skip_download': True,
55 },
01b517a2 56 }, {
57 'url': 'https://www.vlive.tv/video/129100',
58 'md5': 'ca2569453b79d66e5b919e5d308bff6b',
59 'info_dict': {
60 'id': '129100',
61 'ext': 'mp4',
4831ef7f
S
62 'title': '[V LIVE] [BTS+] Run BTS! 2019 - EP.71 :: Behind the scene',
63 'creator': 'BTS+',
01b517a2 64 'view_count': int,
65 'subtitles': 'mincount:10',
66 },
67 'skip': 'This video is only available for CH+ subscribers',
38d70284 68 }, {
69 'url': 'https://www.vlive.tv/embed/1326',
70 'only_matching': True,
71 }, {
72 # works only with gcc=KR
73 'url': 'https://www.vlive.tv/video/225019',
74 'only_matching': True,
58355a3b 75 }]
061f62da 76
01b517a2 77 def _real_initialize(self):
78 self._login()
79
80 def _login(self):
81 email, password = self._get_login_info()
82 if None in (email, password):
83 return
84
85 def is_logged_in():
86 login_info = self._download_json(
87 'https://www.vlive.tv/auth/loginInfo', None,
88 note='Downloading login info',
89 headers={'Referer': 'https://www.vlive.tv/home'})
ef19739e
S
90 return try_get(
91 login_info, lambda x: x['message']['login'], bool) or False
01b517a2 92
93 LOGIN_URL = 'https://www.vlive.tv/auth/email/login'
ef19739e
S
94 self._request_webpage(
95 LOGIN_URL, None, note='Downloading login cookies')
01b517a2 96
97 self._download_webpage(
98 LOGIN_URL, None, note='Logging in',
99 data=urlencode_postdata({'email': email, 'pwd': password}),
100 headers={
101 'Referer': LOGIN_URL,
102 'Content-Type': 'application/x-www-form-urlencoded'
103 })
104
105 if not is_logged_in():
106 raise ExtractorError('Unable to log in', expected=True)
107
38d70284 108 def _call_api(self, path_template, video_id, fields=None):
109 query = {'appId': self._APP_ID, 'gcc': 'KR'}
110 if fields:
111 query['fields'] = fields
112 try:
113 return self._download_json(
114 'https://www.vlive.tv/globalv-web/vam-web/' + path_template % video_id, video_id,
115 'Downloading %s JSON metadata' % path_template.split('/')[-1].split('-')[0],
116 headers={'Referer': 'https://www.vlive.tv/'}, query=query)
117 except ExtractorError as e:
118 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
119 self.raise_login_required(json.loads(e.cause.read().decode())['message'])
120 raise
d3260f40 121
38d70284 122 def _real_extract(self, url):
123 video_id = self._match_id(url)
124
125 post = self._call_api(
126 'post/v1.0/officialVideoPost-%s', video_id,
127 'author{nickname},channel{channelCode,channelName},officialVideo{commentCount,exposeStatus,likeCount,playCount,playTime,status,title,type,vodId}')
128
129 video = post['officialVideo']
130
131 def get_common_fields():
132 channel = post.get('channel') or {}
133 return {
134 'title': video.get('title'),
135 'creator': post.get('author', {}).get('nickname'),
136 'channel': channel.get('channelName'),
137 'channel_id': channel.get('channelCode'),
138 'duration': int_or_none(video.get('playTime')),
139 'view_count': int_or_none(video.get('playCount')),
140 'like_count': int_or_none(video.get('likeCount')),
141 'comment_count': int_or_none(video.get('commentCount')),
142 }
143
144 video_type = video.get('type')
145 if video_type == 'VOD':
146 inkey = self._call_api('video/v1.0/vod/%s/inkey', video_id)['inkey']
147 vod_id = video['vodId']
148 return merge_dicts(
149 get_common_fields(),
150 self._extract_video_info(video_id, vod_id, inkey))
151 elif video_type == 'LIVE':
152 status = video.get('status')
153 if status == 'ON_AIR':
154 stream_url = self._call_api(
155 'old/v3/live/%s/playInfo',
156 video_id)['result']['adaptiveStreamUrl']
157 formats = self._extract_m3u8_formats(stream_url, video_id, 'mp4')
158 info = get_common_fields()
159 info.update({
160 'title': self._live_title(video['title']),
161 'id': video_id,
162 'formats': formats,
163 'is_live': True,
164 })
165 return info
166 elif status == 'ENDED':
167 raise ExtractorError(
168 'Uploading for replay. Please wait...', expected=True)
169 elif status == 'RESERVED':
0536e60b 170 raise ExtractorError('Coming soon!', expected=True)
38d70284 171 elif video.get('exposeStatus') == 'CANCEL':
172 raise ExtractorError(
173 'We are sorry, but the live broadcast has been canceled.',
174 expected=True)
0536e60b 175 else:
38d70284 176 raise ExtractorError('Unknown status ' + status)
57774807 177
57774807 178
38d70284 179class VLivePostIE(VLiveIE):
180 IE_NAME = 'vlive:post'
181 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/post/(?P<id>\d-\d+)'
182 _TESTS = [{
183 # uploadType = SOS
184 'url': 'https://www.vlive.tv/post/1-20088044',
185 'info_dict': {
186 'id': '1-20088044',
187 'title': 'Hola estrellitas la tierra les dice hola (si era así no?) Ha...',
188 'description': 'md5:fab8a1e50e6e51608907f46c7fa4b407',
189 },
190 'playlist_count': 3,
191 }, {
192 # uploadType = V
193 'url': 'https://www.vlive.tv/post/1-20087926',
194 'info_dict': {
195 'id': '1-20087926',
196 'title': 'James Corden: And so, the baby becamos the Papa💜😭💪😭',
197 },
198 'playlist_count': 1,
199 }]
200 _FVIDEO_TMPL = 'fvideo/v1.0/fvideo-%%s/%s'
201 _SOS_TMPL = _FVIDEO_TMPL % 'sosPlayInfo'
202 _INKEY_TMPL = _FVIDEO_TMPL % 'inKey'
d3260f40 203
38d70284 204 def _real_extract(self, url):
205 post_id = self._match_id(url)
d3260f40 206
38d70284 207 post = self._call_api(
208 'post/v1.0/post-%s', post_id,
209 'attachments{video},officialVideo{videoSeq},plainBody,title')
d3260f40 210
38d70284 211 video_seq = str_or_none(try_get(
212 post, lambda x: x['officialVideo']['videoSeq']))
213 if video_seq:
214 return self.url_result(
215 'http://www.vlive.tv/video/' + video_seq,
216 VLiveIE.ie_key(), video_seq)
d3260f40 217
38d70284 218 title = post['title']
219 entries = []
220 for idx, video in enumerate(post['attachments']['video'].values()):
221 video_id = video.get('videoId')
222 if not video_id:
223 continue
224 upload_type = video.get('uploadType')
225 upload_info = video.get('uploadInfo') or {}
226 entry = None
227 if upload_type == 'SOS':
228 download = self._call_api(
229 self._SOS_TMPL, video_id)['videoUrl']['download']
230 formats = []
231 for f_id, f_url in download.items():
232 formats.append({
233 'format_id': f_id,
234 'url': f_url,
235 'height': int_or_none(f_id[:-1]),
236 })
237 self._sort_formats(formats)
238 entry = {
239 'formats': formats,
240 'id': video_id,
241 'thumbnail': upload_info.get('imageUrl'),
242 }
243 elif upload_type == 'V':
244 vod_id = upload_info.get('videoId')
245 if not vod_id:
246 continue
247 inkey = self._call_api(self._INKEY_TMPL, video_id)['inKey']
248 entry = self._extract_video_info(video_id, vod_id, inkey)
249 if entry:
250 entry['title'] = '%s_part%s' % (title, idx)
251 entries.append(entry)
252 return self.playlist_result(
253 entries, post_id, title, strip_or_none(post.get('plainBody')))
d3260f40 254
255
38d70284 256class VLiveChannelIE(VLiveBaseIE):
b92d3c53 257 IE_NAME = 'vlive:channel'
38d70284 258 _VALID_URL = r'https?://(?:channels\.vlive\.tv|(?:(?:www|m)\.)?vlive\.tv/channel)/(?P<id>[0-9A-Z]+)'
1923b146 259 _TESTS = [{
38d70284 260 'url': 'http://channels.vlive.tv/FCD4B',
1923b146 261 'info_dict': {
262 'id': 'FCD4B',
263 'title': 'MAMAMOO',
264 },
265 'playlist_mincount': 110
266 }, {
267 'url': 'https://www.vlive.tv/channel/FCD4B',
38d70284 268 'only_matching': True,
1923b146 269 }]
38d70284 270
271 def _call_api(self, path, channel_key_suffix, channel_value, note, query):
272 q = {
273 'app_id': self._APP_ID,
274 'channel' + channel_key_suffix: channel_value,
275 }
276 q.update(query)
277 return self._download_json(
278 'http://api.vfan.vlive.tv/vproxy/channelplus/' + path,
279 channel_value, note='Downloading ' + note, query=q)['result']
b92d3c53 280
281 def _real_extract(self, url):
282 channel_code = self._match_id(url)
283
38d70284 284 channel_seq = self._call_api(
285 'decodeChannelCode', 'Code', channel_code,
286 'decode channel code', {})['channelSeq']
d3260f40 287
b92d3c53 288 channel_name = None
289 entries = []
290
291 for page_num in itertools.count(1):
38d70284 292 video_list = self._call_api(
293 'getChannelVideoList', 'Seq', channel_seq,
294 'channel list page #%d' % page_num, {
f172c86d
S
295 # Large values of maxNumOfRows (~300 or above) may cause
296 # empty responses (see [1]), e.g. this happens for [2] that
297 # has more than 300 videos.
067aa17e 298 # 1. https://github.com/ytdl-org/youtube-dl/issues/13830
f172c86d
S
299 # 2. http://channels.vlive.tv/EDBF.
300 'maxNumOfRows': 100,
b92d3c53 301 'pageNo': page_num
302 }
303 )
b92d3c53 304
661cc229
S
305 if not channel_name:
306 channel_name = try_get(
307 video_list,
38d70284 308 lambda x: x['channelInfo']['channelName'],
661cc229
S
309 compat_str)
310
311 videos = try_get(
38d70284 312 video_list, lambda x: x['videoList'], list)
661cc229 313 if not videos:
b92d3c53 314 break
315
661cc229
S
316 for video in videos:
317 video_id = video.get('videoSeq')
d02f1210
KYK
318 video_type = video.get('videoType')
319
320 if not video_id or not video_type:
661cc229
S
321 continue
322 video_id = compat_str(video_id)
d02f1210
KYK
323
324 if video_type in ('PLAYLIST'):
325 playlist_videos = try_get(
326 video,
327 lambda x: x['videoPlaylist']['videoList'], list)
328 if not playlist_videos:
329 continue
330
331 for playlist_video in playlist_videos:
332 playlist_video_id = playlist_video.get('videoSeq')
333 if not playlist_video_id:
334 continue
335 playlist_video_id = compat_str(playlist_video_id)
336
337 entries.append(
338 self.url_result(
339 'http://www.vlive.tv/video/%s' % playlist_video_id,
340 ie=VLiveIE.ie_key(), video_id=playlist_video_id))
341 else:
342 entries.append(
343 self.url_result(
344 'http://www.vlive.tv/video/%s' % video_id,
345 ie=VLiveIE.ie_key(), video_id=video_id))
b92d3c53 346
347 return self.playlist_result(
348 entries, channel_code, channel_name)