]>
Commit | Line | Data |
---|---|---|
57cf9b7f PR |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
1dbfd787 PR |
4 | import re |
5 | ||
57cf9b7f | 6 | from .common import InfoExtractor |
51ef4919 | 7 | from ..compat import compat_xpath |
57cf9b7f | 8 | from ..utils import ( |
b2eeee0c | 9 | date_from_str, |
6b9466de | 10 | determine_ext, |
57cf9b7f PR |
11 | ExtractorError, |
12 | int_or_none, | |
b2eeee0c | 13 | unified_strdate, |
3052a30d | 14 | url_or_none, |
e51762be | 15 | urlencode_postdata, |
833b644f | 16 | xpath_text, |
57cf9b7f PR |
17 | ) |
18 | ||
19 | ||
20 | class AfreecaTVIE(InfoExtractor): | |
c60089c0 | 21 | IE_NAME = 'afreecatv' |
57cf9b7f | 22 | IE_DESC = 'afreecatv.com' |
e58609b2 S |
23 | _VALID_URL = r'''(?x) |
24 | https?:// | |
25 | (?: | |
26 | (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)? | |
27 | (?: | |
28 | /app/(?:index|read_ucc_bbs)\.cgi| | |
29 | /player/[Pp]layer\.(?:swf|html) | |
30 | )\?.*?\bnTitleNo=| | |
31 | vod\.afreecatv\.com/PLAYER/STATION/ | |
32 | ) | |
33 | (?P<id>\d+) | |
34 | ''' | |
e51762be | 35 | _NETRC_MACHINE = 'afreecatv' |
8d93c214 | 36 | _TESTS = [{ |
57cf9b7f PR |
37 | 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=', |
38 | 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e', | |
39 | 'info_dict': { | |
40 | 'id': '36164052', | |
41 | 'ext': 'mp4', | |
42 | 'title': '데일리 에이프릴 요정들의 시상식!', | |
3452c3a2 | 43 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', |
57cf9b7f PR |
44 | 'uploader': 'dailyapril', |
45 | 'uploader_id': 'dailyapril', | |
8d93c214 | 46 | 'upload_date': '20160503', |
51ef4919 YCH |
47 | }, |
48 | 'skip': 'Video is gone', | |
8d93c214 PR |
49 | }, { |
50 | 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867', | |
51 | 'info_dict': { | |
52 | 'id': '36153164', | |
53 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
3452c3a2 | 54 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', |
8d93c214 PR |
55 | 'uploader': 'dailyapril', |
56 | 'uploader_id': 'dailyapril', | |
57 | }, | |
58 | 'playlist_count': 2, | |
59 | 'playlist': [{ | |
60 | 'md5': 'd8b7c174568da61d774ef0203159bf97', | |
61 | 'info_dict': { | |
62 | 'id': '36153164_1', | |
63 | 'ext': 'mp4', | |
64 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
65 | 'upload_date': '20160502', | |
66 | }, | |
67 | }, { | |
68 | 'md5': '58f2ce7f6044e34439ab2d50612ab02b', | |
69 | 'info_dict': { | |
70 | 'id': '36153164_2', | |
71 | 'ext': 'mp4', | |
72 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
73 | 'upload_date': '20160502', | |
74 | }, | |
75 | }], | |
51ef4919 YCH |
76 | 'skip': 'Video is gone', |
77 | }, { | |
78 | 'url': 'http://vod.afreecatv.com/PLAYER/STATION/18650793', | |
79 | 'info_dict': { | |
80 | 'id': '18650793', | |
6b9466de S |
81 | 'ext': 'mp4', |
82 | 'title': '오늘은 다르다! 쏘님의 우월한 위아래~ 댄스리액션!', | |
83 | 'thumbnail': r're:^https?://.*\.jpg$', | |
51ef4919 YCH |
84 | 'uploader': '윈아디', |
85 | 'uploader_id': 'badkids', | |
6b9466de S |
86 | 'duration': 107, |
87 | }, | |
88 | 'params': { | |
89 | 'skip_download': True, | |
90 | }, | |
91 | }, { | |
92 | 'url': 'http://vod.afreecatv.com/PLAYER/STATION/10481652', | |
93 | 'info_dict': { | |
94 | 'id': '10481652', | |
95 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
96 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', | |
97 | 'uploader': 'dailyapril', | |
98 | 'uploader_id': 'dailyapril', | |
99 | 'duration': 6492, | |
51ef4919 | 100 | }, |
6b9466de S |
101 | 'playlist_count': 2, |
102 | 'playlist': [{ | |
103 | 'md5': 'd8b7c174568da61d774ef0203159bf97', | |
104 | 'info_dict': { | |
e109f1ff | 105 | 'id': '20160502_c4c62b9d_174361386_1', |
6b9466de S |
106 | 'ext': 'mp4', |
107 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 1)", | |
108 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', | |
109 | 'uploader': 'dailyapril', | |
110 | 'uploader_id': 'dailyapril', | |
111 | 'upload_date': '20160502', | |
112 | 'duration': 3601, | |
113 | }, | |
114 | }, { | |
115 | 'md5': '58f2ce7f6044e34439ab2d50612ab02b', | |
116 | 'info_dict': { | |
e109f1ff | 117 | 'id': '20160502_39e739bb_174361386_2', |
6b9466de S |
118 | 'ext': 'mp4', |
119 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 2)", | |
120 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', | |
121 | 'uploader': 'dailyapril', | |
122 | 'uploader_id': 'dailyapril', | |
123 | 'upload_date': '20160502', | |
124 | 'duration': 2891, | |
125 | }, | |
126 | }], | |
51ef4919 | 127 | 'params': { |
6b9466de | 128 | 'skip_download': True, |
51ef4919 | 129 | }, |
e109f1ff S |
130 | }, { |
131 | # non standard key | |
132 | 'url': 'http://vod.afreecatv.com/PLAYER/STATION/20515605', | |
133 | 'info_dict': { | |
134 | 'id': '20170411_BE689A0E_190960999_1_2_h', | |
135 | 'ext': 'mp4', | |
136 | 'title': '혼자사는여자집', | |
137 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', | |
138 | 'uploader': '♥이슬이', | |
139 | 'uploader_id': 'dasl8121', | |
140 | 'upload_date': '20170411', | |
141 | 'duration': 213, | |
142 | }, | |
143 | 'params': { | |
144 | 'skip_download': True, | |
145 | }, | |
839728f5 | 146 | }, { |
86693c49 S |
147 | # PARTIAL_ADULT |
148 | 'url': 'http://vod.afreecatv.com/PLAYER/STATION/32028439', | |
839728f5 | 149 | 'info_dict': { |
86693c49 | 150 | 'id': '20180327_27901457_202289533_1', |
839728f5 | 151 | 'ext': 'mp4', |
86693c49 | 152 | 'title': '[생]빨개요♥ (part 1)', |
839728f5 | 153 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', |
86693c49 | 154 | 'uploader': '[SA]서아', |
839728f5 | 155 | 'uploader_id': 'bjdyrksu', |
86693c49 S |
156 | 'upload_date': '20180327', |
157 | 'duration': 3601, | |
839728f5 S |
158 | }, |
159 | 'params': { | |
160 | 'skip_download': True, | |
161 | }, | |
86693c49 | 162 | 'expected_warnings': ['adult content'], |
3452c3a2 PR |
163 | }, { |
164 | 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652', | |
165 | 'only_matching': True, | |
e58609b2 S |
166 | }, { |
167 | 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030', | |
168 | 'only_matching': True, | |
8d93c214 | 169 | }] |
57cf9b7f | 170 | |
1dbfd787 PR |
171 | @staticmethod |
172 | def parse_video_key(key): | |
0fdbe314 | 173 | video_key = {} |
1dbfd787 PR |
174 | m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key) |
175 | if m: | |
176 | video_key['upload_date'] = m.group('upload_date') | |
6b9466de | 177 | video_key['part'] = int(m.group('part')) |
1dbfd787 PR |
178 | return video_key |
179 | ||
e51762be S |
180 | def _real_initialize(self): |
181 | self._login() | |
182 | ||
183 | def _login(self): | |
184 | username, password = self._get_login_info() | |
185 | if username is None: | |
186 | return | |
187 | ||
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 | ||
57cf9b7f PR |
225 | def _real_extract(self, url): |
226 | video_id = self._match_id(url) | |
e58609b2 | 227 | |
9e36fedd S |
228 | webpage = self._download_webpage(url, video_id) |
229 | ||
f9f10268 S |
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 | ||
9e36fedd S |
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) | |
d563fb32 | 240 | |
86693c49 | 241 | partial_view = False |
875cfb8c | 242 | adult_view = False |
86693c49 S |
243 | for _ in range(2): |
244 | query = { | |
839728f5 | 245 | 'nTitleNo': video_id, |
9e36fedd S |
246 | 'nStationNo': station_id, |
247 | 'nBbsNo': bbs_id, | |
86693c49 S |
248 | } |
249 | if partial_view: | |
250 | query['partialView'] = 'SKIP_ADULT' | |
875cfb8c LR |
251 | if adult_view: |
252 | query['adultView'] = 'ADULT_VIEW' | |
86693c49 S |
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) | |
839728f5 | 260 | |
86693c49 S |
261 | flag = xpath_text(video_xml, './track/flag', 'flag', default=None) |
262 | if flag and flag == 'SUCCEED': | |
263 | break | |
264 | if flag == 'PARTIAL_ADULT': | |
6a39ee13 | 265 | self.report_warning( |
86693c49 S |
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': | |
875cfb8c LR |
272 | if not adult_view: |
273 | adult_view = True | |
274 | continue | |
86693c49 S |
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 | |
839728f5 | 278 | raise ExtractorError( |
86693c49 S |
279 | '%s said: %s' % (self.IE_NAME, error), expected=True) |
280 | else: | |
281 | raise ExtractorError('Unable to download video info') | |
57cf9b7f | 282 | |
f241a973 | 283 | video_element = video_xml.findall(compat_xpath('./track/video'))[-1] |
51ef4919 | 284 | if video_element is None or video_element.text is None: |
f9f10268 | 285 | raise ExtractorError( |
8bdd16b4 | 286 | 'Video %s does not exist' % video_id, expected=True) |
e7d85c4e | 287 | |
6b9466de | 288 | video_url = video_element.text.strip() |
51ef4919 YCH |
289 | |
290 | title = xpath_text(video_xml, './track/title', 'title', fatal=True) | |
6b9466de | 291 | |
833b644f PR |
292 | uploader = xpath_text(video_xml, './track/nickname', 'uploader') |
293 | uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id') | |
6b9466de S |
294 | duration = int_or_none(xpath_text( |
295 | video_xml, './track/duration', 'duration')) | |
833b644f | 296 | thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail') |
57cf9b7f | 297 | |
6b9466de S |
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 = [] | |
e109f1ff S |
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): | |
3052a30d | 316 | file_url = url_or_none(file_element.text) |
6b9466de S |
317 | if not file_url: |
318 | continue | |
e109f1ff | 319 | key = file_element.get('key', '') |
b2eeee0c LR |
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 | |
6b9466de | 329 | file_duration = int_or_none(file_element.get('duration')) |
e109f1ff | 330 | format_id = key if key else '%s_%s' % (video_id, file_num) |
4a109f81 S |
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 | }] | |
a06916d9 | 341 | if not formats and not self.get_param('ignore_no_formats'): |
4a109f81 S |
342 | continue |
343 | self._sort_formats(formats) | |
6b9466de S |
344 | file_info = common_entry.copy() |
345 | file_info.update({ | |
346 | 'id': format_id, | |
6b4ddd33 | 347 | 'title': title if one else '%s (part %d)' % (title, file_num), |
e109f1ff | 348 | 'upload_date': upload_date, |
6b9466de S |
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 = { | |
57cf9b7f PR |
361 | 'id': video_id, |
362 | 'title': title, | |
363 | 'uploader': uploader, | |
364 | 'uploader_id': uploader_id, | |
365 | 'duration': duration, | |
366 | 'thumbnail': thumbnail, | |
367 | } | |
368 | ||
6b9466de S |
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 |