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