]>
Commit | Line | Data |
---|---|---|
57cf9b7f PR |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
1dbfd787 PR |
4 | import re |
5 | ||
57cf9b7f PR |
6 | from .common import InfoExtractor |
7 | from ..compat import ( | |
8 | compat_urllib_parse_urlparse, | |
9 | compat_urlparse, | |
10 | ) | |
11 | from ..utils import ( | |
12 | ExtractorError, | |
13 | int_or_none, | |
e58609b2 | 14 | update_url_query, |
e7d85c4e | 15 | xpath_element, |
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 | ''' | |
8d93c214 | 35 | _TESTS = [{ |
57cf9b7f PR |
36 | 'url': 'http://live.afreecatv.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=dailyapril&nStationNo=16711924&nBbsNo=18605867&nTitleNo=36164052&szSkin=', |
37 | 'md5': 'f72c89fe7ecc14c1b5ce506c4996046e', | |
38 | 'info_dict': { | |
39 | 'id': '36164052', | |
40 | 'ext': 'mp4', | |
41 | 'title': '데일리 에이프릴 요정들의 시상식!', | |
3452c3a2 | 42 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', |
57cf9b7f PR |
43 | 'uploader': 'dailyapril', |
44 | 'uploader_id': 'dailyapril', | |
8d93c214 | 45 | 'upload_date': '20160503', |
57cf9b7f | 46 | } |
8d93c214 PR |
47 | }, { |
48 | 'url': 'http://afbbs.afreecatv.com:8080/app/read_ucc_bbs.cgi?nStationNo=16711924&nTitleNo=36153164&szBjId=dailyapril&nBbsNo=18605867', | |
49 | 'info_dict': { | |
50 | 'id': '36153164', | |
51 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
3452c3a2 | 52 | 'thumbnail': 're:^https?://(?:video|st)img.afreecatv.com/.*$', |
8d93c214 PR |
53 | 'uploader': 'dailyapril', |
54 | 'uploader_id': 'dailyapril', | |
55 | }, | |
56 | 'playlist_count': 2, | |
57 | 'playlist': [{ | |
58 | 'md5': 'd8b7c174568da61d774ef0203159bf97', | |
59 | 'info_dict': { | |
60 | 'id': '36153164_1', | |
61 | 'ext': 'mp4', | |
62 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
63 | 'upload_date': '20160502', | |
64 | }, | |
65 | }, { | |
66 | 'md5': '58f2ce7f6044e34439ab2d50612ab02b', | |
67 | 'info_dict': { | |
68 | 'id': '36153164_2', | |
69 | 'ext': 'mp4', | |
70 | 'title': "BJ유트루와 함께하는 '팅커벨 메이크업!'", | |
71 | 'upload_date': '20160502', | |
72 | }, | |
73 | }], | |
3452c3a2 PR |
74 | }, { |
75 | 'url': 'http://www.afreecatv.com/player/Player.swf?szType=szBjId=djleegoon&nStationNo=11273158&nBbsNo=13161095&nTitleNo=36327652', | |
76 | 'only_matching': True, | |
e58609b2 S |
77 | }, { |
78 | 'url': 'http://vod.afreecatv.com/PLAYER/STATION/15055030', | |
79 | 'only_matching': True, | |
8d93c214 | 80 | }] |
57cf9b7f | 81 | |
1dbfd787 PR |
82 | @staticmethod |
83 | def parse_video_key(key): | |
0fdbe314 | 84 | video_key = {} |
1dbfd787 PR |
85 | m = re.match(r'^(?P<upload_date>\d{8})_\w+_(?P<part>\d+)$', key) |
86 | if m: | |
87 | video_key['upload_date'] = m.group('upload_date') | |
88 | video_key['part'] = m.group('part') | |
89 | return video_key | |
90 | ||
57cf9b7f PR |
91 | def _real_extract(self, url): |
92 | video_id = self._match_id(url) | |
93 | parsed_url = compat_urllib_parse_urlparse(url) | |
94 | info_url = compat_urlparse.urlunparse(parsed_url._replace( | |
95 | netloc='afbbs.afreecatv.com:8080', | |
96 | path='/api/video/get_video_info.php')) | |
e58609b2 S |
97 | |
98 | video_xml = self._download_xml( | |
99 | update_url_query(info_url, {'nTitleNo': video_id}), video_id) | |
57cf9b7f | 100 | |
e7d85c4e | 101 | if xpath_element(video_xml, './track/video/file') is None: |
57cf9b7f PR |
102 | raise ExtractorError('Specified AfreecaTV video does not exist', |
103 | expected=True) | |
e7d85c4e | 104 | |
833b644f PR |
105 | title = xpath_text(video_xml, './track/title', 'title') |
106 | uploader = xpath_text(video_xml, './track/nickname', 'uploader') | |
107 | uploader_id = xpath_text(video_xml, './track/bj_id', 'uploader id') | |
108 | duration = int_or_none(xpath_text(video_xml, './track/duration', | |
109 | 'duration')) | |
110 | thumbnail = xpath_text(video_xml, './track/titleImage', 'thumbnail') | |
57cf9b7f PR |
111 | |
112 | entries = [] | |
93fdb141 PR |
113 | for i, video_file in enumerate(video_xml.findall('./track/video/file')): |
114 | video_key = self.parse_video_key(video_file.get('key', '')) | |
115 | if not video_key: | |
116 | continue | |
833b644f | 117 | entries.append({ |
0fdbe314 | 118 | 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)), |
833b644f | 119 | 'title': title, |
0fdbe314 | 120 | 'upload_date': video_key.get('upload_date'), |
833b644f | 121 | 'duration': int_or_none(video_file.get('duration')), |
22e35ade | 122 | 'url': video_file.text, |
833b644f | 123 | }) |
57cf9b7f PR |
124 | |
125 | info = { | |
126 | 'id': video_id, | |
127 | 'title': title, | |
128 | 'uploader': uploader, | |
129 | 'uploader_id': uploader_id, | |
130 | 'duration': duration, | |
131 | 'thumbnail': thumbnail, | |
132 | } | |
133 | ||
134 | if len(entries) > 1: | |
135 | info['_type'] = 'multi_video' | |
136 | info['entries'] = entries | |
137 | elif len(entries) == 1: | |
22e35ade | 138 | info['url'] = entries[0]['url'] |
370d4eb8 | 139 | info['upload_date'] = entries[0].get('upload_date') |
57cf9b7f PR |
140 | else: |
141 | raise ExtractorError( | |
142 | 'No files found for the specified AfreecaTV video, either' | |
143 | ' the URL is incorrect or the video has been made private.', | |
144 | expected=True) | |
145 | ||
146 | return info | |
c60089c0 RA |
147 | |
148 | ||
149 | class AfreecaTVGlobalIE(AfreecaTVIE): | |
150 | IE_NAME = 'afreecatv:global' | |
151 | _VALID_URL = r'https?://(?:www\.)?afreeca\.tv/(?P<channel_id>\d+)(?:/v/(?P<video_id>\d+))?' | |
152 | _TESTS = [{ | |
153 | 'url': 'http://afreeca.tv/36853014/v/58301', | |
154 | 'info_dict': { | |
155 | 'id': '58301', | |
156 | 'title': 'tryhard top100', | |
157 | 'uploader_id': '36853014', | |
158 | 'uploader': 'makgi Hearthstone Live!', | |
159 | }, | |
160 | 'playlist_count': 3, | |
161 | }] | |
162 | ||
163 | def _real_extract(self, url): | |
164 | channel_id, video_id = re.match(self._VALID_URL, url).groups() | |
165 | video_type = 'video' if video_id else 'live' | |
166 | query = { | |
167 | 'pt': 'view', | |
168 | 'bid': channel_id, | |
169 | } | |
170 | if video_id: | |
171 | query['vno'] = video_id | |
172 | video_data = self._download_json( | |
173 | 'http://api.afreeca.tv/%s/view_%s.php' % (video_type, video_type), | |
174 | video_id or channel_id, query=query)['channel'] | |
175 | ||
176 | if video_data.get('result') != 1: | |
177 | raise ExtractorError('%s said: %s' % (self.IE_NAME, video_data['remsg'])) | |
178 | ||
179 | title = video_data['title'] | |
180 | ||
181 | info = { | |
182 | 'thumbnail': video_data.get('thumb'), | |
183 | 'view_count': int_or_none(video_data.get('vcnt')), | |
184 | 'age_limit': int_or_none(video_data.get('grade')), | |
185 | 'uploader_id': channel_id, | |
186 | 'uploader': video_data.get('cname'), | |
187 | } | |
188 | ||
189 | if video_id: | |
190 | entries = [] | |
191 | for i, f in enumerate(video_data.get('flist', [])): | |
192 | video_key = self.parse_video_key(f.get('key', '')) | |
193 | f_url = f.get('file') | |
194 | if not video_key or not f_url: | |
195 | continue | |
196 | entries.append({ | |
197 | 'id': '%s_%s' % (video_id, video_key.get('part', i + 1)), | |
198 | 'title': title, | |
199 | 'upload_date': video_key.get('upload_date'), | |
200 | 'duration': int_or_none(f.get('length')), | |
201 | 'url': f_url, | |
202 | 'protocol': 'm3u8_native', | |
203 | 'ext': 'mp4', | |
204 | }) | |
205 | ||
206 | info.update({ | |
207 | 'id': video_id, | |
208 | 'title': title, | |
209 | 'duration': int_or_none(video_data.get('length')), | |
210 | }) | |
211 | if len(entries) > 1: | |
212 | info['_type'] = 'multi_video' | |
213 | info['entries'] = entries | |
214 | elif len(entries) == 1: | |
215 | i = entries[0].copy() | |
216 | i.update(info) | |
217 | info = i | |
218 | else: | |
219 | formats = [] | |
220 | for s in video_data.get('strm', []): | |
221 | s_url = s.get('purl') | |
222 | if not s_url: | |
223 | continue | |
224 | # TODO: extract rtmp formats | |
225 | if s.get('stype') == 'HLS': | |
226 | formats.extend(self._extract_m3u8_formats( | |
227 | s_url, channel_id, 'mp4', fatal=False)) | |
228 | self._sort_formats(formats) | |
229 | ||
230 | info.update({ | |
231 | 'id': channel_id, | |
232 | 'title': self._live_title(title), | |
233 | 'is_live': True, | |
234 | 'formats': formats, | |
235 | }) | |
236 | ||
237 | return info |