]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
91a6adde JMF |
2 | from __future__ import unicode_literals |
3 | ||
91a6adde | 4 | import base64 |
10db0d2f | 5 | import io |
10db0d2f | 6 | import sys |
91a6adde JMF |
7 | |
8 | from .common import InfoExtractor | |
dab0daee | 9 | from ..compat import ( |
cf282071 | 10 | compat_b64decode, |
edaa23f8 | 11 | compat_struct_unpack, |
dab0daee | 12 | ) |
91a6adde | 13 | from ..utils import ( |
2c53bd51 | 14 | determine_ext, |
ce73839f | 15 | ExtractorError, |
f3bff94c | 16 | float_or_none, |
10db0d2f | 17 | qualities, |
2b9faf55 | 18 | remove_end, |
3e769682 | 19 | remove_start, |
e9f65f87 | 20 | std_headers, |
5edb8dfe | 21 | try_get, |
91a6adde JMF |
22 | ) |
23 | ||
10db0d2f | 24 | _bytes_to_chr = (lambda x: x) if sys.version_info[0] == 2 else (lambda x: map(chr, x)) |
2b9faf55 PH |
25 | |
26 | ||
91a6adde JMF |
27 | class RTVEALaCartaIE(InfoExtractor): |
28 | IE_NAME = 'rtve.es:alacarta' | |
29 | IE_DESC = 'RTVE a la carta' | |
92519402 | 30 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)' |
91a6adde | 31 | |
2b9faf55 | 32 | _TESTS = [{ |
91a6adde | 33 | 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/', |
9aeaf730 | 34 | 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43', |
91a6adde JMF |
35 | 'info_dict': { |
36 | 'id': '2491869', | |
37 | 'ext': 'mp4', | |
38 | 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia', | |
f3bff94c | 39 | 'duration': 5024.566, |
10db0d2f | 40 | 'series': 'Balonmano', |
91a6adde | 41 | }, |
10db0d2f | 42 | 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'], |
2b9faf55 PH |
43 | }, { |
44 | 'note': 'Live stream', | |
45 | 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/', | |
46 | 'info_dict': { | |
47 | 'id': '1694255', | |
10db0d2f | 48 | 'ext': 'mp4', |
49 | 'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
50 | 'is_live': True, | |
51 | }, | |
52 | 'params': { | |
53 | 'skip_download': 'live stream', | |
cc57bd33 | 54 | }, |
2c53bd51 GF |
55 | }, { |
56 | 'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/', | |
10db0d2f | 57 | 'md5': 'd850f3c8731ea53952ebab489cf81cbf', |
2c53bd51 GF |
58 | 'info_dict': { |
59 | 'id': '4236788', | |
60 | 'ext': 'mp4', | |
10db0d2f | 61 | 'title': 'Servir y proteger - Capítulo 104', |
2c53bd51 GF |
62 | 'duration': 3222.0, |
63 | }, | |
10db0d2f | 64 | 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'], |
cd596028 JMF |
65 | }, { |
66 | 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve', | |
67 | 'only_matching': True, | |
4c718d3c JMF |
68 | }, { |
69 | 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/', | |
70 | 'only_matching': True, | |
2b9faf55 | 71 | }] |
91a6adde | 72 | |
e9f65f87 JMF |
73 | def _real_initialize(self): |
74 | user_agent_b64 = base64.b64encode(std_headers['User-Agent'].encode('utf-8')).decode('utf-8') | |
10db0d2f | 75 | self._manager = self._download_json( |
e9f65f87 | 76 | 'http://www.rtve.es/odin/loki/' + user_agent_b64, |
10db0d2f | 77 | None, 'Fetching manager info')['manager'] |
78 | ||
79 | @staticmethod | |
80 | def _decrypt_url(png): | |
81 | encrypted_data = io.BytesIO(compat_b64decode(png)[8:]) | |
82 | while True: | |
83 | length = compat_struct_unpack('!I', encrypted_data.read(4))[0] | |
84 | chunk_type = encrypted_data.read(4) | |
85 | if chunk_type == b'IEND': | |
86 | break | |
87 | data = encrypted_data.read(length) | |
88 | if chunk_type == b'tEXt': | |
89 | alphabet_data, text = data.split(b'\0') | |
90 | quality, url_data = text.split(b'%%') | |
91 | alphabet = [] | |
92 | e = 0 | |
93 | d = 0 | |
94 | for l in _bytes_to_chr(alphabet_data): | |
95 | if d == 0: | |
96 | alphabet.append(l) | |
97 | d = e = (e + 1) % 4 | |
98 | else: | |
99 | d -= 1 | |
100 | url = '' | |
101 | f = 0 | |
102 | e = 3 | |
103 | b = 1 | |
104 | for letter in _bytes_to_chr(url_data): | |
105 | if f == 0: | |
106 | l = int(letter) * 10 | |
107 | f = 1 | |
108 | else: | |
109 | if e == 0: | |
110 | l += int(letter) | |
111 | url += alphabet[l] | |
112 | e = (b + 3) % 4 | |
113 | f = 0 | |
114 | b += 1 | |
115 | else: | |
116 | e -= 1 | |
117 | ||
118 | yield quality.decode(), url | |
119 | encrypted_data.read(4) # CRC | |
120 | ||
121 | def _extract_png_formats(self, video_id): | |
122 | png = self._download_webpage( | |
123 | 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self._manager, video_id), | |
124 | video_id, 'Downloading url information', query={'q': 'v2'}) | |
125 | q = qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL']) | |
126 | formats = [] | |
127 | for quality, video_url in self._decrypt_url(png): | |
128 | ext = determine_ext(video_url) | |
129 | if ext == 'm3u8': | |
130 | formats.extend(self._extract_m3u8_formats( | |
131 | video_url, video_id, 'mp4', 'm3u8_native', | |
132 | m3u8_id='hls', fatal=False)) | |
133 | elif ext == 'mpd': | |
134 | formats.extend(self._extract_mpd_formats( | |
135 | video_url, video_id, 'dash', fatal=False)) | |
136 | else: | |
137 | formats.append({ | |
138 | 'format_id': quality, | |
139 | 'quality': q(quality), | |
140 | 'url': video_url, | |
141 | }) | |
142 | self._sort_formats(formats) | |
143 | return formats | |
e9f65f87 | 144 | |
91a6adde | 145 | def _real_extract(self, url): |
10db0d2f | 146 | video_id = self._match_id(url) |
91a6adde JMF |
147 | info = self._download_json( |
148 | 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id, | |
149 | video_id)['page']['items'][0] | |
ce73839f JMF |
150 | if info['state'] == 'DESPU': |
151 | raise ExtractorError('The video is no longer available', expected=True) | |
10db0d2f | 152 | title = info['title'].strip() |
153 | formats = self._extract_png_formats(video_id) | |
2c53bd51 | 154 | |
25ac63ed | 155 | subtitles = None |
10db0d2f | 156 | sbt_file = info.get('sbtFile') |
157 | if sbt_file: | |
158 | subtitles = self.extract_subtitles(video_id, sbt_file) | |
159 | ||
160 | is_live = info.get('live') is True | |
25ac63ed | 161 | |
91a6adde JMF |
162 | return { |
163 | 'id': video_id, | |
39ca3b5c | 164 | 'title': title, |
2c53bd51 | 165 | 'formats': formats, |
2b9faf55 | 166 | 'thumbnail': info.get('image'), |
25ac63ed | 167 | 'subtitles': subtitles, |
10db0d2f | 168 | 'duration': float_or_none(info.get('duration'), 1000), |
169 | 'is_live': is_live, | |
170 | 'series': info.get('programTitle'), | |
2b9faf55 PH |
171 | } |
172 | ||
25ac63ed JMF |
173 | def _get_subtitles(self, video_id, sub_file): |
174 | subs = self._download_json( | |
175 | sub_file + '.json', video_id, | |
176 | 'Downloading subtitles info')['page']['items'] | |
9c665ab7 PH |
177 | return dict( |
178 | (s['lang'], [{'ext': 'vtt', 'url': s['src']}]) | |
25ac63ed JMF |
179 | for s in subs) |
180 | ||
b68eedba | 181 | |
5edb8dfe | 182 | class RTVEAudioIE(RTVEALaCartaIE): |
183 | IE_NAME = 'rtve.es:audio' | |
184 | IE_DESC = 'RTVE audio' | |
185 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/(alacarta|play)/audios/[^/]+/[^/]+/(?P<id>[0-9]+)' | |
186 | ||
187 | _TESTS = [{ | |
188 | 'url': 'https://www.rtve.es/alacarta/audios/a-hombros-de-gigantes/palabra-ingeniero-codigos-informaticos-27-04-21/5889192/', | |
189 | 'md5': 'ae06d27bff945c4e87a50f89f6ce48ce', | |
190 | 'info_dict': { | |
191 | 'id': '5889192', | |
192 | 'ext': 'mp3', | |
193 | 'title': 'Códigos informáticos', | |
194 | 'thumbnail': r're:https?://.+/1598856591583.jpg', | |
195 | 'duration': 349.440, | |
196 | 'series': 'A hombros de gigantes', | |
197 | }, | |
198 | }, { | |
199 | 'url': 'https://www.rtve.es/play/audios/en-radio-3/ignatius-farray/5791165/', | |
200 | 'md5': '072855ab89a9450e0ba314c717fa5ebc', | |
201 | 'info_dict': { | |
202 | 'id': '5791165', | |
203 | 'ext': 'mp3', | |
204 | 'title': 'Ignatius Farray', | |
205 | 'thumbnail': r're:https?://.+/1613243011863.jpg', | |
206 | 'duration': 3559.559, | |
207 | 'series': 'En Radio 3' | |
208 | }, | |
209 | }, { | |
210 | 'url': 'https://www.rtve.es/play/audios/frankenstein-o-el-moderno-prometeo/capitulo-26-ultimo-muerte-victor-juan-jose-plans-mary-shelley/6082623/', | |
211 | 'md5': '0eadab248cc8dd193fa5765712e84d5c', | |
212 | 'info_dict': { | |
213 | 'id': '6082623', | |
214 | 'ext': 'mp3', | |
215 | 'title': 'Capítulo 26 y último: La muerte de Victor', | |
216 | 'thumbnail': r're:https?://.+/1632147445707.jpg', | |
217 | 'duration': 3174.086, | |
218 | 'series': 'Frankenstein o el moderno Prometeo' | |
219 | }, | |
220 | }] | |
221 | ||
222 | def _extract_png_formats(self, audio_id): | |
223 | """ | |
224 | This function retrieves media related png thumbnail which obfuscate | |
225 | valuable information about the media. This information is decrypted | |
226 | via base class _decrypt_url function providing media quality and | |
227 | media url | |
228 | """ | |
229 | png = self._download_webpage( | |
230 | 'http://www.rtve.es/ztnr/movil/thumbnail/%s/audios/%s.png' % | |
231 | (self._manager, audio_id), | |
232 | audio_id, 'Downloading url information', query={'q': 'v2'}) | |
233 | q = qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL']) | |
234 | formats = [] | |
235 | for quality, audio_url in self._decrypt_url(png): | |
236 | ext = determine_ext(audio_url) | |
237 | if ext == 'm3u8': | |
238 | formats.extend(self._extract_m3u8_formats( | |
239 | audio_url, audio_id, 'mp4', 'm3u8_native', | |
240 | m3u8_id='hls', fatal=False)) | |
241 | elif ext == 'mpd': | |
242 | formats.extend(self._extract_mpd_formats( | |
243 | audio_url, audio_id, 'dash', fatal=False)) | |
244 | else: | |
245 | formats.append({ | |
246 | 'format_id': quality, | |
247 | 'quality': q(quality), | |
248 | 'url': audio_url, | |
249 | }) | |
250 | self._sort_formats(formats) | |
251 | return formats | |
252 | ||
253 | def _real_extract(self, url): | |
254 | audio_id = self._match_id(url) | |
255 | info = self._download_json( | |
256 | 'https://www.rtve.es/api/audios/%s.json' % audio_id, | |
257 | audio_id)['page']['items'][0] | |
258 | ||
259 | return { | |
260 | 'id': audio_id, | |
261 | 'title': info['title'].strip(), | |
262 | 'thumbnail': info.get('thumbnail'), | |
263 | 'duration': float_or_none(info.get('duration'), 1000), | |
264 | 'series': try_get(info, lambda x: x['programInfo']['title']), | |
265 | 'formats': self._extract_png_formats(audio_id), | |
266 | } | |
267 | ||
268 | ||
10db0d2f | 269 | class RTVEInfantilIE(RTVEALaCartaIE): |
b68eedba JMF |
270 | IE_NAME = 'rtve.es:infantil' |
271 | IE_DESC = 'RTVE infantil' | |
10db0d2f | 272 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/[^/]+/video/[^/]+/(?P<id>[0-9]+)/' |
d5b55939 EF |
273 | |
274 | _TESTS = [{ | |
275 | 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/', | |
10db0d2f | 276 | 'md5': '5747454717aedf9f9fdf212d1bcfc48d', |
d5b55939 EF |
277 | 'info_dict': { |
278 | 'id': '3040283', | |
279 | 'ext': 'mp4', | |
280 | 'title': 'Maneras de vivir', | |
10db0d2f | 281 | 'thumbnail': r're:https?://.+/1426182947956\.JPG', |
d5b55939 EF |
282 | 'duration': 357.958, |
283 | }, | |
10db0d2f | 284 | 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'], |
b68eedba | 285 | }] |
d5b55939 | 286 | |
d5b55939 | 287 | |
10db0d2f | 288 | class RTVELiveIE(RTVEALaCartaIE): |
2b9faf55 PH |
289 | IE_NAME = 'rtve.es:live' |
290 | IE_DESC = 'RTVE.es live streams' | |
92519402 | 291 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)' |
2b9faf55 PH |
292 | |
293 | _TESTS = [{ | |
3e769682 | 294 | 'url': 'http://www.rtve.es/directo/la-1/', |
2b9faf55 | 295 | 'info_dict': { |
3e769682 JMF |
296 | 'id': 'la-1', |
297 | 'ext': 'mp4', | |
10db0d2f | 298 | 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', |
2b9faf55 PH |
299 | }, |
300 | 'params': { | |
301 | 'skip_download': 'live stream', | |
302 | } | |
303 | }] | |
304 | ||
305 | def _real_extract(self, url): | |
5ad28e7f | 306 | mobj = self._match_valid_url(url) |
2b9faf55 PH |
307 | video_id = mobj.group('id') |
308 | ||
309 | webpage = self._download_webpage(url, video_id) | |
3e769682 JMF |
310 | title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es') |
311 | title = remove_start(title, 'Estoy viendo ') | |
2b9faf55 PH |
312 | |
313 | vidplayer_id = self._search_regex( | |
b63005f5 S |
314 | (r'playerId=player([0-9]+)', |
315 | r'class=["\'].*?\blive_mod\b.*?["\'][^>]+data-assetid=["\'](\d+)', | |
316 | r'data-id=["\'](\d+)'), | |
317 | webpage, 'internal video ID') | |
2b9faf55 | 318 | |
2b9faf55 PH |
319 | return { |
320 | 'id': video_id, | |
39ca3b5c | 321 | 'title': title, |
10db0d2f | 322 | 'formats': self._extract_png_formats(vidplayer_id), |
3e769682 | 323 | 'is_live': True, |
91a6adde | 324 | } |
233b58de JMF |
325 | |
326 | ||
327 | class RTVETelevisionIE(InfoExtractor): | |
328 | IE_NAME = 'rtve.es:television' | |
92519402 | 329 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml' |
233b58de JMF |
330 | |
331 | _TEST = { | |
332 | 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml', | |
333 | 'info_dict': { | |
334 | 'id': '3069778', | |
335 | 'ext': 'mp4', | |
336 | 'title': 'Documentos TV - La revolución del móvil', | |
337 | 'duration': 3496.948, | |
338 | }, | |
339 | 'params': { | |
340 | 'skip_download': True, | |
341 | }, | |
342 | } | |
343 | ||
344 | def _real_extract(self, url): | |
345 | page_id = self._match_id(url) | |
346 | webpage = self._download_webpage(url, page_id) | |
347 | ||
348 | alacarta_url = self._search_regex( | |
349 | r'data-location="alacarta_videos"[^<]+url":"(http://www\.rtve\.es/alacarta.+?)&', | |
350 | webpage, 'alacarta url', default=None) | |
351 | if alacarta_url is None: | |
352 | raise ExtractorError( | |
353 | 'The webpage doesn\'t contain any video', expected=True) | |
354 | ||
355 | return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key()) |