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'
31 def _prepare_call(self, path, timestamp=None, post_data=None):
32 path += '?' if '?' not in path else '&'
34 timestamp = int(time.time())
35 query = self._API_QUERY_TEMPLATE % (path, self._APP, timestamp)
37 self._APP_SECRET.encode('ascii'),
38 query.encode('ascii'),
41 url = self._API_URL_TEMPLATE % (query, sig)
42 return compat_urllib_request.Request(
43 url, json.dumps(post_data).encode('utf-8')) if post_data else url
45 def _call_api(self, path, video_id, note, timestamp=None, post_data=None):
46 resp = self._download_json(
47 self._prepare_call(path, timestamp, post_data), video_id, note)
49 error = resp.get('error')
51 if error == 'invalid timestamp':
52 resp = self._download_json(
53 self._prepare_call(path, int(resp['current_timestamp']), post_data),
54 video_id, '%s (retry)' % note)
55 error = resp.get('error')
57 self._raise_error(resp['error'])
61 def _raise_error(self, error):
63 '%s returned error: %s' % (self.IE_NAME, error),
66 def _real_initialize(self):
70 (username, password) = self._get_login_info()
80 'sessions.json', None,
81 'Logging in as %s' % username, post_data=login_form)
84 class VikiIE(VikiBaseIE):
86 _VALID_URL = r'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE._VALID_URL_BASE
88 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
92 'title': 'Heirs Episode 14',
94 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
95 'upload_date': '20131121',
98 'skip': 'Blocked in the US',
101 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
102 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
106 'title': "'The Avengers: Age of Ultron' Press Conference",
107 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
109 'timestamp': 1430380829,
110 'upload_date': '20150430',
111 'uploader': 'Arirang TV',
116 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
120 'title': 'Ankhon Dekhi',
122 'timestamp': 1408532356,
123 'upload_date': '20140820',
130 'skip_download': True,
134 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
135 'md5': '190f3ef426005ba3a080a63325955bc3',
139 'title': 'Boys Over Flowers - Episode 1',
140 'description': 'md5:52617e4f729c7d03bfd4bcbbb6e946f2',
142 'timestamp': 1270496524,
143 'upload_date': '20100405',
144 'uploader': 'group8',
150 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1',
151 'md5': '216d1afdc0c64d1febc1e9f2bd4b864b',
155 'title': 'Poor Nastya [COMPLETE] - Episode 1',
158 'timestamp': 1274949505,
159 'upload_date': '20101213',
160 'uploader': 'ad14065n',
161 'uploader_id': 'ad14065n',
166 'url': 'http://www.viki.com/player/44699v',
167 'only_matching': True,
170 def _real_extract(self, url):
171 video_id = self._match_id(url)
173 video = self._call_api(
174 'videos/%s.json' % video_id, video_id, 'Downloading video JSON')
177 titles = video.get('titles')
179 title = titles.get('en') or titles[titles.keys()[0]]
181 title = 'Episode %d' % video.get('number') if video.get('type') == 'episode' else video.get('id') or video_id
182 container_titles = video.get('container', {}).get('titles')
184 container_title = container_titles.get('en') or container_titles[container_titles.keys()[0]]
185 title = '%s - %s' % (container_title, title)
187 descriptions = video.get('descriptions')
188 description = descriptions.get('en') or descriptions[titles.keys()[0]] if descriptions else None
190 duration = int_or_none(video.get('duration'))
191 timestamp = parse_iso8601(video.get('created_at'))
192 uploader = video.get('author')
193 like_count = int_or_none(video.get('likes', {}).get('count'))
194 age_limit = parse_age_limit(video.get('rating'))
197 for thumbnail_id, thumbnail in video.get('images', {}).items():
200 'url': thumbnail.get('url'),
204 for subtitle_lang, _ in video.get('subtitle_completions', {}).items():
205 subtitles[subtitle_lang] = [{
206 'ext': subtitles_format,
207 'url': self._prepare_call(
208 'videos/%s/subtitles/%s.%s' % (video_id, subtitle_lang, subtitles_format)),
209 } for subtitles_format in ('srt', 'vtt')]
214 'description': description,
215 'duration': duration,
216 'timestamp': timestamp,
217 'uploader': uploader,
218 'like_count': like_count,
219 'age_limit': age_limit,
220 'thumbnails': thumbnails,
221 'subtitles': subtitles,
224 streams = self._call_api(
225 'videos/%s/streams.json' % video_id, video_id,
226 'Downloading video streams JSON')
228 if 'external' in streams:
230 '_type': 'url_transparent',
231 'url': streams['external']['url'],
236 for format_id, stream_dict in streams.items():
237 height = self._search_regex(
238 r'^(\d+)[pP]$', format_id, 'height', default=None)
239 for protocol, format_dict in stream_dict.items():
240 if format_id == 'm3u8':
241 formats = self._extract_m3u8_formats(
242 format_dict['url'], video_id, 'mp4', m3u8_id='m3u8-%s' % protocol)
245 'url': format_dict['url'],
246 'format_id': '%s-%s' % (format_id, protocol),
249 self._sort_formats(formats)
251 result['formats'] = formats
255 class VikiChannelIE(VikiBaseIE):
256 IE_NAME = 'viki:channel'
257 _VALID_URL = r'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE._VALID_URL_BASE
259 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
262 'title': 'Boys Over Flowers',
263 'description': 'md5:ecd3cff47967fe193cff37c0bec52790',
265 'playlist_count': 70,
267 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
270 'title': 'Poor Nastya [COMPLETE]',
271 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
273 'playlist_count': 127,
275 'url': 'http://www.viki.com/news/24569c-showbiz-korea',
276 'only_matching': True,
278 'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005',
279 'only_matching': True,
281 'url': 'http://www.viki.com/artists/2141c-shinee',
282 'only_matching': True,
287 def _real_extract(self, url):
288 channel_id = self._match_id(url)
290 channel = self._call_api(
291 'containers/%s.json' % channel_id, channel_id,
292 'Downloading channel JSON')
294 titles = channel['titles']
295 title = titles.get('en') or titles[titles.keys()[0]]
297 descriptions = channel['descriptions']
298 description = descriptions.get('en') or descriptions[descriptions.keys()[0]]
301 for video_type in ('episodes', 'clips', 'movies'):
302 for page_num in itertools.count(1):
303 page = self._call_api(
304 'containers/%s/%s.json?per_page=%d&sort=number&direction=asc&with_paging=true&page=%d'
305 % (channel_id, video_type, self._PER_PAGE, page_num), channel_id,
306 'Downloading %s JSON page #%d' % (video_type, page_num))
307 for video in page['response']:
308 video_id = video['id']
309 entries.append(self.url_result(
310 'http://www.viki.com/videos/%s' % video_id, 'Viki'))
311 if not page['pagination']['next']:
314 return self.playlist_result(entries, channel_id, title, description)