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