5 from .common
import InfoExtractor
20 _bytes_to_chr
= (lambda x
: x
) if sys
.version_info
[0] == 2 else (lambda x
: map(chr, x
))
23 class RTVEALaCartaIE(InfoExtractor
):
24 IE_NAME
= 'rtve.es:alacarta'
25 IE_DESC
= 'RTVE a la carta'
26 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)'
29 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
30 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
34 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
36 'series': 'Balonmano',
38 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
40 'note': 'Live stream',
41 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
45 'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
49 'skip_download': 'live stream',
52 'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/',
53 'md5': 'd850f3c8731ea53952ebab489cf81cbf',
57 'title': 'Servir y proteger - Capítulo 104',
60 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
62 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
63 'only_matching': True,
65 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/',
66 'only_matching': True,
69 def _real_initialize(self
):
70 user_agent_b64
= base64
.b64encode(self
.get_param('http_headers')['User-Agent'].encode('utf-8')).decode('utf-8')
71 self
._manager
= self
._download
_json
(
72 'http://www.rtve.es/odin/loki/' + user_agent_b64
,
73 None, 'Fetching manager info')['manager']
76 def _decrypt_url(png
):
77 encrypted_data
= io
.BytesIO(compat_b64decode(png
)[8:])
79 length
= compat_struct_unpack('!I', encrypted_data
.read(4))[0]
80 chunk_type
= encrypted_data
.read(4)
81 if chunk_type
== b
'IEND':
83 data
= encrypted_data
.read(length
)
84 if chunk_type
== b
'tEXt':
85 alphabet_data
, text
= data
.split(b
'\0')
86 quality
, url_data
= text
.split(b
'%%')
90 for l
in _bytes_to_chr(alphabet_data
):
100 for letter
in _bytes_to_chr(url_data
):
114 yield quality
.decode(), url
115 encrypted_data
.read(4) # CRC
117 def _extract_png_formats(self
, video_id
):
118 png
= self
._download
_webpage
(
119 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self
._manager
, video_id
),
120 video_id
, 'Downloading url information', query
={'q': 'v2'}
)
121 q
= qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL'])
123 for quality
, video_url
in self
._decrypt
_url
(png
):
124 ext
= determine_ext(video_url
)
126 formats
.extend(self
._extract
_m
3u8_formats
(
127 video_url
, video_id
, 'mp4', 'm3u8_native',
128 m3u8_id
='hls', fatal
=False))
130 formats
.extend(self
._extract
_mpd
_formats
(
131 video_url
, video_id
, 'dash', fatal
=False))
134 'format_id': quality
,
135 'quality': q(quality
),
138 self
._sort
_formats
(formats
)
141 def _real_extract(self
, url
):
142 video_id
= self
._match
_id
(url
)
143 info
= self
._download
_json
(
144 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id
,
145 video_id
)['page']['items'][0]
146 if info
['state'] == 'DESPU':
147 raise ExtractorError('The video is no longer available', expected
=True)
148 title
= info
['title'].strip()
149 formats
= self
._extract
_png
_formats
(video_id
)
152 sbt_file
= info
.get('sbtFile')
154 subtitles
= self
.extract_subtitles(video_id
, sbt_file
)
156 is_live
= info
.get('live') is True
162 'thumbnail': info
.get('image'),
163 'subtitles': subtitles
,
164 'duration': float_or_none(info
.get('duration'), 1000),
166 'series': info
.get('programTitle'),
169 def _get_subtitles(self
, video_id
, sub_file
):
170 subs
= self
._download
_json
(
171 sub_file
+ '.json', video_id
,
172 'Downloading subtitles info')['page']['items']
174 (s
['lang'], [{'ext': 'vtt', 'url': s['src']}
])
178 class RTVEAudioIE(RTVEALaCartaIE
):
179 IE_NAME
= 'rtve.es:audio'
180 IE_DESC
= 'RTVE audio'
181 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/(alacarta|play)/audios/[^/]+/[^/]+/(?P<id>[0-9]+)'
184 'url': 'https://www.rtve.es/alacarta/audios/a-hombros-de-gigantes/palabra-ingeniero-codigos-informaticos-27-04-21/5889192/',
185 'md5': 'ae06d27bff945c4e87a50f89f6ce48ce',
189 'title': 'Códigos informáticos',
190 'thumbnail': r
're:https?://.+/1598856591583.jpg',
192 'series': 'A hombros de gigantes',
195 'url': 'https://www.rtve.es/play/audios/en-radio-3/ignatius-farray/5791165/',
196 'md5': '072855ab89a9450e0ba314c717fa5ebc',
200 'title': 'Ignatius Farray',
201 'thumbnail': r
're:https?://.+/1613243011863.jpg',
202 'duration': 3559.559,
203 'series': 'En Radio 3'
206 'url': 'https://www.rtve.es/play/audios/frankenstein-o-el-moderno-prometeo/capitulo-26-ultimo-muerte-victor-juan-jose-plans-mary-shelley/6082623/',
207 'md5': '0eadab248cc8dd193fa5765712e84d5c',
211 'title': 'Capítulo 26 y último: La muerte de Victor',
212 'thumbnail': r
're:https?://.+/1632147445707.jpg',
213 'duration': 3174.086,
214 'series': 'Frankenstein o el moderno Prometeo'
218 def _extract_png_formats(self
, audio_id
):
220 This function retrieves media related png thumbnail which obfuscate
221 valuable information about the media. This information is decrypted
222 via base class _decrypt_url function providing media quality and
225 png
= self
._download
_webpage
(
226 'http://www.rtve.es/ztnr/movil/thumbnail/%s/audios/%s.png' %
227 (self
._manager
, audio_id
),
228 audio_id
, 'Downloading url information', query
={'q': 'v2'}
)
229 q
= qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL'])
231 for quality
, audio_url
in self
._decrypt
_url
(png
):
232 ext
= determine_ext(audio_url
)
234 formats
.extend(self
._extract
_m
3u8_formats
(
235 audio_url
, audio_id
, 'mp4', 'm3u8_native',
236 m3u8_id
='hls', fatal
=False))
238 formats
.extend(self
._extract
_mpd
_formats
(
239 audio_url
, audio_id
, 'dash', fatal
=False))
242 'format_id': quality
,
243 'quality': q(quality
),
246 self
._sort
_formats
(formats
)
249 def _real_extract(self
, url
):
250 audio_id
= self
._match
_id
(url
)
251 info
= self
._download
_json
(
252 'https://www.rtve.es/api/audios/%s.json' % audio_id
,
253 audio_id
)['page']['items'][0]
257 'title': info
['title'].strip(),
258 'thumbnail': info
.get('thumbnail'),
259 'duration': float_or_none(info
.get('duration'), 1000),
260 'series': try_get(info
, lambda x
: x
['programInfo']['title']),
261 'formats': self
._extract
_png
_formats
(audio_id
),
265 class RTVEInfantilIE(RTVEALaCartaIE
):
266 IE_NAME
= 'rtve.es:infantil'
267 IE_DESC
= 'RTVE infantil'
268 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/infantil/serie/[^/]+/video/[^/]+/(?P<id>[0-9]+)/'
271 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
272 'md5': '5747454717aedf9f9fdf212d1bcfc48d',
276 'title': 'Maneras de vivir',
277 'thumbnail': r
're:https?://.+/1426182947956\.JPG',
280 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
284 class RTVELiveIE(RTVEALaCartaIE
):
285 IE_NAME
= 'rtve.es:live'
286 IE_DESC
= 'RTVE.es live streams'
287 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
290 'url': 'http://www.rtve.es/directo/la-1/',
294 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
297 'skip_download': 'live stream',
301 def _real_extract(self
, url
):
302 mobj
= self
._match
_valid
_url
(url
)
303 video_id
= mobj
.group('id')
305 webpage
= self
._download
_webpage
(url
, video_id
)
306 title
= remove_end(self
._og
_search
_title
(webpage
), ' en directo en RTVE.es')
307 title
= remove_start(title
, 'Estoy viendo ')
309 vidplayer_id
= self
._search
_regex
(
310 (r
'playerId=player([0-9]+)',
311 r
'class=["\'].*?
\blive
_mod
\b.*?
["\'][^>]+data-assetid=["\'](\d
+)',
312 r'data
-id=["\'](\d+)'),
313 webpage, 'internal video ID')
318 'formats': self._extract_png_formats(vidplayer_id),
323 class RTVETelevisionIE(InfoExtractor):
324 IE_NAME = 'rtve.es:television'
325 _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml'
328 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml',
332 'title': 'Documentos TV - La revolución del móvil',
333 'duration': 3496.948,
336 'skip_download': True,
340 def _real_extract(self, url):
341 page_id = self._match_id(url)
342 webpage = self._download_webpage(url, page_id)
344 alacarta_url = self._search_regex(
345 r'data-location="alacarta_videos
"[^<]+url":"(http://www\.rtve\.es/alacarta.+?)&',
346 webpage, 'alacarta url', default=None)
347 if alacarta_url is None:
348 raise ExtractorError(
349 'The webpage doesn\'t contain any video', expected=True)
351 return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key())