]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/afreecatv.py
[extractor/huya] Fix stream extraction (#4798)
[yt-dlp.git] / yt_dlp / extractor / afreecatv.py
1 import functools
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 OnDemandPagedList,
8 date_from_str,
9 determine_ext,
10 int_or_none,
11 qualities,
12 traverse_obj,
13 unified_strdate,
14 unified_timestamp,
15 update_url_query,
16 url_or_none,
17 urlencode_postdata,
18 xpath_text,
19 )
20
21
22 class AfreecaTVIE(InfoExtractor):
23 IE_NAME = 'afreecatv'
24 IE_DESC = 'afreecatv.com'
25 _VALID_URL = r'''(?x)
26 https?://
27 (?:
28 (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
29 (?:
30 /app/(?:index|read_ucc_bbs)\.cgi|
31 /player/[Pp]layer\.(?:swf|html)
32 )\?.*?\bnTitleNo=|
33 vod\.afreecatv\.com/(PLAYER/STATION|player)/
34 )
35 (?P<id>\d+)
36 '''
37 _NETRC_MACHINE = 'afreecatv'
38 _TESTS = [{
39 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
40 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
41 'info_dict': {
42 'id': '36164052',
43 'ext': 'mp4',
44 'title': '데일리 에이프릴 요정들의 시상식!',
45 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
46 'uploader': 'dailyapril',
47 'uploader_id': 'dailyapril',
48 'upload_date': '20160503',
49 },
50 'skip': 'Video is gone',
51 }, {
52 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
53 'info_dict': {
54 'id': '36153164',
55 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
56 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
57 'uploader': 'dailyapril',
58 'uploader_id': 'dailyapril',
59 },
60 'playlist_count': 2,
61 'playlist': [{
62 'md5': 'd8b7c174568da61d774ef0203159bf97',
63 'info_dict': {
64 'id': '36153164_1',
65 'ext': 'mp4',
66 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
67 'upload_date': '20160502',
68 },
69 }, {
70 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
71 'info_dict': {
72 'id': '36153164_2',
73 'ext': 'mp4',
74 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
75 'upload_date': '20160502',
76 },
77 }],
78 'skip': 'Video is gone',
79 }, {
80 'url': 'http://vod.afreecatv.com/PLAYER/STATION/18650793',
81 'info_dict': {
82 'id': '18650793',
83 'ext': 'mp4',
84 'title': '오늘은 다르다! 쏘님의 우월한 위아래~ 댄스리액션!',
85 'thumbnail': r're:^https?://.*\.jpg$',
86 'uploader': '윈아디',
87 'uploader_id': 'badkids',
88 'duration': 107,
89 },
90 'params': {
91 'skip_download': True,
92 },
93 }, {
94 'url': 'http://vod.afreecatv.com/PLAYER/STATION/10481652',
95 'info_dict': {
96 'id': '10481652',
97 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
98 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
99 'uploader': 'dailyapril',
100 'uploader_id': 'dailyapril',
101 'duration': 6492,
102 },
103 'playlist_count': 2,
104 'playlist': [{
105 'md5': 'd8b7c174568da61d774ef0203159bf97',
106 'info_dict': {
107 'id': '20160502_c4c62b9d_174361386_1',
108 'ext': 'mp4',
109 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 1)",
110 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
111 'uploader': 'dailyapril',
112 'uploader_id': 'dailyapril',
113 'upload_date': '20160502',
114 'duration': 3601,
115 },
116 }, {
117 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
118 'info_dict': {
119 'id': '20160502_39e739bb_174361386_2',
120 'ext': 'mp4',
121 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 2)",
122 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
123 'uploader': 'dailyapril',
124 'uploader_id': 'dailyapril',
125 'upload_date': '20160502',
126 'duration': 2891,
127 },
128 }],
129 'params': {
130 'skip_download': True,
131 },
132 }, {
133 # non standard key
134 'url': 'http://vod.afreecatv.com/PLAYER/STATION/20515605',
135 'info_dict': {
136 'id': '20170411_BE689A0E_190960999_1_2_h',
137 'ext': 'mp4',
138 'title': '혼자사는여자집',
139 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
140 'uploader': '♥이슬이',
141 'uploader_id': 'dasl8121',
142 'upload_date': '20170411',
143 'duration': 213,
144 },
145 'params': {
146 'skip_download': True,
147 },
148 }, {
149 # PARTIAL_ADULT
150 'url': 'http://vod.afreecatv.com/PLAYER/STATION/32028439',
151 'info_dict': {
152 'id': '20180327_27901457_202289533_1',
153 'ext': 'mp4',
154 'title': '[생]빨개요♥ (part 1)',
155 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
156 'uploader': '[SA]서아',
157 'uploader_id': 'bjdyrksu',
158 'upload_date': '20180327',
159 'duration': 3601,
160 },
161 'params': {
162 'skip_download': True,
163 },
164 'expected_warnings': ['adult content'],
165 }, {
166 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
167 'only_matching': True,
168 }, {
169 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030',
170 'only_matching': True,
171 }, {
172 'url': 'http://vod.afreecatv.com/player/15055030',
173 'only_matching': True,
174 }]
175
176 @staticmethod
177 def parse_video_key(key):
178 video_key = {}
179 m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
180 if m:
181 video_key['upload_date'] = m.group('upload_date')
182 video_key['part'] = int(m.group('part'))
183 return video_key
184
185 def _perform_login(self, username, password):
186 login_form = {
187 'szWork': 'login',
188 'szType': 'json',
189 'szUid': username,
190 'szPassword': password,
191 'isSaveId': 'false',
192 'szScriptVar': 'oLoginRet',
193 'szAction': '',
194 }
195
196 response = self._download_json(
197 'https://login.afreecatv.com/app/LoginAction.php', None,
198 'Logging in', data=urlencode_postdata(login_form))
199
200 _ERRORS = {
201 -4: 'Your account has been suspended due to a violation of our terms and policies.',
202 -5: 'https://member.afreecatv.com/app/user_delete_progress.php',
203 -6: 'https://login.afreecatv.com/membership/changeMember.php',
204 -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.",
205 -9: 'https://member.afreecatv.com/app/pop_login_block.php',
206 -11: 'https://login.afreecatv.com/afreeca/second_login.php',
207 -12: 'https://member.afreecatv.com/app/user_security.php',
208 0: 'The username does not exist or you have entered the wrong password.',
209 -1: 'The username does not exist or you have entered the wrong password.',
210 -3: 'You have entered your username/password incorrectly.',
211 -7: 'You cannot use your Global AfreecaTV account to access Korean AfreecaTV.',
212 -10: 'Sorry for the inconvenience. \nYour account has been blocked due to an unauthorized access. \nPlease contact our Help Center for assistance.',
213 -32008: 'You have failed to log in. Please contact our Help Center.',
214 }
215
216 result = int_or_none(response.get('RESULT'))
217 if result != 1:
218 error = _ERRORS.get(result, 'You have failed to log in.')
219 raise ExtractorError(
220 'Unable to login: %s said: %s' % (self.IE_NAME, error),
221 expected=True)
222
223 def _real_extract(self, url):
224 video_id = self._match_id(url)
225
226 webpage = self._download_webpage(url, video_id)
227
228 if re.search(r'alert\(["\']This video has been deleted', webpage):
229 raise ExtractorError(
230 'Video %s has been deleted' % video_id, expected=True)
231
232 station_id = self._search_regex(
233 r'nStationNo\s*=\s*(\d+)', webpage, 'station')
234 bbs_id = self._search_regex(
235 r'nBbsNo\s*=\s*(\d+)', webpage, 'bbs')
236 video_id = self._search_regex(
237 r'nTitleNo\s*=\s*(\d+)', webpage, 'title', default=video_id)
238
239 partial_view = False
240 adult_view = False
241 for _ in range(2):
242 query = {
243 'nTitleNo': video_id,
244 'nStationNo': station_id,
245 'nBbsNo': bbs_id,
246 }
247 if partial_view:
248 query['partialView'] = 'SKIP_ADULT'
249 if adult_view:
250 query['adultView'] = 'ADULT_VIEW'
251 video_xml = self._download_xml(
252 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
253 video_id, 'Downloading video info XML%s'
254 % (' (skipping adult)' if partial_view else ''),
255 video_id, headers={
256 'Referer': url,
257 }, query=query)
258
259 flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
260 if flag and flag == 'SUCCEED':
261 break
262 if flag == 'PARTIAL_ADULT':
263 self.report_warning(
264 'In accordance with local laws and regulations, underage users are restricted from watching adult content. '
265 'Only content suitable for all ages will be downloaded. '
266 'Provide account credentials if you wish to download restricted content.')
267 partial_view = True
268 continue
269 elif flag == 'ADULT':
270 if not adult_view:
271 adult_view = True
272 continue
273 error = 'Only users older than 19 are able to watch this video. Provide account credentials to download this content.'
274 else:
275 error = flag
276 raise ExtractorError(
277 '%s said: %s' % (self.IE_NAME, error), expected=True)
278 else:
279 raise ExtractorError('Unable to download video info')
280
281 video_element = video_xml.findall('./track/video')[-1]
282 if video_element is None or video_element.text is None:
283 raise ExtractorError(
284 'Video %s does not exist' % video_id, expected=True)
285
286 video_url = video_element.text.strip()
287
288 title = xpath_text(video_xml, './track/title', 'title', fatal=True)
289
290 uploader = xpath_text(video_xml, './track/nickname', 'uploader')
291 uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
292 duration = int_or_none(xpath_text(
293 video_xml, './track/duration', 'duration'))
294 thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
295
296 common_entry = {
297 'uploader': uploader,
298 'uploader_id': uploader_id,
299 'thumbnail': thumbnail,
300 }
301
302 info = common_entry.copy()
303 info.update({
304 'id': video_id,
305 'title': title,
306 'duration': duration,
307 })
308
309 if not video_url:
310 entries = []
311 file_elements = video_element.findall('./file')
312 one = len(file_elements) == 1
313 for file_num, file_element in enumerate(file_elements, start=1):
314 file_url = url_or_none(file_element.text)
315 if not file_url:
316 continue
317 key = file_element.get('key', '')
318 upload_date = unified_strdate(self._search_regex(
319 r'^(\d{8})_', key, 'upload date', default=None))
320 if upload_date is not None:
321 # sometimes the upload date isn't included in the file name
322 # instead, another random ID is, which may parse as a valid
323 # date but be wildly out of a reasonable range
324 parsed_date = date_from_str(upload_date)
325 if parsed_date.year < 2000 or parsed_date.year >= 2100:
326 upload_date = None
327 file_duration = int_or_none(file_element.get('duration'))
328 format_id = key if key else '%s_%s' % (video_id, file_num)
329 if determine_ext(file_url) == 'm3u8':
330 formats = self._extract_m3u8_formats(
331 file_url, video_id, 'mp4', entry_protocol='m3u8_native',
332 m3u8_id='hls',
333 note='Downloading part %d m3u8 information' % file_num)
334 else:
335 formats = [{
336 'url': file_url,
337 'format_id': 'http',
338 }]
339 if not formats and not self.get_param('ignore_no_formats'):
340 continue
341 self._sort_formats(formats)
342 file_info = common_entry.copy()
343 file_info.update({
344 'id': format_id,
345 'title': title if one else '%s (part %d)' % (title, file_num),
346 'upload_date': upload_date,
347 'duration': file_duration,
348 'formats': formats,
349 })
350 entries.append(file_info)
351 entries_info = info.copy()
352 entries_info.update({
353 '_type': 'multi_video',
354 'entries': entries,
355 })
356 return entries_info
357
358 info = {
359 'id': video_id,
360 'title': title,
361 'uploader': uploader,
362 'uploader_id': uploader_id,
363 'duration': duration,
364 'thumbnail': thumbnail,
365 }
366
367 if determine_ext(video_url) == 'm3u8':
368 info['formats'] = self._extract_m3u8_formats(
369 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
370 m3u8_id='hls')
371 else:
372 app, playpath = video_url.split('mp4:')
373 info.update({
374 'url': app,
375 'ext': 'flv',
376 'play_path': 'mp4:' + playpath,
377 'rtmp_live': True, # downloading won't end without this
378 })
379
380 return info
381
382
383 class AfreecaTVLiveIE(AfreecaTVIE):
384
385 IE_NAME = 'afreecatv:live'
386 _VALID_URL = r'https?://play\.afreeca(?:tv)?\.com/(?P<id>[^/]+)(?:/(?P<bno>\d+))?'
387 _TESTS = [{
388 'url': 'https://play.afreecatv.com/pyh3646/237852185',
389 'info_dict': {
390 'id': '237852185',
391 'ext': 'mp4',
392 'title': '【 우루과이 오늘은 무슨일이? 】',
393 'uploader': '박진우[JINU]',
394 'uploader_id': 'pyh3646',
395 'timestamp': 1640661495,
396 'is_live': True,
397 },
398 'skip': 'Livestream has ended',
399 }, {
400 'url': 'http://play.afreeca.com/pyh3646/237852185',
401 'only_matching': True,
402 }, {
403 'url': 'http://play.afreeca.com/pyh3646',
404 'only_matching': True,
405 }]
406
407 _LIVE_API_URL = 'https://live.afreecatv.com/afreeca/player_live_api.php'
408
409 _QUALITIES = ('sd', 'hd', 'hd2k', 'original')
410
411 def _real_extract(self, url):
412 broadcaster_id, broadcast_no = self._match_valid_url(url).group('id', 'bno')
413 password = self.get_param('videopassword')
414
415 info = self._download_json(self._LIVE_API_URL, broadcaster_id, fatal=False,
416 data=urlencode_postdata({'bid': broadcaster_id})) or {}
417 channel_info = info.get('CHANNEL') or {}
418 broadcaster_id = channel_info.get('BJID') or broadcaster_id
419 broadcast_no = channel_info.get('BNO') or broadcast_no
420 password_protected = channel_info.get('BPWD')
421 if not broadcast_no:
422 raise ExtractorError(f'Unable to extract broadcast number ({broadcaster_id} may not be live)', expected=True)
423 if password_protected == 'Y' and password is None:
424 raise ExtractorError(
425 'This livestream is protected by a password, use the --video-password option',
426 expected=True)
427
428 formats = []
429 quality_key = qualities(self._QUALITIES)
430 for quality_str in self._QUALITIES:
431 params = {
432 'bno': broadcast_no,
433 'stream_type': 'common',
434 'type': 'aid',
435 'quality': quality_str,
436 }
437 if password is not None:
438 params['pwd'] = password
439 aid_response = self._download_json(
440 self._LIVE_API_URL, broadcast_no, fatal=False,
441 data=urlencode_postdata(params),
442 note=f'Downloading access token for {quality_str} stream',
443 errnote=f'Unable to download access token for {quality_str} stream')
444 aid = traverse_obj(aid_response, ('CHANNEL', 'AID'))
445 if not aid:
446 continue
447
448 stream_base_url = channel_info.get('RMD') or 'https://livestream-manager.afreecatv.com'
449 stream_info = self._download_json(
450 f'{stream_base_url}/broad_stream_assign.html', broadcast_no, fatal=False,
451 query={
452 'return_type': channel_info.get('CDN', 'gcp_cdn'),
453 'broad_key': f'{broadcast_no}-common-{quality_str}-hls',
454 },
455 note=f'Downloading metadata for {quality_str} stream',
456 errnote=f'Unable to download metadata for {quality_str} stream') or {}
457
458 if stream_info.get('view_url'):
459 formats.append({
460 'format_id': quality_str,
461 'url': update_url_query(stream_info['view_url'], {'aid': aid}),
462 'ext': 'mp4',
463 'protocol': 'm3u8',
464 'quality': quality_key(quality_str),
465 })
466
467 self._sort_formats(formats)
468
469 station_info = self._download_json(
470 'https://st.afreecatv.com/api/get_station_status.php', broadcast_no,
471 query={'szBjId': broadcaster_id}, fatal=False,
472 note='Downloading channel metadata', errnote='Unable to download channel metadata') or {}
473
474 return {
475 'id': broadcast_no,
476 'title': channel_info.get('TITLE') or station_info.get('station_title'),
477 'uploader': channel_info.get('BJNICK') or station_info.get('station_name'),
478 'uploader_id': broadcaster_id,
479 'timestamp': unified_timestamp(station_info.get('broad_start')),
480 'formats': formats,
481 'is_live': True,
482 }
483
484
485 class AfreecaTVUserIE(InfoExtractor):
486 IE_NAME = 'afreecatv:user'
487 _VALID_URL = r'https?://bj\.afreeca(?:tv)?\.com/(?P<id>[^/]+)/vods/?(?P<slug_type>[^/]+)?'
488 _TESTS = [{
489 'url': 'https://bj.afreecatv.com/ryuryu24/vods/review',
490 'info_dict': {
491 '_type': 'playlist',
492 'id': 'ryuryu24',
493 'title': 'ryuryu24 - review',
494 },
495 'playlist_count': 218,
496 }, {
497 'url': 'https://bj.afreecatv.com/parang1995/vods/highlight',
498 'info_dict': {
499 '_type': 'playlist',
500 'id': 'parang1995',
501 'title': 'parang1995 - highlight',
502 },
503 'playlist_count': 997,
504 }, {
505 'url': 'https://bj.afreecatv.com/ryuryu24/vods',
506 'info_dict': {
507 '_type': 'playlist',
508 'id': 'ryuryu24',
509 'title': 'ryuryu24 - all',
510 },
511 'playlist_count': 221,
512 }, {
513 'url': 'https://bj.afreecatv.com/ryuryu24/vods/balloonclip',
514 'info_dict': {
515 '_type': 'playlist',
516 'id': 'ryuryu24',
517 'title': 'ryuryu24 - balloonclip',
518 },
519 'playlist_count': 0,
520 }]
521 _PER_PAGE = 60
522
523 def _fetch_page(self, user_id, user_type, page):
524 page += 1
525 info = self._download_json(f'https://bjapi.afreecatv.com/api/{user_id}/vods/{user_type}', user_id,
526 query={'page': page, 'per_page': self._PER_PAGE, 'orderby': 'reg_date'},
527 note=f'Downloading {user_type} video page {page}')
528 for item in info['data']:
529 yield self.url_result(
530 f'https://vod.afreecatv.com/player/{item["title_no"]}/', AfreecaTVIE, item['title_no'])
531
532 def _real_extract(self, url):
533 user_id, user_type = self._match_valid_url(url).group('id', 'slug_type')
534 user_type = user_type or 'all'
535 entries = OnDemandPagedList(functools.partial(self._fetch_page, user_id, user_type), self._PER_PAGE)
536 return self.playlist_result(entries, user_id, f'{user_id} - {user_type}')