]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/afreecatv.py
New upstream version 2018.06.18
[youtubedl] / youtube_dl / 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 determine_ext,
10 ExtractorError,
11 int_or_none,
12 urlencode_postdata,
13 xpath_text,
14 )
15
16
17 class AfreecaTVIE(InfoExtractor):
18 IE_NAME = 'afreecatv'
19 IE_DESC = 'afreecatv.com'
20 _VALID_URL = r'''(?x)
21 https?://
22 (?:
23 (?:(?:live|afbbs|www)\.)?afreeca(?:tv)?\.com(?::\d+)?
24 (?:
25 /app/(?:index|read_ucc_bbs)\.cgi|
26 /player/[Pp]layer\.(?:swf|html)
27 )\?.*?\bnTitleNo=|
28 vod\.afreecatv\.com/PLAYER/STATION/
29 )
30 (?P<id>\d+)
31 '''
32 _NETRC_MACHINE = 'afreecatv'
33 _TESTS = [{
34 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=',
35 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e',
36 'info_dict': {
37 'id': '36164052',
38 'ext': 'mp4',
39 'title': '데일리 에이프릴 요정들의 시상식!',
40 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
41 'uploader': 'dailyapril',
42 'uploader_id': 'dailyapril',
43 'upload_date': '20160503',
44 },
45 'skip': 'Video is gone',
46 }, {
47 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867',
48 'info_dict': {
49 'id': '36153164',
50 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
51 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
52 'uploader': 'dailyapril',
53 'uploader_id': 'dailyapril',
54 },
55 'playlist_count': 2,
56 'playlist': [{
57 'md5': 'd8b7c174568da61d774ef0203159bf97',
58 'info_dict': {
59 'id': '36153164_1',
60 'ext': 'mp4',
61 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
62 'upload_date': '20160502',
63 },
64 }, {
65 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
66 'info_dict': {
67 'id': '36153164_2',
68 'ext': 'mp4',
69 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
70 'upload_date': '20160502',
71 },
72 }],
73 'skip': 'Video is gone',
74 }, {
75 'url': 'http://vod.afreecatv.com/PLAYER/STATION/18650793',
76 'info_dict': {
77 'id': '18650793',
78 'ext': 'mp4',
79 'title': '오늘은 다르다! 쏘님의 우월한 위아래~ 댄스리액션!',
80 'thumbnail': r're:^https?://.*\.jpg$',
81 'uploader': '윈아디',
82 'uploader_id': 'badkids',
83 'duration': 107,
84 },
85 'params': {
86 'skip_download': True,
87 },
88 }, {
89 'url': 'http://vod.afreecatv.com/PLAYER/STATION/10481652',
90 'info_dict': {
91 'id': '10481652',
92 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'",
93 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
94 'uploader': 'dailyapril',
95 'uploader_id': 'dailyapril',
96 'duration': 6492,
97 },
98 'playlist_count': 2,
99 'playlist': [{
100 'md5': 'd8b7c174568da61d774ef0203159bf97',
101 'info_dict': {
102 'id': '20160502_c4c62b9d_174361386_1',
103 'ext': 'mp4',
104 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 1)",
105 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
106 'uploader': 'dailyapril',
107 'uploader_id': 'dailyapril',
108 'upload_date': '20160502',
109 'duration': 3601,
110 },
111 }, {
112 'md5': '58f2ce7f6044e34439ab2d50612ab02b',
113 'info_dict': {
114 'id': '20160502_39e739bb_174361386_2',
115 'ext': 'mp4',
116 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!' (part 2)",
117 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
118 'uploader': 'dailyapril',
119 'uploader_id': 'dailyapril',
120 'upload_date': '20160502',
121 'duration': 2891,
122 },
123 }],
124 'params': {
125 'skip_download': True,
126 },
127 }, {
128 # non standard key
129 'url': 'http://vod.afreecatv.com/PLAYER/STATION/20515605',
130 'info_dict': {
131 'id': '20170411_BE689A0E_190960999_1_2_h',
132 'ext': 'mp4',
133 'title': '혼자사는여자집',
134 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
135 'uploader': '♥이슬이',
136 'uploader_id': 'dasl8121',
137 'upload_date': '20170411',
138 'duration': 213,
139 },
140 'params': {
141 'skip_download': True,
142 },
143 }, {
144 # PARTIAL_ADULT
145 'url': 'http://vod.afreecatv.com/PLAYER/STATION/32028439',
146 'info_dict': {
147 'id': '20180327_27901457_202289533_1',
148 'ext': 'mp4',
149 'title': '[생]빨개요♥ (part 1)',
150 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$',
151 'uploader': '[SA]서아',
152 'uploader_id': 'bjdyrksu',
153 'upload_date': '20180327',
154 'duration': 3601,
155 },
156 'params': {
157 'skip_download': True,
158 },
159 'expected_warnings': ['adult content'],
160 }, {
161 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652',
162 'only_matching': True,
163 }, {
164 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030',
165 'only_matching': True,
166 }]
167
168 @staticmethod
169 def parse_video_key(key):
170 video_key = {}
171 m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key)
172 if m:
173 video_key['upload_date'] = m.group('upload_date')
174 video_key['part'] = int(m.group('part'))
175 return video_key
176
177 def _real_initialize(self):
178 self._login()
179
180 def _login(self):
181 username, password = self._get_login_info()
182 if username is None:
183 return
184
185 login_form = {
186 'szWork': 'login',
187 'szType': 'json',
188 'szUid': username,
189 'szPassword': password,
190 'isSaveId': 'false',
191 'szScriptVar': 'oLoginRet',
192 'szAction': '',
193 }
194
195 response = self._download_json(
196 'https://login.afreecatv.com/app/LoginAction.php', None,
197 'Logging in', data=urlencode_postdata(login_form))
198
199 _ERRORS = {
200 -4: 'Your account has been suspended due to a violation of our terms and policies.',
201 -5: 'https://member.afreecatv.com/app/user_delete_progress.php',
202 -6: 'https://login.afreecatv.com/membership/changeMember.php',
203 -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.",
204 -9: 'https://member.afreecatv.com/app/pop_login_block.php',
205 -11: 'https://login.afreecatv.com/afreeca/second_login.php',
206 -12: 'https://member.afreecatv.com/app/user_security.php',
207 0: 'The username does not exist or you have entered the wrong password.',
208 -1: 'The username does not exist or you have entered the wrong password.',
209 -3: 'You have entered your username/password incorrectly.',
210 -7: 'You cannot use your Global AfreecaTV account to access Korean AfreecaTV.',
211 -10: 'Sorry for the inconvenience. \nYour account has been blocked due to an unauthorized access. \nPlease contact our Help Center for assistance.',
212 -32008: 'You have failed to log in. Please contact our Help Center.',
213 }
214
215 result = int_or_none(response.get('RESULT'))
216 if result != 1:
217 error = _ERRORS.get(result, 'You have failed to log in.')
218 raise ExtractorError(
219 'Unable to login: %s said: %s' % (self.IE_NAME, error),
220 expected=True)
221
222 def _real_extract(self, url):
223 video_id = self._match_id(url)
224
225 webpage = self._download_webpage(url, video_id)
226
227 if re.search(r'alert\(["\']This video has been deleted', webpage):
228 raise ExtractorError(
229 'Video %s has been deleted' % video_id, expected=True)
230
231 station_id = self._search_regex(
232 r'nStationNo\s*=\s*(\d+)', webpage, 'station')
233 bbs_id = self._search_regex(
234 r'nBbsNo\s*=\s*(\d+)', webpage, 'bbs')
235 video_id = self._search_regex(
236 r'nTitleNo\s*=\s*(\d+)', webpage, 'title', default=video_id)
237
238 partial_view = False
239 for _ in range(2):
240 query = {
241 'nTitleNo': video_id,
242 'nStationNo': station_id,
243 'nBbsNo': bbs_id,
244 }
245 if partial_view:
246 query['partialView'] = 'SKIP_ADULT'
247 video_xml = self._download_xml(
248 'http://afbbs.afreecatv.com:8080/api/video/get_video_info.php',
249 video_id, 'Downloading video info XML%s'
250 % (' (skipping adult)' if partial_view else ''),
251 video_id, headers={
252 'Referer': url,
253 }, query=query)
254
255 flag = xpath_text(video_xml, './track/flag', 'flag', default=None)
256 if flag and flag == 'SUCCEED':
257 break
258 if flag == 'PARTIAL_ADULT':
259 self._downloader.report_warning(
260 'In accordance with local laws and regulations, underage users are restricted from watching adult content. '
261 'Only content suitable for all ages will be downloaded. '
262 'Provide account credentials if you wish to download restricted content.')
263 partial_view = True
264 continue
265 elif flag == 'ADULT':
266 error = 'Only users older than 19 are able to watch this video. Provide account credentials to download this content.'
267 else:
268 error = flag
269 raise ExtractorError(
270 '%s said: %s' % (self.IE_NAME, error), expected=True)
271 else:
272 raise ExtractorError('Unable to download video info')
273
274 video_element = video_xml.findall(compat_xpath('./track/video'))[-1]
275 if video_element is None or video_element.text is None:
276 raise ExtractorError(
277 'Video %s video does not exist' % video_id, expected=True)
278
279 video_url = video_element.text.strip()
280
281 title = xpath_text(video_xml, './track/title', 'title', fatal=True)
282
283 uploader = xpath_text(video_xml, './track/nickname', 'uploader')
284 uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id')
285 duration = int_or_none(xpath_text(
286 video_xml, './track/duration', 'duration'))
287 thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail')
288
289 common_entry = {
290 'uploader': uploader,
291 'uploader_id': uploader_id,
292 'thumbnail': thumbnail,
293 }
294
295 info = common_entry.copy()
296 info.update({
297 'id': video_id,
298 'title': title,
299 'duration': duration,
300 })
301
302 if not video_url:
303 entries = []
304 file_elements = video_element.findall(compat_xpath('./file'))
305 one = len(file_elements) == 1
306 for file_num, file_element in enumerate(file_elements, start=1):
307 file_url = file_element.text
308 if not file_url:
309 continue
310 key = file_element.get('key', '')
311 upload_date = self._search_regex(
312 r'^(\d{8})_', key, 'upload date', default=None)
313 file_duration = int_or_none(file_element.get('duration'))
314 format_id = key if key else '%s_%s' % (video_id, file_num)
315 if determine_ext(file_url) == 'm3u8':
316 formats = self._extract_m3u8_formats(
317 file_url, video_id, 'mp4', entry_protocol='m3u8_native',
318 m3u8_id='hls',
319 note='Downloading part %d m3u8 information' % file_num)
320 else:
321 formats = [{
322 'url': file_url,
323 'format_id': 'http',
324 }]
325 if not formats:
326 continue
327 self._sort_formats(formats)
328 file_info = common_entry.copy()
329 file_info.update({
330 'id': format_id,
331 'title': title if one else '%s (part %d)' % (title, file_num),
332 'upload_date': upload_date,
333 'duration': file_duration,
334 'formats': formats,
335 })
336 entries.append(file_info)
337 entries_info = info.copy()
338 entries_info.update({
339 '_type': 'multi_video',
340 'entries': entries,
341 })
342 return entries_info
343
344 info = {
345 'id': video_id,
346 'title': title,
347 'uploader': uploader,
348 'uploader_id': uploader_id,
349 'duration': duration,
350 'thumbnail': thumbnail,
351 }
352
353 if determine_ext(video_url) == 'm3u8':
354 info['formats'] = self._extract_m3u8_formats(
355 video_url, video_id, 'mp4', entry_protocol='m3u8_native',
356 m3u8_id='hls')
357 else:
358 app, playpath = video_url.split('mp4:')
359 info.update({
360 'url': app,
361 'ext': 'flv',
362 'play_path': 'mp4:' + playpath,
363 'rtmp_live': True, # downloading won't end without this
364 })
365
366 return info