[ie/dropbox] Fix password-protected video extraction (#11636)
[yt-dlp3.git] / yt_dlp / extractor / afreecatv.py
blob572d1a38936be242b441ba4c3a0cf20f27b8d85a
1 import functools
3 from .common import InfoExtractor
4 from ..networking import Request
5 from ..utils import (
6 ExtractorError,
7 OnDemandPagedList,
8 UserNotLive,
9 determine_ext,
10 filter_dict,
11 int_or_none,
12 orderedSet,
13 unified_timestamp,
14 url_or_none,
15 urlencode_postdata,
16 urljoin,
18 from ..utils.traversal import traverse_obj
21 class AfreecaTVBaseIE(InfoExtractor):
22 _NETRC_MACHINE = 'afreecatv'
24 def _perform_login(self, username, password):
25 login_form = {
26 'szWork': 'login',
27 'szType': 'json',
28 'szUid': username,
29 'szPassword': password,
30 'isSaveId': 'false',
31 'szScriptVar': 'oLoginRet',
32 'szAction': '',
35 response = self._download_json(
36 'https://login.sooplive.co.kr/app/LoginAction.php', None,
37 'Logging in', data=urlencode_postdata(login_form))
39 _ERRORS = {
40 -4: 'Your account has been suspended due to a violation of our terms and policies.',
41 -5: 'https://member.sooplive.co.kr/app/user_delete_progress.php',
42 -6: 'https://login.sooplive.co.kr/membership/changeMember.php',
43 -8: "Hello! Soop here.\nThe username you have entered belongs to \n an account that requires a legal guardian's consent. \nIf you wish to use our services without restriction, \nplease make sure to go through the necessary verification process.",
44 -9: 'https://member.sooplive.co.kr/app/pop_login_block.php',
45 -11: 'https://login.sooplive.co.kr/afreeca/second_login.php',
46 -12: 'https://member.sooplive.co.kr/app/user_security.php',
47 0: 'The username does not exist or you have entered the wrong password.',
48 -1: 'The username does not exist or you have entered the wrong password.',
49 -3: 'You have entered your username/password incorrectly.',
50 -7: 'You cannot use your Global Soop account to access Korean Soop.',
51 -10: 'Sorry for the inconvenience. \nYour account has been blocked due to an unauthorized access. \nPlease contact our Help Center for assistance.',
52 -32008: 'You have failed to log in. Please contact our Help Center.',
55 result = int_or_none(response.get('RESULT'))
56 if result != 1:
57 error = _ERRORS.get(result, 'You have failed to log in.')
58 raise ExtractorError(
59 f'Unable to login: {self.IE_NAME} said: {error}',
60 expected=True)
62 def _call_api(self, endpoint, display_id, data=None, headers=None, query=None):
63 return self._download_json(Request(
64 f'https://api.m.sooplive.co.kr/{endpoint}',
65 data=data, headers=headers, query=query,
66 extensions={'legacy_ssl': True}), display_id,
67 'Downloading API JSON', 'Unable to download API JSON')
69 @staticmethod
70 def _fixup_thumb(thumb_url):
71 if not url_or_none(thumb_url):
72 return None
73 # Core would determine_ext as 'php' from the url, so we need to provide the real ext
74 # See: https://github.com/yt-dlp/yt-dlp/issues/11537
75 return [{'url': thumb_url, 'ext': 'jpg'}]
78 class AfreecaTVIE(AfreecaTVBaseIE):
79 IE_NAME = 'soop'
80 IE_DESC = 'sooplive.co.kr'
81 _VALID_URL = r'https?://vod\.(?:sooplive\.co\.kr|afreecatv\.com)/(?:PLAYER/STATION|player)/(?P<id>\d+)/?(?:$|[?#&])'
82 _TESTS = [{
83 'url': 'https://vod.sooplive.co.kr/player/96753363',
84 'info_dict': {
85 'id': '20230108_9FF5BEE1_244432674_1',
86 'ext': 'mp4',
87 'uploader_id': 'rlantnghks',
88 'uploader': '페이즈으',
89 'duration': 10840,
90 'thumbnail': r're:https?://videoimg\.sooplive\.co/.kr/.+',
91 'upload_date': '20230108',
92 'timestamp': 1673218805,
93 'title': '젠지 페이즈',
95 'params': {
96 'skip_download': True,
98 }, {
99 # non standard key
100 'url': 'http://vod.sooplive.co.kr/PLAYER/STATION/20515605',
101 'info_dict': {
102 'id': '20170411_BE689A0E_190960999_1_2_h',
103 'ext': 'mp4',
104 'title': '혼자사는여자집',
105 'thumbnail': r're:https?://(?:video|st)img\.sooplive\.co\.kr/.+',
106 'uploader': '♥이슬이',
107 'uploader_id': 'dasl8121',
108 'upload_date': '20170411',
109 'timestamp': 1491929865,
110 'duration': 213,
112 'params': {
113 'skip_download': True,
115 }, {
116 # adult content
117 'url': 'https://vod.sooplive.co.kr/player/97267690',
118 'info_dict': {
119 'id': '20180327_27901457_202289533_1',
120 'ext': 'mp4',
121 'title': '[생]빨개요♥ (part 1)',
122 'thumbnail': r're:https?://(?:video|st)img\.sooplive\.co\.kr/.+',
123 'uploader': '[SA]서아',
124 'uploader_id': 'bjdyrksu',
125 'upload_date': '20180327',
126 'duration': 3601,
128 'params': {
129 'skip_download': True,
131 'skip': 'The VOD does not exist',
132 }, {
133 # adult content
134 'url': 'https://vod.sooplive.co.kr/player/70395877',
135 'only_matching': True,
136 }, {
137 # subscribers only
138 'url': 'https://vod.sooplive.co.kr/player/104647403',
139 'only_matching': True,
140 }, {
141 # private
142 'url': 'https://vod.sooplive.co.kr/player/81669846',
143 'only_matching': True,
146 def _real_extract(self, url):
147 video_id = self._match_id(url)
148 data = self._call_api(
149 'station/video/a/view', video_id, headers={'Referer': url},
150 data=urlencode_postdata({
151 'nTitleNo': video_id,
152 'nApiLevel': 10,
153 }))['data']
155 error_code = traverse_obj(data, ('code', {int}))
156 if error_code == -6221:
157 raise ExtractorError('The VOD does not exist', expected=True)
158 elif error_code == -6205:
159 raise ExtractorError('This VOD is private', expected=True)
161 common_info = traverse_obj(data, {
162 'title': ('title', {str}),
163 'uploader': ('writer_nick', {str}),
164 'uploader_id': ('bj_id', {str}),
165 'duration': ('total_file_duration', {int_or_none(scale=1000)}),
166 'thumbnails': ('thumb', {self._fixup_thumb}),
169 entries = []
170 for file_num, file_element in enumerate(
171 traverse_obj(data, ('files', lambda _, v: url_or_none(v['file']))), start=1):
172 file_url = file_element['file']
173 if determine_ext(file_url) == 'm3u8':
174 formats = self._extract_m3u8_formats(
175 file_url, video_id, 'mp4', m3u8_id='hls',
176 note=f'Downloading part {file_num} m3u8 information')
177 else:
178 formats = [{
179 'url': file_url,
180 'format_id': 'http',
183 entries.append({
184 **common_info,
185 'id': file_element.get('file_info_key') or f'{video_id}_{file_num}',
186 'title': f'{common_info.get("title") or "Untitled"} (part {file_num})',
187 'formats': formats,
188 **traverse_obj(file_element, {
189 'duration': ('duration', {int_or_none(scale=1000)}),
190 'timestamp': ('file_start', {unified_timestamp}),
194 if traverse_obj(data, ('adult_status', {str})) == 'notLogin':
195 if not entries:
196 self.raise_login_required(
197 'Only users older than 19 are able to watch this video', method='password')
198 self.report_warning(
199 'In accordance with local laws and regulations, underage users are '
200 'restricted from watching adult content. Only content suitable for all '
201 f'ages will be downloaded. {self._login_hint("password")}')
203 if not entries and traverse_obj(data, ('sub_upload_type', {str})):
204 self.raise_login_required('This VOD is for subscribers only', method='password')
206 if len(entries) == 1:
207 return {
208 **entries[0],
209 'title': common_info.get('title'),
212 common_info['timestamp'] = traverse_obj(entries, (..., 'timestamp'), get_all=False)
214 return self.playlist_result(entries, video_id, multi_video=True, **common_info)
217 class AfreecaTVCatchStoryIE(AfreecaTVBaseIE):
218 IE_NAME = 'soop:catchstory'
219 IE_DESC = 'sooplive.co.kr catch story'
220 _VALID_URL = r'https?://vod\.(?:sooplive\.co\.kr|afreecatv\.com)/player/(?P<id>\d+)/catchstory'
221 _TESTS = [{
222 'url': 'https://vod.sooplive.co.kr/player/103247/catchstory',
223 'info_dict': {
224 'id': '103247',
226 'playlist_count': 2,
229 def _real_extract(self, url):
230 video_id = self._match_id(url)
231 data = self._call_api(
232 'catchstory/a/view', video_id, headers={'Referer': url},
233 query={'aStoryListIdx': '', 'nStoryIdx': video_id})
235 return self.playlist_result(self._entries(data), video_id)
237 def _entries(self, data):
238 # 'files' is always a list with 1 element
239 yield from traverse_obj(data, (
240 'data', lambda _, v: v['story_type'] == 'catch',
241 'catch_list', lambda _, v: v['files'][0]['file'], {
242 'id': ('files', 0, 'file_info_key', {str}),
243 'url': ('files', 0, 'file', {url_or_none}),
244 'duration': ('files', 0, 'duration', {int_or_none(scale=1000)}),
245 'title': ('title', {str}),
246 'uploader': ('writer_nick', {str}),
247 'uploader_id': ('writer_id', {str}),
248 'thumbnails': ('thumb', {self._fixup_thumb}),
249 'timestamp': ('write_timestamp', {int_or_none}),
253 class AfreecaTVLiveIE(AfreecaTVBaseIE):
254 IE_NAME = 'soop:live'
255 IE_DESC = 'sooplive.co.kr livestreams'
256 _VALID_URL = r'https?://play\.(?:sooplive\.co\.kr|afreecatv\.com)/(?P<id>[^/?#]+)(?:/(?P<bno>\d+))?'
257 _TESTS = [{
258 'url': 'https://play.sooplive.co.kr/pyh3646/237852185',
259 'info_dict': {
260 'id': '237852185',
261 'ext': 'mp4',
262 'title': '【 우루과이 오늘은 무슨일이? 】',
263 'uploader': '박진우[JINU]',
264 'uploader_id': 'pyh3646',
265 'timestamp': 1640661495,
266 'is_live': True,
268 'skip': 'Livestream has ended',
269 }, {
270 'url': 'https://play.sooplive.co.kr/pyh3646/237852185',
271 'only_matching': True,
272 }, {
273 'url': 'https://play.sooplive.co.kr/pyh3646',
274 'only_matching': True,
277 _LIVE_API_URL = 'https://live.sooplive.co.kr/afreeca/player_live_api.php'
278 _WORKING_CDNS = [
279 'gcp_cdn', # live-global-cdn-v02.sooplive.co.kr
280 'gs_cdn_pc_app', # pc-app.stream.sooplive.co.kr
281 'gs_cdn_mobile_web', # mobile-web.stream.sooplive.co.kr
282 'gs_cdn_pc_web', # pc-web.stream.sooplive.co.kr
284 _BAD_CDNS = [
285 'gs_cdn', # chromecast.afreeca.gscdn.com (cannot resolve)
286 'gs_cdn_chromecast', # chromecast.stream.sooplive.co.kr (HTTP Error 400)
287 'azure_cdn', # live-global-cdn-v01.sooplive.co.kr (cannot resolve)
288 'aws_cf', # live-global-cdn-v03.sooplive.co.kr (cannot resolve)
289 'kt_cdn', # kt.stream.sooplive.co.kr (HTTP Error 400)
292 def _extract_formats(self, channel_info, broadcast_no, aid):
293 stream_base_url = channel_info.get('RMD') or 'https://livestream-manager.sooplive.co.kr'
295 # If user has not passed CDN IDs, try API-provided CDN ID followed by other working CDN IDs
296 default_cdn_ids = orderedSet([
297 *traverse_obj(channel_info, ('CDN', {str}, all, lambda _, v: v not in self._BAD_CDNS)),
298 *self._WORKING_CDNS,
300 cdn_ids = self._configuration_arg('cdn', default_cdn_ids)
302 for attempt, cdn_id in enumerate(cdn_ids, start=1):
303 m3u8_url = traverse_obj(self._download_json(
304 urljoin(stream_base_url, 'broad_stream_assign.html'), broadcast_no,
305 f'Downloading {cdn_id} stream info', f'Unable to download {cdn_id} stream info',
306 fatal=False, query={
307 'return_type': cdn_id,
308 'broad_key': f'{broadcast_no}-common-master-hls',
309 }), ('view_url', {url_or_none}))
310 try:
311 return self._extract_m3u8_formats(
312 m3u8_url, broadcast_no, 'mp4', m3u8_id='hls', query={'aid': aid},
313 headers={'Referer': 'https://play.sooplive.co.kr/'})
314 except ExtractorError as e:
315 if attempt == len(cdn_ids):
316 raise
317 self.report_warning(
318 f'{e.cause or e.msg}. Retrying... (attempt {attempt} of {len(cdn_ids)})')
320 def _real_extract(self, url):
321 broadcaster_id, broadcast_no = self._match_valid_url(url).group('id', 'bno')
322 channel_info = traverse_obj(self._download_json(
323 self._LIVE_API_URL, broadcaster_id, data=urlencode_postdata({'bid': broadcaster_id})),
324 ('CHANNEL', {dict})) or {}
326 broadcaster_id = channel_info.get('BJID') or broadcaster_id
327 broadcast_no = channel_info.get('BNO') or broadcast_no
328 if not broadcast_no:
329 result = channel_info.get('RESULT')
330 if result == 0:
331 raise UserNotLive(video_id=broadcaster_id)
332 elif result == -6:
333 self.raise_login_required(
334 'This channel is streaming for subscribers only', method='password')
335 raise ExtractorError('Unable to extract broadcast number')
337 password = self.get_param('videopassword')
338 if channel_info.get('BPWD') == 'Y' and password is None:
339 raise ExtractorError(
340 'This livestream is protected by a password, use the --video-password option',
341 expected=True)
343 token_info = traverse_obj(self._download_json(
344 self._LIVE_API_URL, broadcast_no, 'Downloading access token for stream',
345 'Unable to download access token for stream', data=urlencode_postdata(filter_dict({
346 'bno': broadcast_no,
347 'stream_type': 'common',
348 'type': 'aid',
349 'quality': 'master',
350 'pwd': password,
351 }))), ('CHANNEL', {dict})) or {}
352 aid = token_info.get('AID')
353 if not aid:
354 result = token_info.get('RESULT')
355 if result == 0:
356 raise ExtractorError('This livestream has ended', expected=True)
357 elif result == -6:
358 self.raise_login_required('This livestream is for subscribers only', method='password')
359 raise ExtractorError('Unable to extract access token')
361 formats = self._extract_formats(channel_info, broadcast_no, aid)
363 station_info = traverse_obj(self._download_json(
364 'https://st.sooplive.co.kr/api/get_station_status.php', broadcast_no,
365 'Downloading channel metadata', 'Unable to download channel metadata',
366 query={'szBjId': broadcaster_id}, fatal=False), {dict}) or {}
368 return {
369 'id': broadcast_no,
370 'title': channel_info.get('TITLE') or station_info.get('station_title'),
371 'uploader': channel_info.get('BJNICK') or station_info.get('station_name'),
372 'uploader_id': broadcaster_id,
373 'timestamp': unified_timestamp(station_info.get('broad_start')),
374 'formats': formats,
375 'is_live': True,
376 'http_headers': {'Referer': url},
380 class AfreecaTVUserIE(AfreecaTVBaseIE):
381 IE_NAME = 'soop:user'
382 _VALID_URL = r'https?://ch\.(?:sooplive\.co\.kr|afreecatv\.com)/(?P<id>[^/?#]+)/vods/?(?P<slug_type>[^/?#]+)?'
383 _TESTS = [{
384 'url': 'https://ch.sooplive.co.kr/ryuryu24/vods/review',
385 'info_dict': {
386 '_type': 'playlist',
387 'id': 'ryuryu24',
388 'title': 'ryuryu24 - review',
390 'playlist_count': 218,
391 }, {
392 'url': 'https://ch.sooplive.co.kr/parang1995/vods/highlight',
393 'info_dict': {
394 '_type': 'playlist',
395 'id': 'parang1995',
396 'title': 'parang1995 - highlight',
398 'playlist_count': 997,
399 }, {
400 'url': 'https://ch.sooplive.co.kr/ryuryu24/vods',
401 'info_dict': {
402 '_type': 'playlist',
403 'id': 'ryuryu24',
404 'title': 'ryuryu24 - all',
406 'playlist_count': 221,
407 }, {
408 'url': 'https://ch.sooplive.co.kr/ryuryu24/vods/balloonclip',
409 'info_dict': {
410 '_type': 'playlist',
411 'id': 'ryuryu24',
412 'title': 'ryuryu24 - balloonclip',
414 'playlist_count': 0,
416 _PER_PAGE = 60
418 def _fetch_page(self, user_id, user_type, page):
419 page += 1
420 info = self._download_json(f'https://chapi.sooplive.co.kr/api/{user_id}/vods/{user_type}', user_id,
421 query={'page': page, 'per_page': self._PER_PAGE, 'orderby': 'reg_date'},
422 note=f'Downloading {user_type} video page {page}')
423 for item in info['data']:
424 yield self.url_result(
425 f'https://vod.sooplive.co.kr/player/{item["title_no"]}/', AfreecaTVIE, item['title_no'])
427 def _real_extract(self, url):
428 user_id, user_type = self._match_valid_url(url).group('id', 'slug_type')
429 user_type = user_type or 'all'
430 entries = OnDemandPagedList(functools.partial(self._fetch_page, user_id, user_type), self._PER_PAGE)
431 return self.playlist_result(entries, user_id, f'{user_id} - {user_type}')