]>
Commit | Line | Data |
---|---|---|
1 | # coding: utf-8 | |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import hashlib | |
5 | import hmac | |
6 | import itertools | |
7 | import json | |
8 | import re | |
9 | import time | |
10 | ||
11 | from .common import InfoExtractor | |
12 | from ..utils import ( | |
13 | ExtractorError, | |
14 | int_or_none, | |
15 | HEADRequest, | |
16 | parse_age_limit, | |
17 | parse_iso8601, | |
18 | sanitized_Request, | |
19 | ) | |
20 | ||
21 | ||
22 | class VikiBaseIE(InfoExtractor): | |
23 | _VALID_URL_BASE = r'https?://(?:www\.)?viki\.(?:com|net|mx|jp|fr)/' | |
24 | _API_QUERY_TEMPLATE = '/v4/%sapp=%s&t=%s&site=www.viki.com' | |
25 | _API_URL_TEMPLATE = 'https://api.viki.io%s&sig=%s' | |
26 | ||
27 | _APP = '100005a' | |
28 | _APP_VERSION = '2.2.5.1428709186' | |
29 | _APP_SECRET = 'MM_d*yP@`&1@]@!AVrXf_o-HVEnoTnm$O-ti4[G~$JDI/Dc-&piU&z&5.;:}95=Iad' | |
30 | ||
31 | _GEO_BYPASS = False | |
32 | _NETRC_MACHINE = 'viki' | |
33 | ||
34 | _token = None | |
35 | ||
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 | ||
42 | def _prepare_call(self, path, timestamp=None, post_data=None): | |
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) | |
47 | if self._token: | |
48 | query += '&token=%s' % self._token | |
49 | sig = hmac.new( | |
50 | self._APP_SECRET.encode('ascii'), | |
51 | query.encode('ascii'), | |
52 | hashlib.sha1 | |
53 | ).hexdigest() | |
54 | url = self._API_URL_TEMPLATE % (query, sig) | |
55 | return sanitized_Request( | |
56 | url, json.dumps(post_data).encode('utf-8')) if post_data else url | |
57 | ||
58 | def _call_api(self, path, video_id, note, timestamp=None, post_data=None): | |
59 | resp = self._download_json( | |
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]) | |
61 | ||
62 | error = resp.get('error') | |
63 | if error: | |
64 | if error == 'invalid timestamp': | |
65 | resp = self._download_json( | |
66 | self._prepare_call(path, int(resp['current_timestamp']), post_data), | |
67 | video_id, '%s (retry)' % note, headers={'x-viki-app-ver': '2.2.5.1428709186'}, expected_status=[200, 400, 404]) | |
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 | ||
79 | def _check_errors(self, data): | |
80 | for reason, status in data.get('blocking', {}).items(): | |
81 | if status and reason in self._ERRORS: | |
82 | message = self._ERRORS[reason] | |
83 | if reason == 'geo': | |
84 | self.raise_geo_restricted(msg=message) | |
85 | raise ExtractorError('%s said: %s' % ( | |
86 | self.IE_NAME, message), expected=True) | |
87 | ||
88 | def _real_initialize(self): | |
89 | self._login() | |
90 | ||
91 | def _login(self): | |
92 | username, password = self._get_login_info() | |
93 | if username is None: | |
94 | return | |
95 | ||
96 | login_form = { | |
97 | 'login_id': username, | |
98 | 'password': password, | |
99 | } | |
100 | ||
101 | login = self._call_api( | |
102 | 'sessions.json', None, | |
103 | 'Logging in', post_data=login_form) | |
104 | ||
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 | ||
109 | @staticmethod | |
110 | def dict_selection(dict_obj, preferred_key, allow_fallback=True): | |
111 | if preferred_key in dict_obj: | |
112 | return dict_obj.get(preferred_key) | |
113 | ||
114 | if not allow_fallback: | |
115 | return | |
116 | ||
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 | ||
120 | ||
121 | class VikiIE(VikiBaseIE): | |
122 | IE_NAME = 'viki' | |
123 | _VALID_URL = r'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE._VALID_URL_BASE | |
124 | _TESTS = [{ | |
125 | 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14', | |
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, | |
134 | }, | |
135 | 'skip': 'Blocked in the US', | |
136 | }, { | |
137 | # clip | |
138 | 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference', | |
139 | 'md5': '86c0b5dbd4d83a6611a79987cc7a1989', | |
140 | 'info_dict': { | |
141 | 'id': '1067139v', | |
142 | 'ext': 'mp4', | |
143 | 'title': "'The Avengers: Age of Ultron' Press Conference", | |
144 | 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea', | |
145 | 'duration': 352, | |
146 | 'timestamp': 1430380829, | |
147 | 'upload_date': '20150430', | |
148 | 'uploader': 'Arirang TV', | |
149 | 'like_count': int, | |
150 | 'age_limit': 0, | |
151 | } | |
152 | }, { | |
153 | 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi', | |
154 | 'info_dict': { | |
155 | 'id': '1048879v', | |
156 | 'ext': 'mp4', | |
157 | 'title': 'Ankhon Dekhi', | |
158 | 'duration': 6512, | |
159 | 'timestamp': 1408532356, | |
160 | 'upload_date': '20140820', | |
161 | 'uploader': 'Spuul', | |
162 | 'like_count': int, | |
163 | 'age_limit': 13, | |
164 | }, | |
165 | 'skip': 'Blocked in the US', | |
166 | }, { | |
167 | # episode | |
168 | 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1', | |
169 | 'md5': '5fa476a902e902783ac7a4d615cdbc7a', | |
170 | 'info_dict': { | |
171 | 'id': '44699v', | |
172 | 'ext': 'mp4', | |
173 | 'title': 'Boys Over Flowers - Episode 1', | |
174 | 'description': 'md5:b89cf50038b480b88b5b3c93589a9076', | |
175 | 'duration': 4204, | |
176 | 'timestamp': 1270496524, | |
177 | 'upload_date': '20100405', | |
178 | 'uploader': 'group8', | |
179 | 'like_count': int, | |
180 | 'age_limit': 13, | |
181 | } | |
182 | }, { | |
183 | # youtube external | |
184 | 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1', | |
185 | 'md5': '63f8600c1da6f01b7640eee7eca4f1da', | |
186 | 'info_dict': { | |
187 | 'id': '50562v', | |
188 | 'ext': 'webm', | |
189 | 'title': 'Poor Nastya [COMPLETE] - Episode 1', | |
190 | 'description': '', | |
191 | 'duration': 606, | |
192 | 'timestamp': 1274949505, | |
193 | 'upload_date': '20101213', | |
194 | 'uploader': 'ad14065n', | |
195 | 'uploader_id': 'ad14065n', | |
196 | 'like_count': int, | |
197 | 'age_limit': 13, | |
198 | } | |
199 | }, { | |
200 | 'url': 'http://www.viki.com/player/44699v', | |
201 | 'only_matching': True, | |
202 | }, { | |
203 | # non-English description | |
204 | 'url': 'http://www.viki.com/videos/158036v-love-in-magic', | |
205 | 'md5': '1713ae35df5a521b31f6dc40730e7c9c', | |
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', | |
214 | 'age_limit': 13, | |
215 | }, | |
216 | }] | |
217 | ||
218 | def _real_extract(self, url): | |
219 | video_id = self._match_id(url) | |
220 | ||
221 | video = self._call_api( | |
222 | 'videos/%s.json' % video_id, video_id, 'Downloading video JSON') | |
223 | ||
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( | |
276 | HEADRequest(format_url), video_id, 'Checking file size', fatal=False) | |
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 | ||
287 | self._check_errors(video) | |
288 | ||
289 | title = self.dict_selection(video.get('titles', {}), 'en', allow_fallback=False) | |
290 | if not title: | |
291 | title = 'Episode %d' % video.get('number') if video.get('type') == 'episode' else video.get('id') or video_id | |
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') | |
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 | ||
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]) | |
314 | ||
315 | subtitles = {} | |
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 | }] | |
322 | ||
323 | result = { | |
324 | 'id': video_id, | |
325 | 'title': title, | |
326 | 'description': description, | |
327 | 'duration': duration, | |
328 | 'timestamp': timestamp, | |
329 | 'uploader': uploader, | |
330 | 'like_count': like_count, | |
331 | 'age_limit': age_limit, | |
332 | 'thumbnails': thumbnails, | |
333 | 'subtitles': subtitles, | |
334 | } | |
335 | ||
336 | if 'external' in streams: | |
337 | result.update({ | |
338 | '_type': 'url_transparent', | |
339 | 'url': streams['external']['url'], | |
340 | }) | |
341 | return result | |
342 | ||
343 | result['formats'] = formats | |
344 | return result | |
345 | ||
346 | ||
347 | class VikiChannelIE(VikiBaseIE): | |
348 | IE_NAME = 'viki:channel' | |
349 | _VALID_URL = r'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE._VALID_URL_BASE | |
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 | }, | |
357 | 'playlist_mincount': 71, | |
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, | |
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, | |
375 | }] | |
376 | ||
377 | _PER_PAGE = 25 | |
378 | ||
379 | def _real_extract(self, url): | |
380 | channel_id = self._match_id(url) | |
381 | ||
382 | channel = self._call_api( | |
383 | 'containers/%s.json' % channel_id, channel_id, | |
384 | 'Downloading channel JSON') | |
385 | ||
386 | self._check_errors(channel) | |
387 | ||
388 | title = self.dict_selection(channel['titles'], 'en') | |
389 | ||
390 | description = self.dict_selection(channel['descriptions'], 'en') | |
391 | ||
392 | entries = [] | |
393 | for video_type in ('episodes', 'clips', 'movies'): | |
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)) | |
399 | for video in page['response']: | |
400 | video_id = video['id'] | |
401 | entries.append(self.url_result( | |
402 | 'https://www.viki.com/videos/%s' % video_id, 'Viki')) | |
403 | if not page['pagination']['next']: | |
404 | break | |
405 | ||
406 | return self.playlist_result(entries, channel_id, title, description) |