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