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