]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/afreecatv.py
7e628396fb0c44064c096f9ac31b8c517ef36af8
[yt-dlp.git] / yt_dlp / extractor / afreecatv.py
1 import functools
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 OnDemandPagedList,
7 UserNotLive,
8 determine_ext,
9 filter_dict,
10 int_or_none,
11 orderedSet,
12 unified_timestamp,
13 url_or_none,
14 urlencode_postdata,
15 urljoin,
16 )
17 from ..utils.traversal import traverse_obj
18
19
20 class AfreecaTVBaseIE(InfoExtractor):
21 _NETRC_MACHINE = 'afreecatv'
22
23 def _perform_login(self, username, password):
24 login_form = {
25 'szWork': 'login',
26 'szType': 'json',
27 'szUid': username,
28 'szPassword': password,
29 'isSaveId': 'false',
30 'szScriptVar': 'oLoginRet',
31 'szAction': '',
32 }
33
34 response = self._download_json(
35 'https://login.afreecatv.com/app/LoginAction.php', None,
36 'Logging in', data=urlencode_postdata(login_form))
37
38 _ERRORS = {
39 -4: 'Your account has been suspended due to a violation of our terms and policies.',
40 -5: 'https://member.afreecatv.com/app/user_delete_progress.php',
41 -6: 'https://login.afreecatv.com/membership/changeMember.php',
42 -8: "Hello! AfreecaTV 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.",
43 -9: 'https://member.afreecatv.com/app/pop_login_block.php',
44 -11: 'https://login.afreecatv.com/afreeca/second_login.php',
45 -12: 'https://member.afreecatv.com/app/user_security.php',
46 0: 'The username does not exist or you have entered the wrong password.',
47 -1: 'The username does not exist or you have entered the wrong password.',
48 -3: 'You have entered your username/password incorrectly.',
49 -7: 'You cannot use your Global AfreecaTV account to access Korean AfreecaTV.',
50 -10: 'Sorry for the inconvenience. \nYour account has been blocked due to an unauthorized access. \nPlease contact our Help Center for assistance.',
51 -32008: 'You have failed to log in. Please contact our Help Center.',
52 }
53
54 result = int_or_none(response.get('RESULT'))
55 if result != 1:
56 error = _ERRORS.get(result, 'You have failed to log in.')
57 raise ExtractorError(
58 f'Unable to login: {self.IE_NAME} said: {error}',
59 expected=True)
60
61
62 class AfreecaTVIE(AfreecaTVBaseIE):
63 IE_NAME = 'afreecatv'
64 IE_DESC = 'afreecatv.com'
65 _VALID_URL = r'''(?x)
66 https?://
67 (?:
68 (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
69 (?:
70 /app/(?:index|read_ucc_bbs)\.cgi|
71 /player/[Pp]layer\.(?:swf|html)
72 )\?.*?\bnTitleNo=|
73 vod\.afreecatv\.com/(PLAYER/STATION|player)/
74 )
75 (?P<id>\d+)
76 '''
77 _TESTS = [{
78 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
79 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
80 'info_dict': {
81 'id': '36164052',
82 'ext': 'mp4',
83 'title': '데일리 에이프릴 요정들의 시상식!',
84 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
85 'uploader': 'dailyapril',
86 'uploader_id': 'dailyapril',
87 'upload_date': '20160503',
88 },
89 'skip': 'Video is gone',
90 }, {
91 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
92 'info_dict': {
93 'id': '36153164',
94 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
95 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
96 'uploader': 'dailyapril',
97 'uploader_id': 'dailyapril',
98 },
99 'playlist_count': 2,
100 'playlist': [{
101 'md5': 'd8b7c174568da61d774ef0203159bf97',
102 'info_dict': {
103 'id': '36153164_1',
104 'ext': 'mp4',
105 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
106 'upload_date': '20160502',
107 },
108 }, {
109 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
110 'info_dict': {
111 'id': '36153164_2',
112 'ext': 'mp4',
113 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
114 'upload_date': '20160502',
115 },
116 }],
117 'skip': 'Video is gone',
118 }, {
119 # non standard key
120 'url': 'http://vod.afreecatv.com/PLAYER/STATION/20515605',
121 'info_dict': {
122 'id': '20170411_BE689A0E_190960999_1_2_h',
123 'ext': 'mp4',
124 'title': '혼자사는여자집',
125 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
126 'uploader': '♥이슬이',
127 'uploader_id': 'dasl8121',
128 'upload_date': '20170411',
129 'timestamp': 1491929865,
130 'duration': 213,
131 },
132 'params': {
133 'skip_download': True,
134 },
135 }, {
136 # adult content
137 'url': 'https://vod.afreecatv.com/player/97267690',
138 'info_dict': {
139 'id': '20180327_27901457_202289533_1',
140 'ext': 'mp4',
141 'title': '[생]빨개요♥ (part 1)',
142 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
143 'uploader': '[SA]서아',
144 'uploader_id': 'bjdyrksu',
145 'upload_date': '20180327',
146 'duration': 3601,
147 },
148 'params': {
149 'skip_download': True,
150 },
151 'skip': 'The VOD does not exist',
152 }, {
153 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
154 'only_matching': True,
155 }, {
156 'url': 'https://vod.afreecatv.com/player/96753363',
157 'info_dict': {
158 'id': '20230108_9FF5BEE1_244432674_1',
159 'ext': 'mp4',
160 'uploader_id': 'rlantnghks',
161 'uploader': '페이즈으',
162 'duration': 10840,
163 'thumbnail': r're:https?://videoimg\.afreecatv\.com/.+',
164 'upload_date': '20230108',
165 'timestamp': 1673218805,
166 'title': '젠지 페이즈',
167 },
168 'params': {
169 'skip_download': True,
170 },
171 }, {
172 # adult content
173 'url': 'https://vod.afreecatv.com/player/70395877',
174 'only_matching': True,
175 }, {
176 # subscribers only
177 'url': 'https://vod.afreecatv.com/player/104647403',
178 'only_matching': True,
179 }, {
180 # private
181 'url': 'https://vod.afreecatv.com/player/81669846',
182 'only_matching': True,
183 }]
184
185 def _real_extract(self, url):
186 video_id = self._match_id(url)
187 data = self._download_json(
188 'https://api.m.afreecatv.com/station/video/a/view', video_id,
189 headers={'Referer': url}, data=urlencode_postdata({
190 'nTitleNo': video_id,
191 'nApiLevel': 10,
192 }), impersonate=True)['data']
193
194 error_code = traverse_obj(data, ('code', {int}))
195 if error_code == -6221:
196 raise ExtractorError('The VOD does not exist', expected=True)
197 elif error_code == -6205:
198 raise ExtractorError('This VOD is private', expected=True)
199
200 common_info = traverse_obj(data, {
201 'title': ('title', {str}),
202 'uploader': ('writer_nick', {str}),
203 'uploader_id': ('bj_id', {str}),
204 'duration': ('total_file_duration', {functools.partial(int_or_none, scale=1000)}),
205 'thumbnail': ('thumb', {url_or_none}),
206 })
207
208 entries = []
209 for file_num, file_element in enumerate(
210 traverse_obj(data, ('files', lambda _, v: url_or_none(v['file']))), start=1):
211 file_url = file_element['file']
212 if determine_ext(file_url) == 'm3u8':
213 formats = self._extract_m3u8_formats(
214 file_url, video_id, 'mp4', m3u8_id='hls',
215 note=f'Downloading part {file_num} m3u8 information')
216 else:
217 formats = [{
218 'url': file_url,
219 'format_id': 'http',
220 }]
221
222 entries.append({
223 **common_info,
224 'id': file_element.get('file_info_key') or f'{video_id}_{file_num}',
225 'title': f'{common_info.get("title") or "Untitled"} (part {file_num})',
226 'formats': formats,
227 **traverse_obj(file_element, {
228 'duration': ('duration', {functools.partial(int_or_none, scale=1000)}),
229 'timestamp': ('file_start', {unified_timestamp}),
230 }),
231 })
232
233 if traverse_obj(data, ('adult_status', {str})) == 'notLogin':
234 if not entries:
235 self.raise_login_required(
236 'Only users older than 19 are able to watch this video', method='password')
237 self.report_warning(
238 'In accordance with local laws and regulations, underage users are '
239 'restricted from watching adult content. Only content suitable for all '
240 f'ages will be downloaded. {self._login_hint("password")}')
241
242 if not entries and traverse_obj(data, ('sub_upload_type', {str})):
243 self.raise_login_required('This VOD is for subscribers only', method='password')
244
245 if len(entries) == 1:
246 return {
247 **entries[0],
248 'title': common_info.get('title'),
249 }
250
251 common_info['timestamp'] = traverse_obj(entries, (..., 'timestamp'), get_all=False)
252
253 return self.playlist_result(entries, video_id, multi_video=True, **common_info)
254
255
256 class AfreecaTVLiveIE(AfreecaTVBaseIE):
257 IE_NAME = 'afreecatv:live'
258 IE_DESC = 'afreecatv.com livestreams'
259 _VALID_URL = r'https?://play\.afreeca(?:tv)?\.com/(?P<id>[^/]+)(?:/(?P<bno>\d+))?'
260 _TESTS = [{
261 'url': 'https://play.afreecatv.com/pyh3646/237852185',
262 'info_dict': {
263 'id': '237852185',
264 'ext': 'mp4',
265 'title': '【 우루과이 오늘은 무슨일이? 】',
266 'uploader': '박진우[JINU]',
267 'uploader_id': 'pyh3646',
268 'timestamp': 1640661495,
269 'is_live': True,
270 },
271 'skip': 'Livestream has ended',
272 }, {
273 'url': 'https://play.afreecatv.com/pyh3646/237852185',
274 'only_matching': True,
275 }, {
276 'url': 'https://play.afreecatv.com/pyh3646',
277 'only_matching': True,
278 }]
279
280 _LIVE_API_URL = 'https://live.afreecatv.com/afreeca/player_live_api.php'
281 _WORKING_CDNS = [
282 'gcp_cdn', # live-global-cdn-v02.afreecatv.com
283 'gs_cdn_pc_app', # pc-app.stream.afreecatv.com
284 'gs_cdn_mobile_web', # mobile-web.stream.afreecatv.com
285 'gs_cdn_pc_web', # pc-web.stream.afreecatv.com
286 ]
287 _BAD_CDNS = [
288 'gs_cdn', # chromecast.afreeca.gscdn.com (cannot resolve)
289 'gs_cdn_chromecast', # chromecast.stream.afreecatv.com (HTTP Error 400)
290 'azure_cdn', # live-global-cdn-v01.afreecatv.com (cannot resolve)
291 'aws_cf', # live-global-cdn-v03.afreecatv.com (cannot resolve)
292 'kt_cdn', # kt.stream.afreecatv.com (HTTP Error 400)
293 ]
294
295 def _extract_formats(self, channel_info, broadcast_no, aid):
296 stream_base_url = channel_info.get('RMD') or 'https://livestream-manager.afreecatv.com'
297
298 # If user has not passed CDN IDs, try API-provided CDN ID followed by other working CDN IDs
299 default_cdn_ids = orderedSet([
300 *traverse_obj(channel_info, ('CDN', {str}, all, lambda _, v: v not in self._BAD_CDNS)),
301 *self._WORKING_CDNS,
302 ])
303 cdn_ids = self._configuration_arg('cdn', default_cdn_ids)
304
305 for attempt, cdn_id in enumerate(cdn_ids, start=1):
306 m3u8_url = traverse_obj(self._download_json(
307 urljoin(stream_base_url, 'broad_stream_assign.html'), broadcast_no,
308 f'Downloading {cdn_id} stream info', f'Unable to download {cdn_id} stream info',
309 fatal=False, query={
310 'return_type': cdn_id,
311 'broad_key': f'{broadcast_no}-common-master-hls',
312 }), ('view_url', {url_or_none}))
313 try:
314 return self._extract_m3u8_formats(
315 m3u8_url, broadcast_no, 'mp4', m3u8_id='hls', query={'aid': aid},
316 headers={'Referer': 'https://play.afreecatv.com/'})
317 except ExtractorError as e:
318 if attempt == len(cdn_ids):
319 raise
320 self.report_warning(
321 f'{e.cause or e.msg}. Retrying... (attempt {attempt} of {len(cdn_ids)})')
322
323 def _real_extract(self, url):
324 broadcaster_id, broadcast_no = self._match_valid_url(url).group('id', 'bno')
325 channel_info = traverse_obj(self._download_json(
326 self._LIVE_API_URL, broadcaster_id, data=urlencode_postdata({'bid': broadcaster_id})),
327 ('CHANNEL', {dict})) or {}
328
329 broadcaster_id = channel_info.get('BJID') or broadcaster_id
330 broadcast_no = channel_info.get('BNO') or broadcast_no
331 if not broadcast_no:
332 raise UserNotLive(video_id=broadcaster_id)
333
334 password = self.get_param('videopassword')
335 if channel_info.get('BPWD') == 'Y' and password is None:
336 raise ExtractorError(
337 'This livestream is protected by a password, use the --video-password option',
338 expected=True)
339
340 token_info = traverse_obj(self._download_json(
341 self._LIVE_API_URL, broadcast_no, 'Downloading access token for stream',
342 'Unable to download access token for stream', data=urlencode_postdata(filter_dict({
343 'bno': broadcast_no,
344 'stream_type': 'common',
345 'type': 'aid',
346 'quality': 'master',
347 'pwd': password,
348 }))), ('CHANNEL', {dict})) or {}
349 aid = token_info.get('AID')
350 if not aid:
351 result = token_info.get('RESULT')
352 if result == 0:
353 raise ExtractorError('This livestream has ended', expected=True)
354 elif result == -6:
355 self.raise_login_required('This livestream is for subscribers only', method='password')
356 raise ExtractorError('Unable to extract access token')
357
358 formats = self._extract_formats(channel_info, broadcast_no, aid)
359
360 station_info = traverse_obj(self._download_json(
361 'https://st.afreecatv.com/api/get_station_status.php', broadcast_no,
362 'Downloading channel metadata', 'Unable to download channel metadata',
363 query={'szBjId': broadcaster_id}, fatal=False), {dict}) or {}
364
365 return {
366 'id': broadcast_no,
367 'title': channel_info.get('TITLE') or station_info.get('station_title'),
368 'uploader': channel_info.get('BJNICK') or station_info.get('station_name'),
369 'uploader_id': broadcaster_id,
370 'timestamp': unified_timestamp(station_info.get('broad_start')),
371 'formats': formats,
372 'is_live': True,
373 'http_headers': {'Referer': url},
374 }
375
376
377 class AfreecaTVUserIE(InfoExtractor):
378 IE_NAME = 'afreecatv:user'
379 _VALID_URL = r'https?://bj\.afreeca(?:tv)?\.com/(?P<id>[^/]+)/vods/?(?P<slug_type>[^/]+)?'
380 _TESTS = [{
381 'url': 'https://bj.afreecatv.com/ryuryu24/vods/review',
382 'info_dict': {
383 '_type': 'playlist',
384 'id': 'ryuryu24',
385 'title': 'ryuryu24 - review',
386 },
387 'playlist_count': 218,
388 }, {
389 'url': 'https://bj.afreecatv.com/parang1995/vods/highlight',
390 'info_dict': {
391 '_type': 'playlist',
392 'id': 'parang1995',
393 'title': 'parang1995 - highlight',
394 },
395 'playlist_count': 997,
396 }, {
397 'url': 'https://bj.afreecatv.com/ryuryu24/vods',
398 'info_dict': {
399 '_type': 'playlist',
400 'id': 'ryuryu24',
401 'title': 'ryuryu24 - all',
402 },
403 'playlist_count': 221,
404 }, {
405 'url': 'https://bj.afreecatv.com/ryuryu24/vods/balloonclip',
406 'info_dict': {
407 '_type': 'playlist',
408 'id': 'ryuryu24',
409 'title': 'ryuryu24 - balloonclip',
410 },
411 'playlist_count': 0,
412 }]
413 _PER_PAGE = 60
414
415 def _fetch_page(self, user_id, user_type, page):
416 page += 1
417 info = self._download_json(f'https://bjapi.afreecatv.com/api/{user_id}/vods/{user_type}', user_id,
418 query={'page': page, 'per_page': self._PER_PAGE, 'orderby': 'reg_date'},
419 note=f'Downloading {user_type} video page {page}')
420 for item in info['data']:
421 yield self.url_result(
422 f'https://vod.afreecatv.com/player/{item["title_no"]}/', AfreecaTVIE, item['title_no'])
423
424 def _real_extract(self, url):
425 user_id, user_type = self._match_valid_url(url).group('id', 'slug_type')
426 user_type = user_type or 'all'
427 entries = OnDemandPagedList(functools.partial(self._fetch_page, user_id, user_type), self._PER_PAGE)
428 return self.playlist_result(entries, user_id, f'{user_id} - {user_type}')