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