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