]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/viki.py
acb5ae550c8bd088bcb7a608d0710f56dcc1ec1a
[yt-dlp.git] / yt_dlp / extractor / viki.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3 import hashlib
4 import hmac
5 import json
6 import time
7
8 from .common import InfoExtractor
9 from ..utils import (
10 ExtractorError,
11 int_or_none,
12 parse_age_limit,
13 parse_iso8601,
14 try_get,
15 )
16
17
18 class VikiBaseIE(InfoExtractor):
19 _VALID_URL_BASE = r'https?://(?:www\.)?viki\.(?:com|net|mx|jp|fr)/'
20 _API_URL_TEMPLATE = 'https://api.viki.io%s'
21
22 _DEVICE_ID = '86085977d' # used for android api
23 _APP = '100005a'
24 _APP_VERSION = '6.11.3'
25 _APP_SECRET = 'd96704b180208dbb2efa30fe44c48bd8690441af9f567ba8fd710a72badc85198f7472'
26
27 _GEO_BYPASS = False
28 _NETRC_MACHINE = 'viki'
29
30 _token = None
31
32 _ERRORS = {
33 'geo': 'Sorry, this content is not available in your region.',
34 'upcoming': 'Sorry, this content is not yet available.',
35 'paywall': 'Sorry, this content is only available to Viki Pass Plus subscribers',
36 }
37
38 def _stream_headers(self, timestamp, sig):
39 return {
40 'X-Viki-manufacturer': 'vivo',
41 'X-Viki-device-model': 'vivo 1606',
42 'X-Viki-device-os-ver': '6.0.1',
43 'X-Viki-connection-type': 'WIFI',
44 'X-Viki-carrier': '',
45 'X-Viki-as-id': '100005a-1625321982-3932',
46 'timestamp': str(timestamp),
47 'signature': str(sig),
48 'x-viki-app-ver': self._APP_VERSION
49 }
50
51 def _api_query(self, path, version=4, **kwargs):
52 path += '?' if '?' not in path else '&'
53 query = f'/v{version}/{path}app={self._APP}'
54 if self._token:
55 query += '&token=%s' % self._token
56 return query + ''.join(f'&{name}={val}' for name, val in kwargs.items())
57
58 def _sign_query(self, path):
59 timestamp = int(time.time())
60 query = self._api_query(path, version=5)
61 sig = hmac.new(
62 self._APP_SECRET.encode('ascii'), f'{query}&t={timestamp}'.encode('ascii'), hashlib.sha1).hexdigest()
63 return timestamp, sig, self._API_URL_TEMPLATE % query
64
65 def _call_api(
66 self, path, video_id, note='Downloading JSON metadata', data=None, query=None, fatal=True):
67 if query is None:
68 timestamp, sig, url = self._sign_query(path)
69 else:
70 url = self._API_URL_TEMPLATE % self._api_query(path, version=4)
71 resp = self._download_json(
72 url, video_id, note, fatal=fatal, query=query,
73 data=json.dumps(data).encode('utf-8') if data else None,
74 headers=({'x-viki-app-ver': self._APP_VERSION} if data
75 else self._stream_headers(timestamp, sig) if query is None
76 else None), expected_status=400) or {}
77
78 self._raise_error(resp.get('error'), fatal)
79 return resp
80
81 def _raise_error(self, error, fatal=True):
82 if error is None:
83 return
84 msg = '%s said: %s' % (self.IE_NAME, error)
85 if fatal:
86 raise ExtractorError(msg, expected=True)
87 else:
88 self.report_warning(msg)
89
90 def _check_errors(self, data):
91 for reason, status in (data.get('blocking') or {}).items():
92 if status and reason in self._ERRORS:
93 message = self._ERRORS[reason]
94 if reason == 'geo':
95 self.raise_geo_restricted(msg=message)
96 elif reason == 'paywall':
97 if try_get(data, lambda x: x['paywallable']['tvod']):
98 self._raise_error('This video is for rent only or TVOD (Transactional Video On demand)')
99 self.raise_login_required(message)
100 self._raise_error(message)
101
102 def _real_initialize(self):
103 self._login()
104
105 def _login(self):
106 username, password = self._get_login_info()
107 if username is None:
108 return
109
110 self._token = self._call_api(
111 'sessions.json', None, 'Logging in', fatal=False,
112 data={'username': username, 'password': password}).get('token')
113 if not self._token:
114 self.report_warning('Login Failed: Unable to get session token')
115
116 @staticmethod
117 def dict_selection(dict_obj, preferred_key):
118 if preferred_key in dict_obj:
119 return dict_obj[preferred_key]
120 return (list(filter(None, dict_obj.values())) or [None])[0]
121
122
123 class VikiIE(VikiBaseIE):
124 IE_NAME = 'viki'
125 _VALID_URL = r'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE._VALID_URL_BASE
126 _TESTS = [{
127 'note': 'Free non-DRM video with storyboards in MPD',
128 'url': 'https://www.viki.com/videos/1175236v-choosing-spouse-by-lottery-episode-1',
129 'info_dict': {
130 'id': '1175236v',
131 'ext': 'mp4',
132 'title': 'Choosing Spouse by Lottery - Episode 1',
133 'timestamp': 1606463239,
134 'age_limit': 13,
135 'uploader': 'FCC',
136 'upload_date': '20201127',
137 },
138 'params': {
139 'format': 'bestvideo',
140 },
141 }, {
142 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
143 'info_dict': {
144 'id': '1023585v',
145 'ext': 'mp4',
146 'title': 'Heirs - Episode 14',
147 'uploader': 'SBS Contents Hub',
148 'timestamp': 1385047627,
149 'upload_date': '20131121',
150 'age_limit': 13,
151 'duration': 3570,
152 'episode_number': 14,
153 },
154 'params': {
155 'format': 'bestvideo',
156 },
157 'skip': 'Blocked in the US',
158 }, {
159 # clip
160 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
161 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
162 'info_dict': {
163 'id': '1067139v',
164 'ext': 'mp4',
165 'title': "'The Avengers: Age of Ultron' Press Conference",
166 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
167 'duration': 352,
168 'timestamp': 1430380829,
169 'upload_date': '20150430',
170 'uploader': 'Arirang TV',
171 'like_count': int,
172 'age_limit': 0,
173 },
174 'skip': 'Sorry. There was an error loading this video',
175 }, {
176 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
177 'info_dict': {
178 'id': '1048879v',
179 'ext': 'mp4',
180 'title': 'Ankhon Dekhi',
181 'duration': 6512,
182 'timestamp': 1408532356,
183 'upload_date': '20140820',
184 'uploader': 'Spuul',
185 'like_count': int,
186 'age_limit': 13,
187 },
188 'skip': 'Blocked in the US',
189 }, {
190 # episode
191 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
192 'md5': '0a53dc252e6e690feccd756861495a8c',
193 'info_dict': {
194 'id': '44699v',
195 'ext': 'mp4',
196 'title': 'Boys Over Flowers - Episode 1',
197 'description': 'md5:b89cf50038b480b88b5b3c93589a9076',
198 'duration': 4172,
199 'timestamp': 1270496524,
200 'upload_date': '20100405',
201 'uploader': 'group8',
202 'like_count': int,
203 'age_limit': 13,
204 'episode_number': 1,
205 },
206 'params': {
207 'format': 'bestvideo',
208 },
209 }, {
210 # youtube external
211 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1',
212 'md5': '63f8600c1da6f01b7640eee7eca4f1da',
213 'info_dict': {
214 'id': '50562v',
215 'ext': 'webm',
216 'title': 'Poor Nastya [COMPLETE] - Episode 1',
217 'description': '',
218 'duration': 606,
219 'timestamp': 1274949505,
220 'upload_date': '20101213',
221 'uploader': 'ad14065n',
222 'uploader_id': 'ad14065n',
223 'like_count': int,
224 'age_limit': 13,
225 },
226 'skip': 'Page not found!',
227 }, {
228 'url': 'http://www.viki.com/player/44699v',
229 'only_matching': True,
230 }, {
231 # non-English description
232 'url': 'http://www.viki.com/videos/158036v-love-in-magic',
233 'md5': '41faaba0de90483fb4848952af7c7d0d',
234 'info_dict': {
235 'id': '158036v',
236 'ext': 'mp4',
237 'uploader': 'I Planet Entertainment',
238 'upload_date': '20111122',
239 'timestamp': 1321985454,
240 'description': 'md5:44b1e46619df3a072294645c770cef36',
241 'title': 'Love In Magic',
242 'age_limit': 13,
243 },
244 'params': {
245 'format': 'bestvideo',
246 },
247 }]
248
249 def _real_extract(self, url):
250 video_id = self._match_id(url)
251 video = self._call_api(f'videos/{video_id}.json', video_id, 'Downloading video JSON', query={})
252 self._check_errors(video)
253
254 title = try_get(video, lambda x: x['titles']['en'], str)
255 episode_number = int_or_none(video.get('number'))
256 if not title:
257 title = 'Episode %d' % episode_number if video.get('type') == 'episode' else video.get('id') or video_id
258 container_titles = try_get(video, lambda x: x['container']['titles'], dict) or {}
259 container_title = self.dict_selection(container_titles, 'en')
260 title = '%s - %s' % (container_title, title)
261
262 thumbnails = [{
263 'id': thumbnail_id,
264 'url': thumbnail['url'],
265 } for thumbnail_id, thumbnail in (video.get('images') or {}).items() if thumbnail.get('url')]
266
267 resp = self._call_api(
268 'playback_streams/%s.json?drms=dt1,dt2&device_id=%s' % (video_id, self._DEVICE_ID),
269 video_id, 'Downloading video streams JSON')['main'][0]
270
271 stream_id = try_get(resp, lambda x: x['properties']['track']['stream_id'])
272 subtitles = dict((lang, [{
273 'ext': ext,
274 'url': self._API_URL_TEMPLATE % self._api_query(
275 f'videos/{video_id}/auth_subtitles/{lang}.{ext}', stream_id=stream_id)
276 } for ext in ('srt', 'vtt')]) for lang in (video.get('subtitle_completions') or {}).keys())
277
278 mpd_url = resp['url']
279 # 1080p is hidden in another mpd which can be found in the current manifest content
280 mpd_content = self._download_webpage(mpd_url, video_id, note='Downloading initial MPD manifest')
281 mpd_url = self._search_regex(
282 r'(?mi)<BaseURL>(http.+.mpd)', mpd_content, 'new manifest', default=mpd_url)
283 formats = self._extract_mpd_formats(mpd_url, video_id)
284 self._sort_formats(formats)
285
286 return {
287 'id': video_id,
288 'formats': formats,
289 'title': title,
290 'description': self.dict_selection(video.get('descriptions', {}), 'en'),
291 'duration': int_or_none(video.get('duration')),
292 'timestamp': parse_iso8601(video.get('created_at')),
293 'uploader': video.get('author'),
294 'uploader_url': video.get('author_url'),
295 'like_count': int_or_none(try_get(video, lambda x: x['likes']['count'])),
296 'age_limit': parse_age_limit(video.get('rating')),
297 'thumbnails': thumbnails,
298 'subtitles': subtitles,
299 'episode_number': episode_number,
300 }
301
302
303 class VikiChannelIE(VikiBaseIE):
304 IE_NAME = 'viki:channel'
305 _VALID_URL = r'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE._VALID_URL_BASE
306 _TESTS = [{
307 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
308 'info_dict': {
309 'id': '50c',
310 'title': 'Boys Over Flowers',
311 'description': 'md5:804ce6e7837e1fd527ad2f25420f4d59',
312 },
313 'playlist_mincount': 51,
314 }, {
315 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
316 'info_dict': {
317 'id': '1354c',
318 'title': 'Poor Nastya [COMPLETE]',
319 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
320 },
321 'playlist_count': 127,
322 'skip': 'Page not found',
323 }, {
324 'url': 'http://www.viki.com/news/24569c-showbiz-korea',
325 'only_matching': True,
326 }, {
327 'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005',
328 'only_matching': True,
329 }, {
330 'url': 'http://www.viki.com/artists/2141c-shinee',
331 'only_matching': True,
332 }]
333
334 _video_types = ('episodes', 'movies', 'clips', 'trailers')
335
336 def _entries(self, channel_id):
337 params = {
338 'app': self._APP, 'token': self._token, 'only_ids': 'true',
339 'direction': 'asc', 'sort': 'number', 'per_page': 30
340 }
341 video_types = self._configuration_arg('video_types') or self._video_types
342 for video_type in video_types:
343 if video_type not in self._video_types:
344 self.report_warning(f'Unknown video_type: {video_type}')
345 page_num = 0
346 while True:
347 page_num += 1
348 params['page'] = page_num
349 res = self._call_api(
350 f'containers/{channel_id}/{video_type}.json', channel_id, query=params, fatal=False,
351 note='Downloading %s JSON page %d' % (video_type.title(), page_num))
352
353 for video_id in res.get('response') or []:
354 yield self.url_result(f'https://www.viki.com/videos/{video_id}', VikiIE.ie_key(), video_id)
355 if not res.get('more'):
356 break
357
358 def _real_extract(self, url):
359 channel_id = self._match_id(url)
360 channel = self._call_api('containers/%s.json' % channel_id, channel_id, 'Downloading channel JSON')
361 self._check_errors(channel)
362 return self.playlist_result(
363 self._entries(channel_id), channel_id,
364 self.dict_selection(channel['titles'], 'en'),
365 self.dict_selection(channel['descriptions'], 'en'))