2 from __future__ import unicode_literals
16 from ..compat import compat_urllib_request
17 from .common import InfoExtractor
20 class VikiBaseIE(InfoExtractor):
21 _VALID_URL_BASE = r'https?://(?:www\.)?viki\.(?:com|net|mx|jp|fr)/'
22 _API_QUERY_TEMPLATE = '/v4/%sapp=%s&t=%s&site=www.viki.com'
23 _API_URL_TEMPLATE = 'http://api.viki.io%s&sig=%s'
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)'
29 _NETRC_MACHINE = 'viki'
33 def _prepare_call(self, path, timestamp=None, post_data=None):
34 path += '?' if '?' not in path else '&'
36 timestamp = int(time.time())
37 query = self._API_QUERY_TEMPLATE % (path, self._APP, timestamp)
39 query += '&token=%s' % self._token
41 self._APP_SECRET.encode('ascii'),
42 query.encode('ascii'),
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
49 def _call_api(self, path, video_id, note, timestamp=None, post_data=None):
50 resp = self._download_json(
51 self._prepare_call(path, timestamp, post_data), video_id, note)
53 error = resp.get('error')
55 if error == 'invalid timestamp':
56 resp = self._download_json(
57 self._prepare_call(path, int(resp['current_timestamp']), post_data),
58 video_id, '%s (retry)' % note)
59 error = resp.get('error')
61 self._raise_error(resp['error'])
65 def _raise_error(self, error):
67 '%s returned error: %s' % (self.IE_NAME, error),
70 def _real_initialize(self):
74 (username, password) = self._get_login_info()
83 login = self._call_api(
84 'sessions.json', None,
85 'Logging in as %s' % username, post_data=login_form)
87 self._token = login.get('token')
89 self.report_warning('Unable to get session token, login has probably failed')
92 class VikiIE(VikiBaseIE):
94 _VALID_URL = r'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE._VALID_URL_BASE
96 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
100 'title': 'Heirs Episode 14',
102 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
103 'upload_date': '20131121',
106 'skip': 'Blocked in the US',
109 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
110 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
114 'title': "'The Avengers: Age of Ultron' Press Conference",
115 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
117 'timestamp': 1430380829,
118 'upload_date': '20150430',
119 'uploader': 'Arirang TV',
124 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
128 'title': 'Ankhon Dekhi',
130 'timestamp': 1408532356,
131 'upload_date': '20140820',
138 'skip_download': True,
142 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
143 'md5': '190f3ef426005ba3a080a63325955bc3',
147 'title': 'Boys Over Flowers - Episode 1',
148 'description': 'md5:52617e4f729c7d03bfd4bcbbb6e946f2',
150 'timestamp': 1270496524,
151 'upload_date': '20100405',
152 'uploader': 'group8',
158 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1',
159 'md5': '216d1afdc0c64d1febc1e9f2bd4b864b',
163 'title': 'Poor Nastya [COMPLETE] - Episode 1',
166 'timestamp': 1274949505,
167 'upload_date': '20101213',
168 'uploader': 'ad14065n',
169 'uploader_id': 'ad14065n',
174 'url': 'http://www.viki.com/player/44699v',
175 'only_matching': True,
178 def _real_extract(self, url):
179 video_id = self._match_id(url)
181 video = self._call_api(
182 'videos/%s.json' % video_id, video_id, 'Downloading video JSON')
185 titles = video.get('titles')
187 title = titles.get('en') or titles[titles.keys()[0]]
189 title = 'Episode %d' % video.get('number') if video.get('type') == 'episode' else video.get('id') or video_id
190 container_titles = video.get('container', {}).get('titles')
192 container_title = container_titles.get('en') or container_titles[container_titles.keys()[0]]
193 title = '%s - %s' % (container_title, title)
195 descriptions = video.get('descriptions')
196 description = descriptions.get('en') or descriptions[titles.keys()[0]] if descriptions else None
198 duration = int_or_none(video.get('duration'))
199 timestamp = parse_iso8601(video.get('created_at'))
200 uploader = video.get('author')
201 like_count = int_or_none(video.get('likes', {}).get('count'))
202 age_limit = parse_age_limit(video.get('rating'))
205 for thumbnail_id, thumbnail in video.get('images', {}).items():
208 'url': thumbnail.get('url'),
212 for subtitle_lang, _ in video.get('subtitle_completions', {}).items():
213 subtitles[subtitle_lang] = [{
214 'ext': subtitles_format,
215 'url': self._prepare_call(
216 'videos/%s/subtitles/%s.%s' % (video_id, subtitle_lang, subtitles_format)),
217 } for subtitles_format in ('srt', 'vtt')]
222 'description': description,
223 'duration': duration,
224 'timestamp': timestamp,
225 'uploader': uploader,
226 'like_count': like_count,
227 'age_limit': age_limit,
228 'thumbnails': thumbnails,
229 'subtitles': subtitles,
232 streams = self._call_api(
233 'videos/%s/streams.json' % video_id, video_id,
234 'Downloading video streams JSON')
236 if 'external' in streams:
238 '_type': 'url_transparent',
239 'url': streams['external']['url'],
244 for format_id, stream_dict in streams.items():
245 height = self._search_regex(
246 r'^(\d+)[pP]$', format_id, 'height', default=None)
247 for protocol, format_dict in stream_dict.items():
248 if format_id == 'm3u8':
249 formats = self._extract_m3u8_formats(
250 format_dict['url'], video_id, 'mp4', m3u8_id='m3u8-%s' % protocol)
253 'url': format_dict['url'],
254 'format_id': '%s-%s' % (format_id, protocol),
257 self._sort_formats(formats)
259 result['formats'] = formats
263 class VikiChannelIE(VikiBaseIE):
264 IE_NAME = 'viki:channel'
265 _VALID_URL = r'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE._VALID_URL_BASE
267 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
270 'title': 'Boys Over Flowers',
271 'description': 'md5:ecd3cff47967fe193cff37c0bec52790',
273 'playlist_count': 70,
275 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
278 'title': 'Poor Nastya [COMPLETE]',
279 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
281 'playlist_count': 127,
283 'url': 'http://www.viki.com/news/24569c-showbiz-korea',
284 'only_matching': True,
286 'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005',
287 'only_matching': True,
289 'url': 'http://www.viki.com/artists/2141c-shinee',
290 'only_matching': True,
295 def _real_extract(self, url):
296 channel_id = self._match_id(url)
298 channel = self._call_api(
299 'containers/%s.json' % channel_id, channel_id,
300 'Downloading channel JSON')
302 titles = channel['titles']
303 title = titles.get('en') or titles[titles.keys()[0]]
305 descriptions = channel['descriptions']
306 description = descriptions.get('en') or descriptions[descriptions.keys()[0]]
309 for video_type in ('episodes', 'clips', 'movies'):
310 for page_num in itertools.count(1):
311 page = self._call_api(
312 'containers/%s/%s.json?per_page=%d&sort=number&direction=asc&with_paging=true&page=%d'
313 % (channel_id, video_type, self._PER_PAGE, page_num), channel_id,
314 'Downloading %s JSON page #%d' % (video_type, page_num))
315 for video in page['response']:
316 video_id = video['id']
317 entries.append(self.url_result(
318 'http://www.viki.com/videos/%s' % video_id, 'Viki'))
319 if not page['pagination']['next']:
322 return self.playlist_result(entries, channel_id, title, description)