]>
Commit | Line | Data |
---|---|---|
accf79b1 | 1 | # coding: utf-8 |
cb9722cb PH |
2 | from __future__ import unicode_literals |
3 | ||
a0566bbf | 4 | import base64 |
1a83c731 | 5 | import hashlib |
9338a0ea | 6 | import hmac |
bc56355e | 7 | import itertools |
9338a0ea S |
8 | import json |
9 | import re | |
10 | import time | |
382ed50e | 11 | |
5c2266df | 12 | from .common import InfoExtractor |
a0566bbf | 13 | from ..compat import ( |
14 | compat_parse_qs, | |
15 | compat_urllib_parse_urlparse, | |
16 | ) | |
382ed50e | 17 | from ..utils import ( |
6d88bc37 | 18 | ExtractorError, |
1a83c731 | 19 | int_or_none, |
169bd46b | 20 | HEADRequest, |
1a83c731 S |
21 | parse_age_limit, |
22 | parse_iso8601, | |
5c2266df | 23 | sanitized_Request, |
38d70284 | 24 | std_headers, |
bc2ca1bb | 25 | try_get, |
382ed50e | 26 | ) |
382ed50e PH |
27 | |
28 | ||
1a83c731 | 29 | class VikiBaseIE(InfoExtractor): |
53de95da | 30 | _VALID_URL_BASE = r'https?://(?:www\.)?viki\.(?:com|net|mx|jp|fr)/' |
1a83c731 | 31 | _API_QUERY_TEMPLATE = '/v4/%sapp=%s&t=%s&site=www.viki.com' |
26a87972 | 32 | _API_URL_TEMPLATE = 'https://api.viki.io%s&sig=%s' |
1a83c731 | 33 | |
8251af63 | 34 | _APP = '100005a' |
5e41dca3 | 35 | _APP_VERSION = '6.0.0' |
8251af63 | 36 | _APP_SECRET = 'MM_d*yP@`&1@]@!AVrXf_o-HVEnoTnm$O-ti4[G~$JDI/Dc-&piU&z&5.;:}95=Iad' |
1a83c731 | 37 | |
4248dad9 | 38 | _GEO_BYPASS = False |
accf79b1 S |
39 | _NETRC_MACHINE = 'viki' |
40 | ||
16d6973f S |
41 | _token = None |
42 | ||
dc016bf5 | 43 | _ERRORS = { |
44 | 'geo': 'Sorry, this content is not available in your region.', | |
45 | 'upcoming': 'Sorry, this content is not yet available.', | |
bc2ca1bb | 46 | 'paywall': 'Sorry, this content is only available to Viki Pass Plus subscribers', |
dc016bf5 | 47 | } |
48 | ||
accf79b1 | 49 | def _prepare_call(self, path, timestamp=None, post_data=None): |
1a83c731 S |
50 | path += '?' if '?' not in path else '&' |
51 | if not timestamp: | |
52 | timestamp = int(time.time()) | |
53 | query = self._API_QUERY_TEMPLATE % (path, self._APP, timestamp) | |
16d6973f S |
54 | if self._token: |
55 | query += '&token=%s' % self._token | |
1a83c731 S |
56 | sig = hmac.new( |
57 | self._APP_SECRET.encode('ascii'), | |
58 | query.encode('ascii'), | |
59 | hashlib.sha1 | |
60 | ).hexdigest() | |
accf79b1 | 61 | url = self._API_URL_TEMPLATE % (query, sig) |
5c2266df | 62 | return sanitized_Request( |
accf79b1 | 63 | url, json.dumps(post_data).encode('utf-8')) if post_data else url |
1a83c731 | 64 | |
accf79b1 | 65 | def _call_api(self, path, video_id, note, timestamp=None, post_data=None): |
1a83c731 | 66 | resp = self._download_json( |
5e41dca3 | 67 | self._prepare_call(path, timestamp, post_data), |
68 | video_id, note, | |
69 | headers={ | |
70 | 'x-client-user-agent': std_headers['User-Agent'], | |
71 | 'x-viki-as-id': self._APP, | |
72 | 'x-viki-app-ver': self._APP_VERSION, | |
73 | }) | |
1a83c731 S |
74 | |
75 | error = resp.get('error') | |
76 | if error: | |
77 | if error == 'invalid timestamp': | |
78 | resp = self._download_json( | |
accf79b1 | 79 | self._prepare_call(path, int(resp['current_timestamp']), post_data), |
5e41dca3 | 80 | video_id, '%s (retry)' % note, |
81 | headers={ | |
82 | 'x-client-user-agent': std_headers['User-Agent'], | |
83 | 'x-viki-as-id': self._APP, | |
84 | 'x-viki-app-ver': self._APP_VERSION, | |
85 | }) | |
1a83c731 S |
86 | error = resp.get('error') |
87 | if error: | |
88 | self._raise_error(resp['error']) | |
89 | ||
90 | return resp | |
91 | ||
92 | def _raise_error(self, error): | |
93 | raise ExtractorError( | |
94 | '%s returned error: %s' % (self.IE_NAME, error), | |
95 | expected=True) | |
96 | ||
dc016bf5 | 97 | def _check_errors(self, data): |
bc2ca1bb | 98 | for reason, status in (data.get('blocking') or {}).items(): |
dc016bf5 | 99 | if status and reason in self._ERRORS: |
5d3fbf77 S |
100 | message = self._ERRORS[reason] |
101 | if reason == 'geo': | |
102 | self.raise_geo_restricted(msg=message) | |
bc2ca1bb | 103 | elif reason == 'paywall': |
104 | self.raise_login_required(message) | |
dc016bf5 | 105 | raise ExtractorError('%s said: %s' % ( |
5d3fbf77 | 106 | self.IE_NAME, message), expected=True) |
dc016bf5 | 107 | |
accf79b1 S |
108 | def _real_initialize(self): |
109 | self._login() | |
110 | ||
111 | def _login(self): | |
68217024 | 112 | username, password = self._get_login_info() |
accf79b1 S |
113 | if username is None: |
114 | return | |
115 | ||
116 | login_form = { | |
117 | 'login_id': username, | |
118 | 'password': password, | |
119 | } | |
120 | ||
16d6973f | 121 | login = self._call_api( |
accf79b1 | 122 | 'sessions.json', None, |
e4d95865 | 123 | 'Logging in', post_data=login_form) |
accf79b1 | 124 | |
16d6973f S |
125 | self._token = login.get('token') |
126 | if not self._token: | |
127 | self.report_warning('Unable to get session token, login has probably failed') | |
128 | ||
b73b14f7 | 129 | @staticmethod |
6d28c408 | 130 | def dict_selection(dict_obj, preferred_key, allow_fallback=True): |
b73b14f7 YCH |
131 | if preferred_key in dict_obj: |
132 | return dict_obj.get(preferred_key) | |
133 | ||
6d28c408 YCH |
134 | if not allow_fallback: |
135 | return | |
136 | ||
b73b14f7 YCH |
137 | filtered_dict = list(filter(None, [dict_obj.get(k) for k in dict_obj.keys()])) |
138 | return filtered_dict[0] if filtered_dict else None | |
139 | ||
1a83c731 S |
140 | |
141 | class VikiIE(VikiBaseIE): | |
cb9722cb | 142 | IE_NAME = 'viki' |
53de95da | 143 | _VALID_URL = r'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE._VALID_URL_BASE |
8e3df9df | 144 | _TESTS = [{ |
cdb19aa4 | 145 | 'note': 'Free non-DRM video with storyboards in MPD', |
89ee4cf8 | 146 | 'url': 'https://www.viki.com/videos/1175236v-choosing-spouse-by-lottery-episode-1', |
147 | 'info_dict': { | |
148 | 'id': '1175236v', | |
149 | 'ext': 'mp4', | |
150 | 'title': 'Choosing Spouse by Lottery - Episode 1', | |
151 | 'timestamp': 1606463239, | |
152 | 'age_limit': 13, | |
153 | 'uploader': 'FCC', | |
154 | 'upload_date': '20201127', | |
155 | }, | |
156 | 'params': { | |
157 | 'format': 'bestvideo', | |
158 | }, | |
89ee4cf8 | 159 | }, { |
cb9722cb | 160 | 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14', |
cb9722cb PH |
161 | 'info_dict': { |
162 | 'id': '1023585v', | |
163 | 'ext': 'mp4', | |
bc2ca1bb | 164 | 'title': 'Heirs - Episode 14', |
165 | 'uploader': 'SBS Contents Hub', | |
166 | 'timestamp': 1385047627, | |
cb9722cb PH |
167 | 'upload_date': '20131121', |
168 | 'age_limit': 13, | |
bc2ca1bb | 169 | 'duration': 3570, |
170 | 'episode_number': 14, | |
171 | }, | |
172 | 'params': { | |
173 | 'format': 'bestvideo', | |
6d88bc37 | 174 | }, |
cb9722cb | 175 | 'skip': 'Blocked in the US', |
8e3df9df | 176 | }, { |
1a83c731 | 177 | # clip |
8e3df9df | 178 | 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference', |
10568217 | 179 | 'md5': '86c0b5dbd4d83a6611a79987cc7a1989', |
8e3df9df YCH |
180 | 'info_dict': { |
181 | 'id': '1067139v', | |
182 | 'ext': 'mp4', | |
1a83c731 | 183 | 'title': "'The Avengers: Age of Ultron' Press Conference", |
8e3df9df | 184 | 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea', |
1a83c731 S |
185 | 'duration': 352, |
186 | 'timestamp': 1430380829, | |
8e3df9df | 187 | 'upload_date': '20150430', |
1a83c731 S |
188 | 'uploader': 'Arirang TV', |
189 | 'like_count': int, | |
190 | 'age_limit': 0, | |
bc2ca1bb | 191 | }, |
192 | 'skip': 'Sorry. There was an error loading this video', | |
d948e09b YCH |
193 | }, { |
194 | 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi', | |
195 | 'info_dict': { | |
196 | 'id': '1048879v', | |
197 | 'ext': 'mp4', | |
d948e09b | 198 | 'title': 'Ankhon Dekhi', |
1a83c731 S |
199 | 'duration': 6512, |
200 | 'timestamp': 1408532356, | |
201 | 'upload_date': '20140820', | |
202 | 'uploader': 'Spuul', | |
203 | 'like_count': int, | |
204 | 'age_limit': 13, | |
d948e09b | 205 | }, |
94e5d6ae | 206 | 'skip': 'Blocked in the US', |
1a83c731 S |
207 | }, { |
208 | # episode | |
209 | 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1', | |
bc2ca1bb | 210 | 'md5': '0a53dc252e6e690feccd756861495a8c', |
1a83c731 S |
211 | 'info_dict': { |
212 | 'id': '44699v', | |
213 | 'ext': 'mp4', | |
214 | 'title': 'Boys Over Flowers - Episode 1', | |
c83b35d4 | 215 | 'description': 'md5:b89cf50038b480b88b5b3c93589a9076', |
a0566bbf | 216 | 'duration': 4172, |
1a83c731 S |
217 | 'timestamp': 1270496524, |
218 | 'upload_date': '20100405', | |
219 | 'uploader': 'group8', | |
220 | 'like_count': int, | |
221 | 'age_limit': 13, | |
bc2ca1bb | 222 | 'episode_number': 1, |
223 | }, | |
224 | 'params': { | |
225 | 'format': 'bestvideo', | |
a0566bbf | 226 | }, |
ac20d95f S |
227 | }, { |
228 | # youtube external | |
229 | 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1', | |
f22ba4bd | 230 | 'md5': '63f8600c1da6f01b7640eee7eca4f1da', |
ac20d95f S |
231 | 'info_dict': { |
232 | 'id': '50562v', | |
f22ba4bd | 233 | 'ext': 'webm', |
ac20d95f S |
234 | 'title': 'Poor Nastya [COMPLETE] - Episode 1', |
235 | 'description': '', | |
f22ba4bd | 236 | 'duration': 606, |
ac20d95f S |
237 | 'timestamp': 1274949505, |
238 | 'upload_date': '20101213', | |
239 | 'uploader': 'ad14065n', | |
240 | 'uploader_id': 'ad14065n', | |
241 | 'like_count': int, | |
242 | 'age_limit': 13, | |
a0566bbf | 243 | }, |
244 | 'skip': 'Page not found!', | |
1a83c731 S |
245 | }, { |
246 | 'url': 'http://www.viki.com/player/44699v', | |
247 | 'only_matching': True, | |
41597d9b YCH |
248 | }, { |
249 | # non-English description | |
250 | 'url': 'http://www.viki.com/videos/158036v-love-in-magic', | |
bc2ca1bb | 251 | 'md5': '41faaba0de90483fb4848952af7c7d0d', |
41597d9b YCH |
252 | 'info_dict': { |
253 | 'id': '158036v', | |
254 | 'ext': 'mp4', | |
255 | 'uploader': 'I Planet Entertainment', | |
256 | 'upload_date': '20111122', | |
257 | 'timestamp': 1321985454, | |
258 | 'description': 'md5:44b1e46619df3a072294645c770cef36', | |
259 | 'title': 'Love In Magic', | |
dc016bf5 | 260 | 'age_limit': 13, |
41597d9b | 261 | }, |
bc2ca1bb | 262 | 'params': { |
263 | 'format': 'bestvideo', | |
264 | }, | |
8e3df9df | 265 | }] |
382ed50e PH |
266 | |
267 | def _real_extract(self, url): | |
8ee34150 | 268 | video_id = self._match_id(url) |
382ed50e | 269 | |
89ee4cf8 | 270 | resp = self._download_json( |
271 | 'https://www.viki.com/api/videos/' + video_id, | |
272 | video_id, 'Downloading video JSON', headers={ | |
273 | 'x-client-user-agent': std_headers['User-Agent'], | |
274 | 'x-viki-app-ver': '3.0.0', | |
275 | }) | |
276 | video = resp['video'] | |
277 | ||
dc016bf5 | 278 | self._check_errors(video) |
279 | ||
6d28c408 | 280 | title = self.dict_selection(video.get('titles', {}), 'en', allow_fallback=False) |
bc2ca1bb | 281 | episode_number = int_or_none(video.get('number')) |
1a83c731 | 282 | if not title: |
bc2ca1bb | 283 | title = 'Episode %d' % episode_number if video.get('type') == 'episode' else video.get('id') or video_id |
284 | container_titles = try_get(video, lambda x: x['container']['titles'], dict) or {} | |
b73b14f7 YCH |
285 | container_title = self.dict_selection(container_titles, 'en') |
286 | title = '%s - %s' % (container_title, title) | |
287 | ||
288 | description = self.dict_selection(video.get('descriptions', {}), 'en') | |
1a83c731 | 289 | |
bc2ca1bb | 290 | like_count = int_or_none(try_get(video, lambda x: x['likes']['count'])) |
1a83c731 S |
291 | |
292 | thumbnails = [] | |
bc2ca1bb | 293 | for thumbnail_id, thumbnail in (video.get('images') or {}).items(): |
1a83c731 S |
294 | thumbnails.append({ |
295 | 'id': thumbnail_id, | |
296 | 'url': thumbnail.get('url'), | |
297 | }) | |
298 | ||
299 | subtitles = {} | |
31a5e037 RD |
300 | for subtitle_lang, _ in (video.get('subtitle_completions') or {}).items(): |
301 | subtitles[subtitle_lang] = [{ | |
302 | 'ext': subtitles_format, | |
303 | 'url': self._prepare_call( | |
304 | 'videos/%s/subtitles/%s.%s' % (video_id, subtitle_lang, subtitles_format)), | |
305 | } for subtitles_format in ('srt', 'vtt')] | |
382ed50e | 306 | |
ac20d95f | 307 | result = { |
382ed50e PH |
308 | 'id': video_id, |
309 | 'title': title, | |
382ed50e | 310 | 'description': description, |
bc2ca1bb | 311 | 'duration': int_or_none(video.get('duration')), |
312 | 'timestamp': parse_iso8601(video.get('created_at')), | |
313 | 'uploader': video.get('author'), | |
314 | 'uploader_url': video.get('author_url'), | |
1a83c731 | 315 | 'like_count': like_count, |
bc2ca1bb | 316 | 'age_limit': parse_age_limit(video.get('rating')), |
1a83c731 | 317 | 'thumbnails': thumbnails, |
1a83c731 | 318 | 'subtitles': subtitles, |
bc2ca1bb | 319 | 'episode_number': episode_number, |
382ed50e PH |
320 | } |
321 | ||
a0566bbf | 322 | formats = [] |
323 | ||
324 | def add_format(format_id, format_dict, protocol='http'): | |
325 | # rtmps URLs does not seem to work | |
326 | if protocol == 'rtmps': | |
327 | return | |
328 | format_url = format_dict.get('url') | |
329 | if not format_url: | |
330 | return | |
a0566bbf | 331 | qs = compat_parse_qs(compat_urllib_parse_urlparse(format_url).query) |
332 | stream = qs.get('stream', [None])[0] | |
333 | if stream: | |
334 | format_url = base64.b64decode(stream).decode() | |
335 | if format_id in ('m3u8', 'hls'): | |
336 | m3u8_formats = self._extract_m3u8_formats( | |
337 | format_url, video_id, 'mp4', | |
338 | entry_protocol='m3u8_native', | |
339 | m3u8_id='m3u8-%s' % protocol, fatal=False) | |
340 | # Despite CODECS metadata in m3u8 all video-only formats | |
341 | # are actually video+audio | |
342 | for f in m3u8_formats: | |
a06916d9 | 343 | if not self.get_param('allow_unplayable_formats') and '_drm/index_' in f['url']: |
a0566bbf | 344 | continue |
345 | if f.get('acodec') == 'none' and f.get('vcodec') != 'none': | |
346 | f['acodec'] = None | |
347 | formats.append(f) | |
348 | elif format_id in ('mpd', 'dash'): | |
349 | formats.extend(self._extract_mpd_formats( | |
350 | format_url, video_id, 'mpd-%s' % protocol, fatal=False)) | |
351 | elif format_url.startswith('rtmp'): | |
352 | mobj = re.search( | |
353 | r'^(?P<url>rtmp://[^/]+/(?P<app>.+?))/(?P<playpath>mp4:.+)$', | |
354 | format_url) | |
355 | if not mobj: | |
356 | return | |
357 | formats.append({ | |
358 | 'format_id': 'rtmp-%s' % format_id, | |
359 | 'ext': 'flv', | |
360 | 'url': mobj.group('url'), | |
361 | 'play_path': mobj.group('playpath'), | |
362 | 'app': mobj.group('app'), | |
363 | 'page_url': url, | |
a0566bbf | 364 | }) |
365 | else: | |
366 | urlh = self._request_webpage( | |
367 | HEADRequest(format_url), video_id, 'Checking file size', fatal=False) | |
368 | formats.append({ | |
369 | 'url': format_url, | |
370 | 'format_id': '%s-%s' % (format_id, protocol), | |
371 | 'height': int_or_none(self._search_regex( | |
372 | r'^(\d+)[pP]$', format_id, 'height', default=None)), | |
a0566bbf | 373 | 'filesize': int_or_none(urlh.headers.get('Content-Length')), |
374 | }) | |
375 | ||
89ee4cf8 | 376 | for format_id, format_dict in (resp.get('streams') or {}).items(): |
377 | add_format(format_id, format_dict) | |
378 | if not formats: | |
379 | streams = self._call_api( | |
380 | 'videos/%s/streams.json' % video_id, video_id, | |
381 | 'Downloading video streams JSON') | |
382 | ||
383 | if 'external' in streams: | |
384 | result.update({ | |
385 | '_type': 'url_transparent', | |
386 | 'url': streams['external']['url'], | |
387 | }) | |
388 | return result | |
a0566bbf | 389 | |
89ee4cf8 | 390 | for format_id, stream_dict in streams.items(): |
391 | for protocol, format_dict in stream_dict.items(): | |
392 | add_format(format_id, format_dict, protocol) | |
a0566bbf | 393 | self._sort_formats(formats) |
ac20d95f | 394 | |
ac20d95f S |
395 | result['formats'] = formats |
396 | return result | |
397 | ||
0d7f0364 | 398 | |
bc56355e | 399 | class VikiChannelIE(VikiBaseIE): |
8da0e0e9 | 400 | IE_NAME = 'viki:channel' |
53de95da | 401 | _VALID_URL = r'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE._VALID_URL_BASE |
0d7f0364 | 402 | _TESTS = [{ |
403 | 'url': 'http://www.viki.com/tv/50c-boys-over-flowers', | |
404 | 'info_dict': { | |
405 | 'id': '50c', | |
406 | 'title': 'Boys Over Flowers', | |
bc2ca1bb | 407 | 'description': 'md5:804ce6e7837e1fd527ad2f25420f4d59', |
0d7f0364 | 408 | }, |
c83b35d4 | 409 | 'playlist_mincount': 71, |
1c18de00 | 410 | }, { |
411 | 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete', | |
412 | 'info_dict': { | |
413 | 'id': '1354c', | |
414 | 'title': 'Poor Nastya [COMPLETE]', | |
415 | 'description': 'md5:05bf5471385aa8b21c18ad450e350525', | |
416 | }, | |
417 | 'playlist_count': 127, | |
bc2ca1bb | 418 | 'skip': 'Page not found', |
d01924f4 S |
419 | }, { |
420 | 'url': 'http://www.viki.com/news/24569c-showbiz-korea', | |
421 | 'only_matching': True, | |
422 | }, { | |
423 | 'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005', | |
424 | 'only_matching': True, | |
425 | }, { | |
426 | 'url': 'http://www.viki.com/artists/2141c-shinee', | |
427 | 'only_matching': True, | |
0d7f0364 | 428 | }] |
bc56355e | 429 | |
8da0e0e9 | 430 | _PER_PAGE = 25 |
0d7f0364 | 431 | |
432 | def _real_extract(self, url): | |
b0d619fd | 433 | channel_id = self._match_id(url) |
0d7f0364 | 434 | |
bc56355e S |
435 | channel = self._call_api( |
436 | 'containers/%s.json' % channel_id, channel_id, | |
437 | 'Downloading channel JSON') | |
b0d619fd | 438 | |
dc016bf5 | 439 | self._check_errors(channel) |
440 | ||
b73b14f7 | 441 | title = self.dict_selection(channel['titles'], 'en') |
b0d619fd | 442 | |
b73b14f7 | 443 | description = self.dict_selection(channel['descriptions'], 'en') |
0d7f0364 | 444 | |
0d7f0364 | 445 | entries = [] |
d01924f4 | 446 | for video_type in ('episodes', 'clips', 'movies'): |
bc56355e S |
447 | for page_num in itertools.count(1): |
448 | page = self._call_api( | |
449 | 'containers/%s/%s.json?per_page=%d&sort=number&direction=asc&with_paging=true&page=%d' | |
450 | % (channel_id, video_type, self._PER_PAGE, page_num), channel_id, | |
451 | 'Downloading %s JSON page #%d' % (video_type, page_num)) | |
b0d619fd | 452 | for video in page['response']: |
1c18de00 | 453 | video_id = video['id'] |
454 | entries.append(self.url_result( | |
26a87972 | 455 | 'https://www.viki.com/videos/%s' % video_id, 'Viki')) |
bc56355e S |
456 | if not page['pagination']['next']: |
457 | break | |
0d7f0364 | 458 | |
b0d619fd | 459 | return self.playlist_result(entries, channel_id, title, description) |