2 from __future__
import unicode_literals
8 from .common
import InfoExtractor
23 _bytes_to_chr
= (lambda x
: x
) if sys
.version_info
[0] == 2 else (lambda x
: map(chr, x
))
26 class RTVEALaCartaIE(InfoExtractor
):
27 IE_NAME
= 'rtve.es:alacarta'
28 IE_DESC
= 'RTVE a la carta'
29 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)'
32 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
33 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
37 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
39 'series': 'Balonmano',
41 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
43 'note': 'Live stream',
44 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
48 'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
52 'skip_download': 'live stream',
55 'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/',
56 'md5': 'd850f3c8731ea53952ebab489cf81cbf',
60 'title': 'Servir y proteger - Capítulo 104',
63 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
65 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
66 'only_matching': True,
68 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/',
69 'only_matching': True,
72 def _real_initialize(self
):
73 user_agent_b64
= base64
.b64encode(self
.get_param('http_headers')['User-Agent'].encode('utf-8')).decode('utf-8')
74 self
._manager
= self
._download
_json
(
75 'http://www.rtve.es/odin/loki/' + user_agent_b64
,
76 None, 'Fetching manager info')['manager']
79 def _decrypt_url(png
):
80 encrypted_data
= io
.BytesIO(compat_b64decode(png
)[8:])
82 length
= compat_struct_unpack('!I', encrypted_data
.read(4))[0]
83 chunk_type
= encrypted_data
.read(4)
84 if chunk_type
== b
'IEND':
86 data
= encrypted_data
.read(length
)
87 if chunk_type
== b
'tEXt':
88 alphabet_data
, text
= data
.split(b
'\0')
89 quality
, url_data
= text
.split(b
'%%')
93 for l
in _bytes_to_chr(alphabet_data
):
103 for letter
in _bytes_to_chr(url_data
):
117 yield quality
.decode(), url
118 encrypted_data
.read(4) # CRC
120 def _extract_png_formats(self
, video_id
):
121 png
= self
._download
_webpage
(
122 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self
._manager
, video_id
),
123 video_id
, 'Downloading url information', query
={'q': 'v2'}
)
124 q
= qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL'])
126 for quality
, video_url
in self
._decrypt
_url
(png
):
127 ext
= determine_ext(video_url
)
129 formats
.extend(self
._extract
_m
3u8_formats
(
130 video_url
, video_id
, 'mp4', 'm3u8_native',
131 m3u8_id
='hls', fatal
=False))
133 formats
.extend(self
._extract
_mpd
_formats
(
134 video_url
, video_id
, 'dash', fatal
=False))
137 'format_id': quality
,
138 'quality': q(quality
),
141 self
._sort
_formats
(formats
)
144 def _real_extract(self
, url
):
145 video_id
= self
._match
_id
(url
)
146 info
= self
._download
_json
(
147 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id
,
148 video_id
)['page']['items'][0]
149 if info
['state'] == 'DESPU':
150 raise ExtractorError('The video is no longer available', expected
=True)
151 title
= info
['title'].strip()
152 formats
= self
._extract
_png
_formats
(video_id
)
155 sbt_file
= info
.get('sbtFile')
157 subtitles
= self
.extract_subtitles(video_id
, sbt_file
)
159 is_live
= info
.get('live') is True
165 'thumbnail': info
.get('image'),
166 'subtitles': subtitles
,
167 'duration': float_or_none(info
.get('duration'), 1000),
169 'series': info
.get('programTitle'),
172 def _get_subtitles(self
, video_id
, sub_file
):
173 subs
= self
._download
_json
(
174 sub_file
+ '.json', video_id
,
175 'Downloading subtitles info')['page']['items']
177 (s
['lang'], [{'ext': 'vtt', 'url': s['src']}
])
181 class RTVEAudioIE(RTVEALaCartaIE
):
182 IE_NAME
= 'rtve.es:audio'
183 IE_DESC
= 'RTVE audio'
184 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/(alacarta|play)/audios/[^/]+/[^/]+/(?P<id>[0-9]+)'
187 'url': 'https://www.rtve.es/alacarta/audios/a-hombros-de-gigantes/palabra-ingeniero-codigos-informaticos-27-04-21/5889192/',
188 'md5': 'ae06d27bff945c4e87a50f89f6ce48ce',
192 'title': 'Códigos informáticos',
193 'thumbnail': r
're:https?://.+/1598856591583.jpg',
195 'series': 'A hombros de gigantes',
198 'url': 'https://www.rtve.es/play/audios/en-radio-3/ignatius-farray/5791165/',
199 'md5': '072855ab89a9450e0ba314c717fa5ebc',
203 'title': 'Ignatius Farray',
204 'thumbnail': r
're:https?://.+/1613243011863.jpg',
205 'duration': 3559.559,
206 'series': 'En Radio 3'
209 'url': 'https://www.rtve.es/play/audios/frankenstein-o-el-moderno-prometeo/capitulo-26-ultimo-muerte-victor-juan-jose-plans-mary-shelley/6082623/',
210 'md5': '0eadab248cc8dd193fa5765712e84d5c',
214 'title': 'Capítulo 26 y último: La muerte de Victor',
215 'thumbnail': r
're:https?://.+/1632147445707.jpg',
216 'duration': 3174.086,
217 'series': 'Frankenstein o el moderno Prometeo'
221 def _extract_png_formats(self
, audio_id
):
223 This function retrieves media related png thumbnail which obfuscate
224 valuable information about the media. This information is decrypted
225 via base class _decrypt_url function providing media quality and
228 png
= self
._download
_webpage
(
229 'http://www.rtve.es/ztnr/movil/thumbnail/%s/audios/%s.png' %
230 (self
._manager
, audio_id
),
231 audio_id
, 'Downloading url information', query
={'q': 'v2'}
)
232 q
= qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL'])
234 for quality
, audio_url
in self
._decrypt
_url
(png
):
235 ext
= determine_ext(audio_url
)
237 formats
.extend(self
._extract
_m
3u8_formats
(
238 audio_url
, audio_id
, 'mp4', 'm3u8_native',
239 m3u8_id
='hls', fatal
=False))
241 formats
.extend(self
._extract
_mpd
_formats
(
242 audio_url
, audio_id
, 'dash', fatal
=False))
245 'format_id': quality
,
246 'quality': q(quality
),
249 self
._sort
_formats
(formats
)
252 def _real_extract(self
, url
):
253 audio_id
= self
._match
_id
(url
)
254 info
= self
._download
_json
(
255 'https://www.rtve.es/api/audios/%s.json' % audio_id
,
256 audio_id
)['page']['items'][0]
260 'title': info
['title'].strip(),
261 'thumbnail': info
.get('thumbnail'),
262 'duration': float_or_none(info
.get('duration'), 1000),
263 'series': try_get(info
, lambda x
: x
['programInfo']['title']),
264 'formats': self
._extract
_png
_formats
(audio_id
),
268 class RTVEInfantilIE(RTVEALaCartaIE
):
269 IE_NAME
= 'rtve.es:infantil'
270 IE_DESC
= 'RTVE infantil'
271 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/infantil/serie/[^/]+/video/[^/]+/(?P<id>[0-9]+)/'
274 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
275 'md5': '5747454717aedf9f9fdf212d1bcfc48d',
279 'title': 'Maneras de vivir',
280 'thumbnail': r
're:https?://.+/1426182947956\.JPG',
283 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
287 class RTVELiveIE(RTVEALaCartaIE
):
288 IE_NAME
= 'rtve.es:live'
289 IE_DESC
= 'RTVE.es live streams'
290 _VALID_URL
= r
'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
293 'url': 'http://www.rtve.es/directo/la-1/',
297 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
300 'skip_download': 'live stream',
304 def _real_extract(self
, url
):
305 mobj
= self
._match
_valid
_url
(url
)
306 video_id
= mobj
.group('id')
308 webpage
= self
._download
_webpage
(url
, video_id
)
309 title
= remove_end(self
._og
_search
_title
(webpage
), ' en directo en RTVE.es')
310 title
= remove_start(title
, 'Estoy viendo ')
312 vidplayer_id
= self
._search
_regex
(
313 (r
'playerId=player([0-9]+)',
314 r
'class=["\'].*?
\blive
_mod
\b.*?
["\'][^>]+data-assetid=["\'](\d
+)',
315 r'data
-id=["\'](\d+)'),
316 webpage, 'internal video ID')
321 'formats': self._extract_png_formats(vidplayer_id),
326 class RTVETelevisionIE(InfoExtractor):
327 IE_NAME = 'rtve.es:television'
328 _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml'
331 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml',
335 'title': 'Documentos TV - La revolución del móvil',
336 'duration': 3496.948,
339 'skip_download': True,
343 def _real_extract(self, url):
344 page_id = self._match_id(url)
345 webpage = self._download_webpage(url, page_id)
347 alacarta_url = self._search_regex(
348 r'data-location="alacarta_videos
"[^<]+url":"(http://www\.rtve\.es/alacarta.+?)&',
349 webpage, 'alacarta url', default=None)
350 if alacarta_url is None:
351 raise ExtractorError(
352 'The webpage doesn\'t contain any video', expected=True)
354 return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key())