2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
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'
22 _DEVICE_ID
= '112395910d'
24 _APP_VERSION
= '6.11.3'
25 _APP_SECRET
= 'd96704b180208dbb2efa30fe44c48bd8690441af9f567ba8fd710a72badc85198f7472'
28 _NETRC_MACHINE
= 'viki'
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',
38 def _stream_headers(self
, timestamp
, sig
):
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',
45 'X-Viki-as-id': '100005a-1625321982-3932',
46 'timestamp': str(timestamp
),
47 'signature': str(sig
),
48 'x-viki-app-ver': self
._APP
_VERSION
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}'
55 query
+= '&token=%s' % self
._token
56 return query
+ ''.join(f
'&{name}={val}' for name
, val
in kwargs
.items())
58 def _sign_query(self
, path
):
59 timestamp
= int(time
.time())
60 query
= self
._api
_query
(path
, version
=5)
62 self
._APP
_SECRET
.encode('ascii'), f
'{query}&t={timestamp}'.encode('ascii'), hashlib
.sha1
).hexdigest()
63 return timestamp
, sig
, self
._API
_URL
_TEMPLATE
% query
66 self
, path
, video_id
, note
='Downloading JSON metadata', data
=None, query
=None, fatal
=True):
68 timestamp
, sig
, url
= self
._sign
_query
(path
)
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 {}
78 self
._raise
_error
(resp
.get('error'), fatal
)
81 def _raise_error(self
, error
, fatal
=True):
84 msg
= '%s said: %s' % (self
.IE_NAME
, error
)
86 raise ExtractorError(msg
, expected
=True)
88 self
.report_warning(msg
)
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
]
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
)
102 def _perform_login(self
, username
, password
):
103 self
._token
= self
._call
_api
(
104 'sessions.json', None, 'Logging in', fatal
=False,
105 data
={'username': username, 'password': password}
).get('token')
107 self
.report_warning('Login Failed: Unable to get session token')
110 def dict_selection(dict_obj
, preferred_key
):
111 if preferred_key
in dict_obj
:
112 return dict_obj
[preferred_key
]
113 return (list(filter(None, dict_obj
.values())) or [None])[0]
116 class VikiIE(VikiBaseIE
):
118 _VALID_URL
= r
'%s(?:videos|player)/(?P<id>[0-9]+v)' % VikiBaseIE
._VALID
_URL
_BASE
120 'note': 'Free non-DRM video with storyboards in MPD',
121 'url': 'https://www.viki.com/videos/1175236v-choosing-spouse-by-lottery-episode-1',
125 'title': 'Choosing Spouse by Lottery - Episode 1',
126 'timestamp': 1606463239,
129 'upload_date': '20201127',
132 'url': 'http://www.viki.com/videos/1023585v-heirs-episode-14',
136 'title': 'Heirs - Episode 14',
137 'uploader': 'SBS Contents Hub',
138 'timestamp': 1385047627,
139 'upload_date': '20131121',
142 'episode_number': 14,
144 'skip': 'Blocked in the US',
147 'url': 'http://www.viki.com/videos/1067139v-the-avengers-age-of-ultron-press-conference',
148 'md5': '86c0b5dbd4d83a6611a79987cc7a1989',
152 'title': "'The Avengers: Age of Ultron' Press Conference",
153 'description': 'md5:d70b2f9428f5488321bfe1db10d612ea',
155 'timestamp': 1430380829,
156 'upload_date': '20150430',
157 'uploader': 'Arirang TV',
161 'skip': 'Sorry. There was an error loading this video',
163 'url': 'http://www.viki.com/videos/1048879v-ankhon-dekhi',
167 'title': 'Ankhon Dekhi',
169 'timestamp': 1408532356,
170 'upload_date': '20140820',
175 'skip': 'Blocked in the US',
178 'url': 'http://www.viki.com/videos/44699v-boys-over-flowers-episode-1',
179 'md5': '0a53dc252e6e690feccd756861495a8c',
183 'title': 'Boys Over Flowers - Episode 1',
184 'description': 'md5:b89cf50038b480b88b5b3c93589a9076',
186 'timestamp': 1270496524,
187 'upload_date': '20100405',
188 'uploader': 'group8',
195 'url': 'http://www.viki.com/videos/50562v-poor-nastya-complete-episode-1',
196 'md5': '63f8600c1da6f01b7640eee7eca4f1da',
200 'title': 'Poor Nastya [COMPLETE] - Episode 1',
203 'timestamp': 1274949505,
204 'upload_date': '20101213',
205 'uploader': 'ad14065n',
206 'uploader_id': 'ad14065n',
210 'skip': 'Page not found!',
212 'url': 'http://www.viki.com/player/44699v',
213 'only_matching': True,
215 # non-English description
216 'url': 'http://www.viki.com/videos/158036v-love-in-magic',
217 'md5': '41faaba0de90483fb4848952af7c7d0d',
221 'uploader': 'I Planet Entertainment',
222 'upload_date': '20111122',
223 'timestamp': 1321985454,
224 'description': 'md5:44b1e46619df3a072294645c770cef36',
225 'title': 'Love In Magic',
230 def _real_extract(self
, url
):
231 video_id
= self
._match
_id
(url
)
232 video
= self
._call
_api
(f
'videos/{video_id}.json', video_id
, 'Downloading video JSON', query
={})
233 self
._check
_errors
(video
)
235 title
= try_get(video
, lambda x
: x
['titles']['en'], str)
236 episode_number
= int_or_none(video
.get('number'))
238 title
= 'Episode %d' % episode_number
if video
.get('type') == 'episode' else video
.get('id') or video_id
239 container_titles
= try_get(video
, lambda x
: x
['container']['titles'], dict) or {}
240 container_title
= self
.dict_selection(container_titles
, 'en')
241 title
= '%s - %s' % (container_title
, title
)
245 'url': thumbnail
['url'],
246 } for thumbnail_id
, thumbnail
in (video
.get('images') or {}).items() if thumbnail
.get('url')]
248 resp
= self
._call
_api
(
249 'playback_streams/%s.json?drms=dt3&device_id=%s' % (video_id
, self
._DEVICE
_ID
),
250 video_id
, 'Downloading video streams JSON')['main'][0]
252 stream_id
= try_get(resp
, lambda x
: x
['properties']['track']['stream_id'])
253 subtitles
= dict((lang
, [{
255 'url': self
._API
_URL
_TEMPLATE
% self
._api
_query
(
256 f
'videos/{video_id}/auth_subtitles/{lang}.{ext}', stream_id
=stream_id
)
257 } for ext
in ('srt', 'vtt')]) for lang
in (video
.get('subtitle_completions') or {}).keys())
259 mpd_url
= resp
['url']
260 # 720p is hidden in another MPD which can be found in the current manifest content
261 mpd_content
= self
._download
_webpage
(mpd_url
, video_id
, note
='Downloading initial MPD manifest')
262 mpd_url
= self
._search
_regex
(
263 r
'(?mi)<BaseURL>(http.+.mpd)', mpd_content
, 'new manifest', default
=mpd_url
)
264 if 'mpdhd_high' not in mpd_url
and 'sig=' not in mpd_url
:
265 # Modify the URL to get 1080p
266 mpd_url
= mpd_url
.replace('mpdhd', 'mpdhd_high')
267 formats
= self
._extract
_mpd
_formats
(mpd_url
, video_id
)
268 self
._sort
_formats
(formats
)
274 'description': self
.dict_selection(video
.get('descriptions', {}), 'en'),
275 'duration': int_or_none(video
.get('duration')),
276 'timestamp': parse_iso8601(video
.get('created_at')),
277 'uploader': video
.get('author'),
278 'uploader_url': video
.get('author_url'),
279 'like_count': int_or_none(try_get(video
, lambda x
: x
['likes']['count'])),
280 'age_limit': parse_age_limit(video
.get('rating')),
281 'thumbnails': thumbnails
,
282 'subtitles': subtitles
,
283 'episode_number': episode_number
,
287 class VikiChannelIE(VikiBaseIE
):
288 IE_NAME
= 'viki:channel'
289 _VALID_URL
= r
'%s(?:tv|news|movies|artists)/(?P<id>[0-9]+c)' % VikiBaseIE
._VALID
_URL
_BASE
291 'url': 'http://www.viki.com/tv/50c-boys-over-flowers',
294 'title': 'Boys Over Flowers',
295 'description': 'md5:804ce6e7837e1fd527ad2f25420f4d59',
297 'playlist_mincount': 51,
299 'url': 'http://www.viki.com/tv/1354c-poor-nastya-complete',
302 'title': 'Poor Nastya [COMPLETE]',
303 'description': 'md5:05bf5471385aa8b21c18ad450e350525',
305 'playlist_count': 127,
306 'skip': 'Page not found',
308 'url': 'http://www.viki.com/news/24569c-showbiz-korea',
309 'only_matching': True,
311 'url': 'http://www.viki.com/movies/22047c-pride-and-prejudice-2005',
312 'only_matching': True,
314 'url': 'http://www.viki.com/artists/2141c-shinee',
315 'only_matching': True,
318 _video_types
= ('episodes', 'movies', 'clips', 'trailers')
320 def _entries(self
, channel_id
):
322 'app': self
._APP
, 'token': self
._token
, 'only_ids': 'true',
323 'direction': 'asc', 'sort': 'number', 'per_page': 30
325 video_types
= self
._configuration
_arg
('video_types') or self
._video
_types
326 for video_type
in video_types
:
327 if video_type
not in self
._video
_types
:
328 self
.report_warning(f
'Unknown video_type: {video_type}')
332 params
['page'] = page_num
333 res
= self
._call
_api
(
334 f
'containers/{channel_id}/{video_type}.json', channel_id
, query
=params
, fatal
=False,
335 note
='Downloading %s JSON page %d' % (video_type
.title(), page_num
))
337 for video_id
in res
.get('response') or []:
338 yield self
.url_result(f
'https://www.viki.com/videos/{video_id}', VikiIE
.ie_key(), video_id
)
339 if not res
.get('more'):
342 def _real_extract(self
, url
):
343 channel_id
= self
._match
_id
(url
)
344 channel
= self
._call
_api
('containers/%s.json' % channel_id
, channel_id
, 'Downloading channel JSON')
345 self
._check
_errors
(channel
)
346 return self
.playlist_result(
347 self
._entries
(channel_id
), channel_id
,
348 self
.dict_selection(channel
['titles'], 'en'),
349 self
.dict_selection(channel
['descriptions'], 'en'))