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