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