1 from __future__ import unicode_literals
14 from .common import InfoExtractor
17 class VikiBaseIE(InfoExtractor):
18 _API_QUERY_TEMPLATE = '/v4/%sapp=%s&t=%s&site=www.viki.com'
19 _API_URL_TEMPLATE = 'http://api.viki.io%s&sig=%s'
22 _APP_VERSION = '2.2.5.1428709186'
23 _APP_SECRET = '-$iJ}@p7!G@SyU/je1bEyWg}upLu-6V6-Lg9VD(]siH,r.,m-r|ulZ,U4LC/SeR)'
25 def _prepare_call(self, path, timestamp=None):
26 path += '?' if '?' not in path else '&'
28 timestamp = int(time.time())
29 query = self._API_QUERY_TEMPLATE % (path, self._APP, timestamp)
31 self._APP_SECRET.encode('ascii'),
32 query.encode('ascii'),
35 return self._API_URL_TEMPLATE % (query, sig)
37 def _call_api(self, path, video_id, note, timestamp=None):
38 resp = self._download_json(
39 self._prepare_call(path, timestamp), video_id, note)
41 error = resp.get('error')
43 if error == 'invalid timestamp':
44 resp = self._download_json(
45 self._prepare_call(path, int(resp['current_timestamp'])),
46 video_id, '%s (retry)' % note)
47 error = resp.get('error')
49 self._raise_error(resp['error'])
53 def _raise_error(self, error):
55 '%s returned error: %s' % (self.IE_NAME, error),
59 class VikiIE(VikiBaseIE):
61 _VALID_URL = r'https?://(?:www\.)?viki\.com/(?:videos|player)/(?P<id>[0-9]+v)'
63 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
67 'title': 'Heirs Episode 14',
69 'description': 'md5:c4b17b9626dd4b143dcc4d855ba3474e',
70 'upload_date': '20131121',
73 'skip': 'Blocked in the US',
76 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
77 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
81 'title': "'The Avengers: Age of Ultron' Press Conference",
82 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
84 'timestamp': 1430380829,
85 'upload_date': '20150430',
86 'uploader': 'Arirang TV',
91 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
95 'title': 'Ankhon Dekhi',
97 'timestamp': 1408532356,
98 'upload_date': '20140820',
105 'skip_download': True,
109 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
110 'md5': '190f3ef426005ba3a080a63325955bc3',
114 'title': 'Boys Over Flowers - Episode 1',
115 'description': 'md5:52617e4f729c7d03bfd4bcbbb6e946f2',
117 'timestamp': 1270496524,
118 'upload_date': '20100405',
119 'uploader': 'group8',
124 'url': 'http://www.viki.com/player/44699v',
125 'only_matching': True,
128 def _real_extract(self, url):
129 video_id = self._match_id(url)
131 streams = self._call_api(
132 'videos/%s/streams.json' % video_id, video_id,
133 'Downloading video streams JSON')
136 for format_id, stream_dict in streams.items():
137 height = self._search_regex(
138 r'^(\d+)[pP]$', format_id, 'height', default=None)
139 for protocol, format_dict in stream_dict.items():
140 if format_id == 'm3u8':
141 formats = self._extract_m3u8_formats(
142 format_dict['url'], video_id, 'mp4', m3u8_id='m3u8-%s' % protocol)
145 'url': format_dict['url'],
146 'format_id': '%s-%s' % (format_id, protocol),
149 self._sort_formats(formats)
151 video = self._call_api(
152 'videos/%s.json' % video_id, video_id, 'Downloading video JSON')
155 titles = video.get('titles')
157 title = titles.get('en') or titles[titles.keys()[0]]
159 title = 'Episode %d' % video.get('number') if video.get('type') == 'episode' else video.get('id') or video_id
160 container_titles = video.get('container', {}).get('titles')
162 container_title = container_titles.get('en') or container_titles[titles.keys()[0]]
163 title = '%s - %s' % (container_title, title)
165 descriptions = video.get('descriptions')
166 description = descriptions.get('en') or descriptions[titles.keys()[0]] if descriptions else None
168 duration = int_or_none(video.get('duration'))
169 timestamp = parse_iso8601(video.get('created_at'))
170 uploader = video.get('author')
171 like_count = int_or_none(video.get('likes', {}).get('count'))
172 age_limit = parse_age_limit(video.get('rating'))
175 for thumbnail_id, thumbnail in video.get('images', {}).items():
178 'url': thumbnail.get('url'),
182 for subtitle_lang, _ in video.get('subtitle_completions', {}).items():
183 subtitles[subtitle_lang] = [{
184 'ext': subtitles_format,
185 'url': self._prepare_call(
186 'videos/%s/subtitles/%s.%s' % (video_id, subtitle_lang, subtitles_format)),
187 } for subtitles_format in ('srt', 'vtt')]
192 'description': description,
193 'duration': duration,
194 'timestamp': timestamp,
195 'uploader': uploader,
196 'like_count': like_count,
197 'age_limit': age_limit,
198 'thumbnails': thumbnails,
200 'subtitles': subtitles,
204 class VikiChannelIE(InfoExtractor):
205 IE_NAME = 'viki:channel'
206 _VALID_URL = r'https?://(?:www\.)?viki\.com/tv/(?P<id>[0-9]+c)'
208 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
211 'title': 'Boys Over Flowers',
212 'description': 'md5:ecd3cff47967fe193cff37c0bec52790',
214 'playlist_count': 70,
216 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
219 'title': 'Poor Nastya [COMPLETE]',
220 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
222 'playlist_count': 127,
224 _API_BASE = 'http://api.viki.io/v4/containers'
228 def _real_extract(self, url):
229 channel_id = self._match_id(url)
231 channel = self._download_json(
232 '%s/%s.json?app=%s' % (self._API_BASE, channel_id, self._APP),
233 channel_id, 'Downloading channel JSON')
235 titles = channel['titles']
236 title = titles.get('en') or titles[titles.keys()[0]]
238 descriptions = channel['descriptions']
239 description = descriptions.get('en') or descriptions[descriptions.keys()[0]]
242 for video_type in ('episodes', 'clips'):
243 page_url = '%s/%s/%s.json?app=%s&per_page=%d&sort=number&direction=asc&with_paging=true&page=1' % (self._API_BASE, channel_id, video_type, self._APP, self._PER_PAGE)
245 page = self._download_json(
246 page_url, channel_id,
247 'Downloading %s JSON page #%s'
248 % (video_type, re.search(r'[?&]page=([0-9]+)', page_url).group(1)))
249 for video in page['response']:
250 video_id = video['id']
251 entries.append(self.url_result(
252 'http://www.viki.com/videos/%s' % video_id, 'Viki', video_id))
253 page_url = page['pagination']['next']
255 return self.playlist_result(entries, channel_id, title, description)