]>
Commit | Line | Data |
---|---|---|
77c8ebe6 RA |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
4 | import base64 | |
5 | import json | |
6 | import hashlib | |
7 | import hmac | |
8 | import random | |
9 | import string | |
10 | import time | |
11 | ||
12 | from .common import InfoExtractor | |
13 | from ..compat import ( | |
4b85f0f9 | 14 | compat_HTTPError, |
77c8ebe6 | 15 | compat_urllib_parse_urlencode, |
16132cff | 16 | compat_urllib_parse, |
77c8ebe6 RA |
17 | ) |
18 | from ..utils import ( | |
4b85f0f9 | 19 | ExtractorError, |
77c8ebe6 RA |
20 | float_or_none, |
21 | int_or_none, | |
34921b43 | 22 | join_nonempty, |
7bb64347 | 23 | traverse_obj, |
77c8ebe6 RA |
24 | ) |
25 | ||
26 | ||
48ab554f | 27 | class VRVBaseIE(InfoExtractor): |
77c8ebe6 RA |
28 | _API_DOMAIN = None |
29 | _API_PARAMS = {} | |
30 | _CMS_SIGNING = {} | |
4b85f0f9 RA |
31 | _TOKEN = None |
32 | _TOKEN_SECRET = '' | |
77c8ebe6 RA |
33 | |
34 | def _call_api(self, path, video_id, note, data=None): | |
4b85f0f9 | 35 | # https://tools.ietf.org/html/rfc5849#section-3 |
77c8ebe6 | 36 | base_url = self._API_DOMAIN + '/core/' + path |
503b604a RA |
37 | query = [ |
38 | ('oauth_consumer_key', self._API_PARAMS['oAuthKey']), | |
39 | ('oauth_nonce', ''.join([random.choice(string.ascii_letters) for _ in range(32)])), | |
40 | ('oauth_signature_method', 'HMAC-SHA1'), | |
41 | ('oauth_timestamp', int(time.time())), | |
42 | ] | |
4b85f0f9 | 43 | if self._TOKEN: |
503b604a | 44 | query.append(('oauth_token', self._TOKEN)) |
4b85f0f9 | 45 | encoded_query = compat_urllib_parse_urlencode(query) |
77c8ebe6 RA |
46 | headers = self.geo_verification_headers() |
47 | if data: | |
48 | data = json.dumps(data).encode() | |
49 | headers['Content-Type'] = 'application/json' | |
4b85f0f9 RA |
50 | base_string = '&'.join([ |
51 | 'POST' if data else 'GET', | |
52 | compat_urllib_parse.quote(base_url, ''), | |
53 | compat_urllib_parse.quote(encoded_query, '')]) | |
77c8ebe6 | 54 | oauth_signature = base64.b64encode(hmac.new( |
4b85f0f9 | 55 | (self._API_PARAMS['oAuthSecret'] + '&' + self._TOKEN_SECRET).encode('ascii'), |
77c8ebe6 | 56 | base_string.encode(), hashlib.sha1).digest()).decode() |
16132cff | 57 | encoded_query += '&oauth_signature=' + compat_urllib_parse.quote(oauth_signature, '') |
4b85f0f9 RA |
58 | try: |
59 | return self._download_json( | |
60 | '?'.join([base_url, encoded_query]), video_id, | |
61 | note='Downloading %s JSON metadata' % note, headers=headers, data=data) | |
62 | except ExtractorError as e: | |
63 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401: | |
64 | raise ExtractorError(json.loads(e.cause.read().decode())['message'], expected=True) | |
65 | raise | |
77c8ebe6 RA |
66 | |
67 | def _call_cms(self, path, video_id, note): | |
48ab554f | 68 | if not self._CMS_SIGNING: |
1824bfdc RA |
69 | index = self._call_api('index', video_id, 'CMS Signing') |
70 | self._CMS_SIGNING = index.get('cms_signing') or {} | |
71 | if not self._CMS_SIGNING: | |
72 | for signing_policy in index.get('signing_policies', []): | |
73 | signing_path = signing_policy.get('path') | |
74 | if signing_path and signing_path.startswith('/cms/'): | |
75 | name, value = signing_policy.get('name'), signing_policy.get('value') | |
76 | if name and value: | |
77 | self._CMS_SIGNING[name] = value | |
77c8ebe6 RA |
78 | return self._download_json( |
79 | self._API_DOMAIN + path, video_id, query=self._CMS_SIGNING, | |
80 | note='Downloading %s JSON metadata' % note, headers=self.geo_verification_headers()) | |
81 | ||
48ab554f RA |
82 | def _get_cms_resource(self, resource_key, video_id): |
83 | return self._call_api( | |
84 | 'cms_resource', video_id, 'resource path', data={ | |
85 | 'resource_key': resource_key, | |
86 | })['__links__']['cms_resource']['href'] | |
87 | ||
4b85f0f9 RA |
88 | def _real_initialize(self): |
89 | webpage = self._download_webpage( | |
90 | 'https://vrv.co/', None, headers=self.geo_verification_headers()) | |
91 | self._API_PARAMS = self._parse_json(self._search_regex( | |
92 | [ | |
93 | r'window\.__APP_CONFIG__\s*=\s*({.+?})(?:</script>|;)', | |
94 | r'window\.__APP_CONFIG__\s*=\s*({.+})' | |
95 | ], webpage, 'app config'), None)['cxApiParams'] | |
96 | self._API_DOMAIN = self._API_PARAMS.get('apiDomain', 'https://api.vrv.co') | |
97 | ||
48ab554f RA |
98 | |
99 | class VRVIE(VRVBaseIE): | |
100 | IE_NAME = 'vrv' | |
101 | _VALID_URL = r'https?://(?:www\.)?vrv\.co/watch/(?P<id>[A-Z0-9]+)' | |
54a5be4d | 102 | _TESTS = [{ |
48ab554f RA |
103 | 'url': 'https://vrv.co/watch/GR9PNZ396/Hidden-America-with-Jonah-Ray:BOSTON-WHERE-THE-PAST-IS-THE-PRESENT', |
104 | 'info_dict': { | |
105 | 'id': 'GR9PNZ396', | |
106 | 'ext': 'mp4', | |
107 | 'title': 'BOSTON: WHERE THE PAST IS THE PRESENT', | |
108 | 'description': 'md5:4ec8844ac262ca2df9e67c0983c6b83f', | |
109 | 'uploader_id': 'seeso', | |
110 | }, | |
111 | 'params': { | |
112 | # m3u8 download | |
113 | 'skip_download': True, | |
114 | }, | |
1fa88937 RA |
115 | }, { |
116 | # movie listing | |
117 | 'url': 'https://vrv.co/watch/G6NQXZ1J6/Lily-CAT', | |
118 | 'info_dict': { | |
119 | 'id': 'G6NQXZ1J6', | |
120 | 'title': 'Lily C.A.T', | |
121 | 'description': 'md5:988b031e7809a6aeb60968be4af7db07', | |
122 | }, | |
123 | 'playlist_count': 2, | |
54a5be4d | 124 | }] |
4b85f0f9 RA |
125 | _NETRC_MACHINE = 'vrv' |
126 | ||
127 | def _real_initialize(self): | |
128 | super(VRVIE, self)._real_initialize() | |
129 | ||
130 | email, password = self._get_login_info() | |
131 | if email is None: | |
132 | return | |
133 | ||
134 | token_credentials = self._call_api( | |
135 | 'authenticate/by:credentials', None, 'Token Credentials', data={ | |
136 | 'email': email, | |
137 | 'password': password, | |
138 | }) | |
139 | self._TOKEN = token_credentials['oauth_token'] | |
140 | self._TOKEN_SECRET = token_credentials['oauth_token_secret'] | |
54a5be4d RA |
141 | |
142 | def _extract_vrv_formats(self, url, video_id, stream_format, audio_lang, hardsub_lang): | |
0e2dd3fc | 143 | if not url or stream_format not in ('hls', 'dash', 'adaptive_hls'): |
54a5be4d | 144 | return [] |
34921b43 | 145 | format_id = join_nonempty( |
146 | stream_format, | |
147 | audio_lang and 'audio-%s' % audio_lang, | |
148 | hardsub_lang and 'hardsub-%s' % hardsub_lang) | |
0e2dd3fc | 149 | if 'hls' in stream_format: |
54a5be4d RA |
150 | adaptive_formats = self._extract_m3u8_formats( |
151 | url, video_id, 'mp4', m3u8_id=format_id, | |
1fa88937 | 152 | note='Downloading %s information' % format_id, |
54a5be4d RA |
153 | fatal=False) |
154 | elif stream_format == 'dash': | |
155 | adaptive_formats = self._extract_mpd_formats( | |
156 | url, video_id, mpd_id=format_id, | |
1fa88937 | 157 | note='Downloading %s information' % format_id, |
54a5be4d RA |
158 | fatal=False) |
159 | if audio_lang: | |
160 | for f in adaptive_formats: | |
161 | if f.get('acodec') != 'none': | |
162 | f['language'] = audio_lang | |
163 | return adaptive_formats | |
77c8ebe6 RA |
164 | |
165 | def _real_extract(self, url): | |
166 | video_id = self._match_id(url) | |
4b85f0f9 | 167 | |
9f182c23 RA |
168 | object_data = self._call_cms(self._get_cms_resource( |
169 | 'cms:/objects/' + video_id, video_id), video_id, 'object')['items'][0] | |
170 | resource_path = object_data['__links__']['resource']['href'] | |
171 | video_data = self._call_cms(resource_path, video_id, 'video') | |
77c8ebe6 | 172 | title = video_data['title'] |
1fa88937 RA |
173 | description = video_data.get('description') |
174 | ||
175 | if video_data.get('__class__') == 'movie_listing': | |
176 | items = self._call_cms( | |
177 | video_data['__links__']['movie_listing/movies']['href'], | |
178 | video_id, 'movie listing').get('items') or [] | |
179 | if len(items) != 1: | |
180 | entries = [] | |
181 | for item in items: | |
182 | item_id = item.get('id') | |
183 | if not item_id: | |
184 | continue | |
185 | entries.append(self.url_result( | |
186 | 'https://vrv.co/watch/' + item_id, | |
187 | self.ie_key(), item_id, item.get('title'))) | |
188 | return self.playlist_result(entries, video_id, title, description) | |
189 | video_data = items[0] | |
77c8ebe6 | 190 | |
4b85f0f9 RA |
191 | streams_path = video_data['__links__'].get('streams', {}).get('href') |
192 | if not streams_path: | |
193 | self.raise_login_required() | |
194 | streams_json = self._call_cms(streams_path, video_id, 'streams') | |
77c8ebe6 RA |
195 | |
196 | audio_locale = streams_json.get('audio_locale') | |
197 | formats = [] | |
afa0200b RA |
198 | for stream_type, streams in streams_json.get('streams', {}).items(): |
199 | if stream_type in ('adaptive_hls', 'adaptive_dash'): | |
200 | for stream in streams.values(): | |
54a5be4d RA |
201 | formats.extend(self._extract_vrv_formats( |
202 | stream.get('url'), video_id, stream_type.split('_')[1], | |
203 | audio_locale, stream.get('hardsub_locale'))) | |
77c8ebe6 RA |
204 | self._sort_formats(formats) |
205 | ||
afa0200b | 206 | subtitles = {} |
58317428 RA |
207 | for k in ('captions', 'subtitles'): |
208 | for subtitle in streams_json.get(k, {}).values(): | |
209 | subtitle_url = subtitle.get('url') | |
210 | if not subtitle_url: | |
211 | continue | |
212 | subtitles.setdefault(subtitle.get('locale', 'en-US'), []).append({ | |
213 | 'url': subtitle_url, | |
214 | 'ext': subtitle.get('format', 'ass'), | |
215 | }) | |
afa0200b | 216 | |
77c8ebe6 | 217 | thumbnails = [] |
f7590d47 | 218 | for thumbnail in traverse_obj(video_data, ('images', 'thumbnail', ..., ...)) or []: |
77c8ebe6 RA |
219 | thumbnail_url = thumbnail.get('source') |
220 | if not thumbnail_url: | |
221 | continue | |
222 | thumbnails.append({ | |
223 | 'url': thumbnail_url, | |
224 | 'width': int_or_none(thumbnail.get('width')), | |
225 | 'height': int_or_none(thumbnail.get('height')), | |
226 | }) | |
227 | ||
228 | return { | |
229 | 'id': video_id, | |
230 | 'title': title, | |
231 | 'formats': formats, | |
afa0200b | 232 | 'subtitles': subtitles, |
77c8ebe6 | 233 | 'thumbnails': thumbnails, |
1fa88937 | 234 | 'description': description, |
77c8ebe6 RA |
235 | 'duration': float_or_none(video_data.get('duration_ms'), 1000), |
236 | 'uploader_id': video_data.get('channel_id'), | |
237 | 'series': video_data.get('series_title'), | |
238 | 'season': video_data.get('season_title'), | |
239 | 'season_number': int_or_none(video_data.get('season_number')), | |
240 | 'season_id': video_data.get('season_id'), | |
241 | 'episode': title, | |
242 | 'episode_number': int_or_none(video_data.get('episode_number')), | |
243 | 'episode_id': video_data.get('production_episode_id'), | |
244 | } | |
48ab554f RA |
245 | |
246 | ||
247 | class VRVSeriesIE(VRVBaseIE): | |
248 | IE_NAME = 'vrv:series' | |
249 | _VALID_URL = r'https?://(?:www\.)?vrv\.co/series/(?P<id>[A-Z0-9]+)' | |
250 | _TEST = { | |
251 | 'url': 'https://vrv.co/series/G68VXG3G6/The-Perfect-Insider', | |
252 | 'info_dict': { | |
253 | 'id': 'G68VXG3G6', | |
254 | }, | |
255 | 'playlist_mincount': 11, | |
256 | } | |
257 | ||
258 | def _real_extract(self, url): | |
259 | series_id = self._match_id(url) | |
48ab554f | 260 | |
48ab554f RA |
261 | seasons_path = self._get_cms_resource( |
262 | 'cms:/seasons?series_id=' + series_id, series_id) | |
263 | seasons_data = self._call_cms(seasons_path, series_id, 'seasons') | |
264 | ||
265 | entries = [] | |
266 | for season in seasons_data.get('items', []): | |
267 | episodes_path = season['__links__']['season/episodes']['href'] | |
268 | episodes = self._call_cms(episodes_path, series_id, 'episodes') | |
269 | for episode in episodes.get('items', []): | |
270 | episode_id = episode['id'] | |
271 | entries.append(self.url_result( | |
272 | 'https://vrv.co/watch/' + episode_id, | |
273 | 'VRV', episode_id, episode.get('title'))) | |
274 | ||
275 | return self.playlist_result(entries, series_id) |