]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/vlive.py
Update upstream source from tag 'upstream/2020.05.08'
[youtubedl] / youtube_dl / extractor / vlive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import time
6 import itertools
7
8 from .common import InfoExtractor
9 from .naver import NaverBaseIE
10 from ..compat import compat_str
11 from ..utils import (
12 ExtractorError,
13 merge_dicts,
14 remove_start,
15 try_get,
16 urlencode_postdata,
17 )
18
19
20 class VLiveIE(NaverBaseIE):
21 IE_NAME = 'vlive'
22 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<id>[0-9]+)'
23 _NETRC_MACHINE = 'vlive'
24 _TESTS = [{
25 'url': 'http://www.vlive.tv/video/1326',
26 'md5': 'cc7314812855ce56de70a06a27314983',
27 'info_dict': {
28 'id': '1326',
29 'ext': 'mp4',
30 'title': "[V LIVE] Girl's Day's Broadcast",
31 'creator': "Girl's Day",
32 'view_count': int,
33 'uploader_id': 'muploader_a',
34 },
35 }, {
36 'url': 'http://www.vlive.tv/video/16937',
37 'info_dict': {
38 'id': '16937',
39 'ext': 'mp4',
40 'title': '[V LIVE] 첸백시 걍방',
41 'creator': 'EXO',
42 'view_count': int,
43 'subtitles': 'mincount:12',
44 'uploader_id': 'muploader_j',
45 },
46 'params': {
47 'skip_download': True,
48 },
49 }, {
50 'url': 'https://www.vlive.tv/video/129100',
51 'md5': 'ca2569453b79d66e5b919e5d308bff6b',
52 'info_dict': {
53 'id': '129100',
54 'ext': 'mp4',
55 'title': '[V LIVE] [BTS+] Run BTS! 2019 - EP.71 :: Behind the scene',
56 'creator': 'BTS+',
57 'view_count': int,
58 'subtitles': 'mincount:10',
59 },
60 'skip': 'This video is only available for CH+ subscribers',
61 }]
62
63 @classmethod
64 def suitable(cls, url):
65 return False if VLivePlaylistIE.suitable(url) else super(VLiveIE, cls).suitable(url)
66
67 def _real_initialize(self):
68 self._login()
69
70 def _login(self):
71 email, password = self._get_login_info()
72 if None in (email, password):
73 return
74
75 def is_logged_in():
76 login_info = self._download_json(
77 'https://www.vlive.tv/auth/loginInfo', None,
78 note='Downloading login info',
79 headers={'Referer': 'https://www.vlive.tv/home'})
80 return try_get(
81 login_info, lambda x: x['message']['login'], bool) or False
82
83 LOGIN_URL = 'https://www.vlive.tv/auth/email/login'
84 self._request_webpage(
85 LOGIN_URL, None, note='Downloading login cookies')
86
87 self._download_webpage(
88 LOGIN_URL, None, note='Logging in',
89 data=urlencode_postdata({'email': email, 'pwd': password}),
90 headers={
91 'Referer': LOGIN_URL,
92 'Content-Type': 'application/x-www-form-urlencoded'
93 })
94
95 if not is_logged_in():
96 raise ExtractorError('Unable to log in', expected=True)
97
98 def _real_extract(self, url):
99 video_id = self._match_id(url)
100
101 webpage = self._download_webpage(
102 'https://www.vlive.tv/video/%s' % video_id, video_id)
103
104 VIDEO_PARAMS_RE = r'\bvlive\.video\.init\(([^)]+)'
105 VIDEO_PARAMS_FIELD = 'video params'
106
107 params = self._parse_json(self._search_regex(
108 VIDEO_PARAMS_RE, webpage, VIDEO_PARAMS_FIELD, default=''), video_id,
109 transform_source=lambda s: '[' + s + ']', fatal=False)
110
111 if not params or len(params) < 7:
112 params = self._search_regex(
113 VIDEO_PARAMS_RE, webpage, VIDEO_PARAMS_FIELD)
114 params = [p.strip(r'"') for p in re.split(r'\s*,\s*', params)]
115
116 status, long_video_id, key = params[2], params[5], params[6]
117 status = remove_start(status, 'PRODUCT_')
118
119 if status in ('LIVE_ON_AIR', 'BIG_EVENT_ON_AIR'):
120 return self._live(video_id, webpage)
121 elif status in ('VOD_ON_AIR', 'BIG_EVENT_INTRO'):
122 return self._replay(video_id, webpage, long_video_id, key)
123
124 if status == 'LIVE_END':
125 raise ExtractorError('Uploading for replay. Please wait...',
126 expected=True)
127 elif status == 'COMING_SOON':
128 raise ExtractorError('Coming soon!', expected=True)
129 elif status == 'CANCELED':
130 raise ExtractorError('We are sorry, '
131 'but the live broadcast has been canceled.',
132 expected=True)
133 elif status == 'ONLY_APP':
134 raise ExtractorError('Unsupported video type', expected=True)
135 else:
136 raise ExtractorError('Unknown status %s' % status)
137
138 def _get_common_fields(self, webpage):
139 title = self._og_search_title(webpage)
140 creator = self._html_search_regex(
141 r'<div[^>]+class="info_area"[^>]*>\s*(?:<em[^>]*>.*?</em\s*>\s*)?<a\s+[^>]*>([^<]+)',
142 webpage, 'creator', fatal=False)
143 thumbnail = self._og_search_thumbnail(webpage)
144 return {
145 'title': title,
146 'creator': creator,
147 'thumbnail': thumbnail,
148 }
149
150 def _live(self, video_id, webpage):
151 init_page = self._download_init_page(video_id)
152
153 live_params = self._search_regex(
154 r'"liveStreamInfo"\s*:\s*(".*"),',
155 init_page, 'live stream info')
156 live_params = self._parse_json(live_params, video_id)
157 live_params = self._parse_json(live_params, video_id)
158
159 formats = []
160 for vid in live_params.get('resolutions', []):
161 formats.extend(self._extract_m3u8_formats(
162 vid['cdnUrl'], video_id, 'mp4',
163 m3u8_id=vid.get('name'),
164 fatal=False, live=True))
165 self._sort_formats(formats)
166
167 info = self._get_common_fields(webpage)
168 info.update({
169 'title': self._live_title(info['title']),
170 'id': video_id,
171 'formats': formats,
172 'is_live': True,
173 })
174 return info
175
176 def _replay(self, video_id, webpage, long_video_id, key):
177 if '' in (long_video_id, key):
178 init_page = self._download_init_page(video_id)
179 video_info = self._parse_json(self._search_regex(
180 (r'(?s)oVideoStatus\s*=\s*({.+?})\s*</script',
181 r'(?s)oVideoStatus\s*=\s*({.+})'), init_page, 'video info'),
182 video_id)
183 if video_info.get('status') == 'NEED_CHANNEL_PLUS':
184 self.raise_login_required(
185 'This video is only available for CH+ subscribers')
186 long_video_id, key = video_info['vid'], video_info['inkey']
187
188 return merge_dicts(
189 self._get_common_fields(webpage),
190 self._extract_video_info(video_id, long_video_id, key))
191
192 def _download_init_page(self, video_id):
193 return self._download_webpage(
194 'https://www.vlive.tv/video/init/view',
195 video_id, note='Downloading live webpage',
196 data=urlencode_postdata({'videoSeq': video_id}),
197 headers={
198 'Referer': 'https://www.vlive.tv/video/%s' % video_id,
199 'Content-Type': 'application/x-www-form-urlencoded'
200 })
201
202
203 class VLiveChannelIE(InfoExtractor):
204 IE_NAME = 'vlive:channel'
205 _VALID_URL = r'https?://channels\.vlive\.tv/(?P<id>[0-9A-Z]+)'
206 _TEST = {
207 'url': 'http://channels.vlive.tv/FCD4B',
208 'info_dict': {
209 'id': 'FCD4B',
210 'title': 'MAMAMOO',
211 },
212 'playlist_mincount': 110
213 }
214 _APP_ID = '8c6cc7b45d2568fb668be6e05b6e5a3b'
215
216 def _real_extract(self, url):
217 channel_code = self._match_id(url)
218
219 webpage = self._download_webpage(
220 'http://channels.vlive.tv/%s/video' % channel_code, channel_code)
221
222 app_id = None
223
224 app_js_url = self._search_regex(
225 r'<script[^>]+src=(["\'])(?P<url>http.+?/app\.js.*?)\1',
226 webpage, 'app js', default=None, group='url')
227
228 if app_js_url:
229 app_js = self._download_webpage(
230 app_js_url, channel_code, 'Downloading app JS', fatal=False)
231 if app_js:
232 app_id = self._search_regex(
233 r'Global\.VFAN_APP_ID\s*=\s*[\'"]([^\'"]+)[\'"]',
234 app_js, 'app id', default=None)
235
236 app_id = app_id or self._APP_ID
237
238 channel_info = self._download_json(
239 'http://api.vfan.vlive.tv/vproxy/channelplus/decodeChannelCode',
240 channel_code, note='Downloading decode channel code',
241 query={
242 'app_id': app_id,
243 'channelCode': channel_code,
244 '_': int(time.time())
245 })
246
247 channel_seq = channel_info['result']['channelSeq']
248 channel_name = None
249 entries = []
250
251 for page_num in itertools.count(1):
252 video_list = self._download_json(
253 'http://api.vfan.vlive.tv/vproxy/channelplus/getChannelVideoList',
254 channel_code, note='Downloading channel list page #%d' % page_num,
255 query={
256 'app_id': app_id,
257 'channelSeq': channel_seq,
258 # Large values of maxNumOfRows (~300 or above) may cause
259 # empty responses (see [1]), e.g. this happens for [2] that
260 # has more than 300 videos.
261 # 1. https://github.com/ytdl-org/youtube-dl/issues/13830
262 # 2. http://channels.vlive.tv/EDBF.
263 'maxNumOfRows': 100,
264 '_': int(time.time()),
265 'pageNo': page_num
266 }
267 )
268
269 if not channel_name:
270 channel_name = try_get(
271 video_list,
272 lambda x: x['result']['channelInfo']['channelName'],
273 compat_str)
274
275 videos = try_get(
276 video_list, lambda x: x['result']['videoList'], list)
277 if not videos:
278 break
279
280 for video in videos:
281 video_id = video.get('videoSeq')
282 if not video_id:
283 continue
284 video_id = compat_str(video_id)
285 entries.append(
286 self.url_result(
287 'http://www.vlive.tv/video/%s' % video_id,
288 ie=VLiveIE.ie_key(), video_id=video_id))
289
290 return self.playlist_result(
291 entries, channel_code, channel_name)
292
293
294 class VLivePlaylistIE(InfoExtractor):
295 IE_NAME = 'vlive:playlist'
296 _VALID_URL = r'https?://(?:(?:www|m)\.)?vlive\.tv/video/(?P<video_id>[0-9]+)/playlist/(?P<id>[0-9]+)'
297 _VIDEO_URL_TEMPLATE = 'http://www.vlive.tv/video/%s'
298 _TESTS = [{
299 # regular working playlist
300 'url': 'https://www.vlive.tv/video/117956/playlist/117963',
301 'info_dict': {
302 'id': '117963',
303 'title': '아이돌룸(IDOL ROOM) 41회 - (여자)아이들'
304 },
305 'playlist_mincount': 10
306 }, {
307 # playlist with no playlistVideoSeqs
308 'url': 'http://www.vlive.tv/video/22867/playlist/22912',
309 'info_dict': {
310 'id': '22867',
311 'ext': 'mp4',
312 'title': '[V LIVE] Valentine Day Message from MINA',
313 'creator': 'TWICE',
314 'view_count': int
315 },
316 'params': {
317 'skip_download': True,
318 }
319 }]
320
321 def _build_video_result(self, video_id, message):
322 self.to_screen(message)
323 return self.url_result(
324 self._VIDEO_URL_TEMPLATE % video_id,
325 ie=VLiveIE.ie_key(), video_id=video_id)
326
327 def _real_extract(self, url):
328 mobj = re.match(self._VALID_URL, url)
329 video_id, playlist_id = mobj.group('video_id', 'id')
330
331 if self._downloader.params.get('noplaylist'):
332 return self._build_video_result(
333 video_id,
334 'Downloading just video %s because of --no-playlist'
335 % video_id)
336
337 self.to_screen(
338 'Downloading playlist %s - add --no-playlist to just download video'
339 % playlist_id)
340
341 webpage = self._download_webpage(
342 'http://www.vlive.tv/video/%s/playlist/%s'
343 % (video_id, playlist_id), playlist_id)
344
345 raw_item_ids = self._search_regex(
346 r'playlistVideoSeqs\s*=\s*(\[[^]]+\])', webpage,
347 'playlist video seqs', default=None, fatal=False)
348
349 if not raw_item_ids:
350 return self._build_video_result(
351 video_id,
352 'Downloading just video %s because no playlist was found'
353 % video_id)
354
355 item_ids = self._parse_json(raw_item_ids, playlist_id)
356
357 entries = [
358 self.url_result(
359 self._VIDEO_URL_TEMPLATE % item_id, ie=VLiveIE.ie_key(),
360 video_id=compat_str(item_id))
361 for item_id in item_ids]
362
363 playlist_name = self._html_search_regex(
364 r'<div[^>]+class="[^"]*multicam_playlist[^>]*>\s*<h3[^>]+>([^<]+)',
365 webpage, 'playlist title', fatal=False)
366
367 return self.playlist_result(entries, playlist_id, playlist_name)