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