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