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