]>
Commit | Line | Data |
---|---|---|
7def3571 T |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
7def3571 | 4 | |
0fc832e1 | 5 | from .common import InfoExtractor |
2181983a | 6 | from ..utils import ( |
11cc4571 | 7 | clean_html, |
10bb7e51 M |
8 | ExtractorError, |
9 | get_element_by_class, | |
2181983a | 10 | int_or_none, |
11 | parse_iso8601, | |
f2cd7060 | 12 | smuggle_url, |
2181983a | 13 | str_or_none, |
14 | strip_or_none, | |
15 | try_get, | |
f2cd7060 | 16 | unsmuggle_url, |
10bb7e51 | 17 | urlencode_postdata, |
2181983a | 18 | ) |
7def3571 T |
19 | |
20 | ||
f2cd7060 | 21 | class VidioBaseIE(InfoExtractor): |
10bb7e51 M |
22 | _LOGIN_URL = 'https://www.vidio.com/users/login' |
23 | _NETRC_MACHINE = 'vidio' | |
24 | ||
25 | def _login(self): | |
26 | username, password = self._get_login_info() | |
27 | if username is None: | |
28 | return | |
29 | ||
30 | def is_logged_in(): | |
31 | res = self._download_json( | |
32 | 'https://www.vidio.com/interactions.json', None, 'Checking if logged in', fatal=False) or {} | |
33 | return bool(res.get('current_user')) | |
34 | ||
35 | if is_logged_in(): | |
36 | return | |
37 | ||
38 | login_page = self._download_webpage( | |
39 | self._LOGIN_URL, None, 'Downloading log in page') | |
40 | ||
41 | login_form = self._form_hidden_inputs("login-form", login_page) | |
42 | login_form.update({ | |
43 | 'user[login]': username, | |
44 | 'user[password]': password, | |
45 | }) | |
46 | login_post, login_post_urlh = self._download_webpage_handle( | |
47 | self._LOGIN_URL, None, 'Logging in', data=urlencode_postdata(login_form), expected_status=[302, 401]) | |
48 | ||
49 | if login_post_urlh.status == 401: | |
11cc4571 | 50 | if get_element_by_class('onboarding-content-register-popup__title', login_post): |
10bb7e51 | 51 | raise ExtractorError( |
11cc4571 M |
52 | 'Unable to log in: The provided email has not registered yet.', expected=True) |
53 | ||
54 | reason = get_element_by_class('onboarding-form__general-error', login_post) or get_element_by_class('onboarding-modal__title', login_post) | |
55 | if 'Akun terhubung ke' in reason: | |
56 | raise ExtractorError( | |
57 | 'Unable to log in: Your account is linked to a social media account. ' | |
58 | 'Use --cookies to provide account credentials instead', expected=True) | |
59 | elif reason: | |
60 | subreason = get_element_by_class('onboarding-modal__description-text', login_post) or '' | |
61 | raise ExtractorError( | |
62 | 'Unable to log in: %s. %s' % (reason, clean_html(subreason)), expected=True) | |
10bb7e51 | 63 | raise ExtractorError('Unable to log in') |
7def3571 | 64 | |
2181983a | 65 | def _real_initialize(self): |
66 | self._api_key = self._download_json( | |
67 | 'https://www.vidio.com/auth', None, data=b'')['api_key'] | |
10bb7e51 | 68 | self._login() |
0fc832e1 | 69 | |
f2cd7060 M |
70 | def _call_api(self, url, video_id, note=None): |
71 | return self._download_json(url, video_id, note=note, headers={ | |
72 | 'Content-Type': 'application/vnd.api+json', | |
73 | 'X-API-KEY': self._api_key, | |
74 | }) | |
75 | ||
76 | ||
77 | class VidioIE(VidioBaseIE): | |
78 | _VALID_URL = r'https?://(?:www\.)?vidio\.com/watch/(?P<id>\d+)-(?P<display_id>[^/?#&]+)' | |
79 | _TESTS = [{ | |
80 | 'url': 'http://www.vidio.com/watch/165683-dj_ambred-booyah-live-2015', | |
81 | 'md5': 'cd2801394afc164e9775db6a140b91fe', | |
82 | 'info_dict': { | |
83 | 'id': '165683', | |
84 | 'display_id': 'dj_ambred-booyah-live-2015', | |
85 | 'ext': 'mp4', | |
86 | 'title': 'DJ_AMBRED - Booyah (Live 2015)', | |
87 | 'description': 'md5:27dc15f819b6a78a626490881adbadf8', | |
88 | 'thumbnail': r're:^https?://.*\.jpg$', | |
89 | 'duration': 149, | |
90 | 'like_count': int, | |
91 | 'uploader': 'TWELVE Pic', | |
92 | 'timestamp': 1444902800, | |
93 | 'upload_date': '20151015', | |
94 | 'uploader_id': 'twelvepictures', | |
95 | 'channel': 'Cover Music Video', | |
96 | 'channel_id': '280236', | |
97 | 'view_count': int, | |
98 | 'dislike_count': int, | |
99 | 'comment_count': int, | |
100 | 'tags': 'count:4', | |
101 | }, | |
102 | }, { | |
103 | 'url': 'https://www.vidio.com/watch/77949-south-korea-test-fires-missile-that-can-strike-all-of-the-north', | |
104 | 'only_matching': True, | |
105 | }, { | |
106 | # Premier-exclusive video | |
107 | 'url': 'https://www.vidio.com/watch/1550718-stand-by-me-doraemon', | |
108 | 'only_matching': True | |
109 | }] | |
110 | ||
2181983a | 111 | def _real_extract(self, url): |
5ad28e7f | 112 | match = self._match_valid_url(url).groupdict() |
f2cd7060 M |
113 | video_id, display_id = match.get('id'), match.get('display_id') |
114 | data = self._call_api('https://api.vidio.com/videos/' + video_id, display_id) | |
2181983a | 115 | video = data['videos'][0] |
116 | title = video['title'].strip() | |
46c43ffc | 117 | is_premium = video.get('is_premium') |
f2cd7060 | 118 | |
46c43ffc M |
119 | if is_premium: |
120 | sources = self._download_json( | |
121 | 'https://www.vidio.com/interactions_stream.json?video_id=%s&type=videos' % video_id, | |
122 | display_id, note='Downloading premier API JSON') | |
123 | if not (sources.get('source') or sources.get('source_dash')): | |
f2cd7060 | 124 | self.raise_login_required('This video is only available for registered users with the appropriate subscription') |
46c43ffc M |
125 | |
126 | formats, subs = [], {} | |
127 | if sources.get('source'): | |
128 | hls_formats, hls_subs = self._extract_m3u8_formats_and_subtitles( | |
129 | sources['source'], display_id, 'mp4', 'm3u8_native') | |
130 | formats.extend(hls_formats) | |
131 | subs.update(hls_subs) | |
132 | if sources.get('source_dash'): # TODO: Find video example with source_dash | |
133 | dash_formats, dash_subs = self._extract_mpd_formats_and_subtitles( | |
134 | sources['source_dash'], display_id, 'dash') | |
135 | formats.extend(dash_formats) | |
136 | subs.update(dash_subs) | |
137 | else: | |
138 | hls_url = data['clips'][0]['hls_url'] | |
139 | formats, subs = self._extract_m3u8_formats_and_subtitles( | |
140 | hls_url, display_id, 'mp4', 'm3u8_native') | |
0fc832e1 | 141 | |
07ad0cf3 | 142 | self._sort_formats(formats) |
0fc832e1 | 143 | |
2181983a | 144 | get_first = lambda x: try_get(data, lambda y: y[x + 's'][0], dict) or {} |
145 | channel = get_first('channel') | |
146 | user = get_first('user') | |
147 | username = user.get('username') | |
148 | get_count = lambda x: int_or_none(video.get('total_' + x)) | |
7def3571 T |
149 | |
150 | return { | |
151 | 'id': video_id, | |
0fc832e1 S |
152 | 'display_id': display_id, |
153 | 'title': title, | |
2181983a | 154 | 'description': strip_or_none(video.get('description')), |
155 | 'thumbnail': video.get('image_url_medium'), | |
156 | 'duration': int_or_none(video.get('duration')), | |
157 | 'like_count': get_count('likes'), | |
0fc832e1 | 158 | 'formats': formats, |
46c43ffc | 159 | 'subtitles': subs, |
2181983a | 160 | 'uploader': user.get('name'), |
161 | 'timestamp': parse_iso8601(video.get('created_at')), | |
162 | 'uploader_id': username, | |
163 | 'uploader_url': 'https://www.vidio.com/@' + username if username else None, | |
164 | 'channel': channel.get('name'), | |
165 | 'channel_id': str_or_none(channel.get('id')), | |
166 | 'view_count': get_count('view_count'), | |
167 | 'dislike_count': get_count('dislikes'), | |
168 | 'comment_count': get_count('comments'), | |
169 | 'tags': video.get('tag_list'), | |
7def3571 | 170 | } |
f2cd7060 M |
171 | |
172 | ||
173 | class VidioPremierIE(VidioBaseIE): | |
174 | _VALID_URL = r'https?://(?:www\.)?vidio\.com/premier/(?P<id>\d+)/(?P<display_id>[^/?#&]+)' | |
175 | _TESTS = [{ | |
176 | 'url': 'https://www.vidio.com/premier/2885/badai-pasti-berlalu', | |
177 | 'playlist_mincount': 14, | |
178 | }, { | |
179 | # Series with both free and premier-exclusive videos | |
180 | 'url': 'https://www.vidio.com/premier/2567/sosmed', | |
181 | 'only_matching': True, | |
182 | }] | |
183 | ||
184 | def _playlist_entries(self, playlist_url, display_id): | |
185 | index = 1 | |
186 | while playlist_url: | |
187 | playlist_json = self._call_api(playlist_url, display_id, 'Downloading API JSON page %s' % index) | |
188 | for video_json in playlist_json.get('data', []): | |
189 | link = video_json['links']['watchpage'] | |
190 | yield self.url_result(link, 'Vidio', video_json['id']) | |
191 | playlist_url = try_get(playlist_json, lambda x: x['links']['next']) | |
192 | index += 1 | |
193 | ||
194 | def _real_extract(self, url): | |
195 | url, idata = unsmuggle_url(url, {}) | |
5ad28e7f | 196 | playlist_id, display_id = self._match_valid_url(url).groups() |
f2cd7060 M |
197 | |
198 | playlist_url = idata.get('url') | |
199 | if playlist_url: # Smuggled data contains an API URL. Download only that playlist | |
200 | playlist_id = idata['id'] | |
201 | return self.playlist_result( | |
202 | self._playlist_entries(playlist_url, playlist_id), | |
203 | playlist_id=playlist_id, playlist_title=idata.get('title')) | |
204 | ||
205 | playlist_data = self._call_api('https://api.vidio.com/content_profiles/%s/playlists' % playlist_id, display_id) | |
206 | ||
207 | return self.playlist_from_matches( | |
208 | playlist_data.get('data', []), playlist_id=playlist_id, ie=self.ie_key(), | |
209 | getter=lambda data: smuggle_url(url, { | |
210 | 'url': data['relationships']['videos']['links']['related'], | |
211 | 'id': data['id'], | |
212 | 'title': try_get(data, lambda x: x['attributes']['name']) | |
213 | })) | |
214 | ||
215 | ||
216 | class VidioLiveIE(VidioBaseIE): | |
217 | _VALID_URL = r'https?://(?:www\.)?vidio\.com/live/(?P<id>\d+)-(?P<display_id>[^/?#&]+)' | |
218 | _TESTS = [{ | |
219 | 'url': 'https://www.vidio.com/live/204-sctv', | |
220 | 'info_dict': { | |
221 | 'id': '204', | |
222 | 'title': 'SCTV', | |
223 | 'uploader': 'SCTV', | |
224 | 'uploader_id': 'sctv', | |
225 | 'thumbnail': r're:^https?://.*\.jpg$', | |
226 | }, | |
227 | }, { | |
228 | # Premier-exclusive livestream | |
229 | 'url': 'https://www.vidio.com/live/6362-tvn', | |
230 | 'only_matching': True, | |
231 | }, { | |
232 | # DRM premier-exclusive livestream | |
233 | 'url': 'https://www.vidio.com/live/6299-bein-1', | |
234 | 'only_matching': True, | |
235 | }] | |
236 | ||
237 | def _real_extract(self, url): | |
5ad28e7f | 238 | video_id, display_id = self._match_valid_url(url).groups() |
f2cd7060 M |
239 | stream_data = self._call_api( |
240 | 'https://www.vidio.com/api/livestreamings/%s/detail' % video_id, display_id) | |
241 | stream_meta = stream_data['livestreamings'][0] | |
242 | user = stream_data.get('users', [{}])[0] | |
243 | ||
244 | title = stream_meta.get('title') | |
245 | username = user.get('username') | |
246 | ||
247 | formats = [] | |
248 | if stream_meta.get('is_drm'): | |
249 | if not self.get_param('allow_unplayable_formats'): | |
250 | self.raise_no_formats( | |
251 | 'This video is DRM protected.', expected=True) | |
252 | if stream_meta.get('is_premium'): | |
253 | sources = self._download_json( | |
254 | 'https://www.vidio.com/interactions_stream.json?video_id=%s&type=livestreamings' % video_id, | |
255 | display_id, note='Downloading premier API JSON') | |
256 | if not (sources.get('source') or sources.get('source_dash')): | |
257 | self.raise_login_required('This video is only available for registered users with the appropriate subscription') | |
258 | ||
259 | if str_or_none(sources.get('source')): | |
260 | token_json = self._download_json( | |
261 | 'https://www.vidio.com/live/%s/tokens' % video_id, | |
262 | display_id, note='Downloading HLS token JSON', data=b'') | |
263 | formats.extend(self._extract_m3u8_formats( | |
264 | sources['source'] + '?' + token_json.get('token', ''), display_id, 'mp4', 'm3u8_native')) | |
265 | if str_or_none(sources.get('source_dash')): | |
266 | pass | |
267 | else: | |
268 | if stream_meta.get('stream_token_url'): | |
269 | token_json = self._download_json( | |
270 | 'https://www.vidio.com/live/%s/tokens' % video_id, | |
271 | display_id, note='Downloading HLS token JSON', data=b'') | |
272 | formats.extend(self._extract_m3u8_formats( | |
273 | stream_meta['stream_token_url'] + '?' + token_json.get('token', ''), | |
274 | display_id, 'mp4', 'm3u8_native')) | |
275 | if stream_meta.get('stream_dash_url'): | |
276 | pass | |
277 | if stream_meta.get('stream_url'): | |
278 | formats.extend(self._extract_m3u8_formats( | |
279 | stream_meta['stream_url'], display_id, 'mp4', 'm3u8_native')) | |
280 | self._sort_formats(formats) | |
281 | ||
282 | return { | |
283 | 'id': video_id, | |
284 | 'display_id': display_id, | |
285 | 'title': title, | |
286 | 'is_live': True, | |
287 | 'description': strip_or_none(stream_meta.get('description')), | |
288 | 'thumbnail': stream_meta.get('image'), | |
289 | 'like_count': int_or_none(stream_meta.get('like')), | |
290 | 'dislike_count': int_or_none(stream_meta.get('dislike')), | |
291 | 'formats': formats, | |
292 | 'uploader': user.get('name'), | |
293 | 'timestamp': parse_iso8601(stream_meta.get('start_time')), | |
294 | 'uploader_id': username, | |
295 | 'uploader_url': 'https://www.vidio.com/@' + username if username else None, | |
296 | } |