]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/rtve.py
59832eeac813ec901008f0735ad6ffe32e8dca20
[yt-dlp.git] / yt_dlp / extractor / rtve.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import io
6 import sys
7
8 from .common import InfoExtractor
9 from ..compat import (
10 compat_b64decode,
11 compat_struct_unpack,
12 )
13 from ..utils import (
14 determine_ext,
15 ExtractorError,
16 float_or_none,
17 qualities,
18 remove_end,
19 remove_start,
20 std_headers,
21 )
22
23 _bytes_to_chr = (lambda x: x) if sys.version_info[0] == 2 else (lambda x: map(chr, x))
24
25
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+)'
30
31 _TESTS = [{
32 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
33 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
34 'info_dict': {
35 'id': '2491869',
36 'ext': 'mp4',
37 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
38 'duration': 5024.566,
39 'series': 'Balonmano',
40 },
41 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
42 }, {
43 'note': 'Live stream',
44 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
45 'info_dict': {
46 'id': '1694255',
47 'ext': 'mp4',
48 'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
49 'is_live': True,
50 },
51 'params': {
52 'skip_download': 'live stream',
53 },
54 }, {
55 'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/',
56 'md5': 'd850f3c8731ea53952ebab489cf81cbf',
57 'info_dict': {
58 'id': '4236788',
59 'ext': 'mp4',
60 'title': 'Servir y proteger - Capítulo 104',
61 'duration': 3222.0,
62 },
63 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
64 }, {
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,
67 }, {
68 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/',
69 'only_matching': True,
70 }]
71
72 def _real_initialize(self):
73 user_agent_b64 = base64.b64encode(std_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']
77
78 @staticmethod
79 def _decrypt_url(png):
80 encrypted_data = io.BytesIO(compat_b64decode(png)[8:])
81 while True:
82 length = compat_struct_unpack('!I', encrypted_data.read(4))[0]
83 chunk_type = encrypted_data.read(4)
84 if chunk_type == b'IEND':
85 break
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'%%')
90 alphabet = []
91 e = 0
92 d = 0
93 for l in _bytes_to_chr(alphabet_data):
94 if d == 0:
95 alphabet.append(l)
96 d = e = (e + 1) % 4
97 else:
98 d -= 1
99 url = ''
100 f = 0
101 e = 3
102 b = 1
103 for letter in _bytes_to_chr(url_data):
104 if f == 0:
105 l = int(letter) * 10
106 f = 1
107 else:
108 if e == 0:
109 l += int(letter)
110 url += alphabet[l]
111 e = (b + 3) % 4
112 f = 0
113 b += 1
114 else:
115 e -= 1
116
117 yield quality.decode(), url
118 encrypted_data.read(4) # CRC
119
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'])
125 formats = []
126 for quality, video_url in self._decrypt_url(png):
127 ext = determine_ext(video_url)
128 if ext == 'm3u8':
129 formats.extend(self._extract_m3u8_formats(
130 video_url, video_id, 'mp4', 'm3u8_native',
131 m3u8_id='hls', fatal=False))
132 elif ext == 'mpd':
133 formats.extend(self._extract_mpd_formats(
134 video_url, video_id, 'dash', fatal=False))
135 else:
136 formats.append({
137 'format_id': quality,
138 'quality': q(quality),
139 'url': video_url,
140 })
141 self._sort_formats(formats)
142 return formats
143
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)
153
154 subtitles = None
155 sbt_file = info.get('sbtFile')
156 if sbt_file:
157 subtitles = self.extract_subtitles(video_id, sbt_file)
158
159 is_live = info.get('live') is True
160
161 return {
162 'id': video_id,
163 'title': self._live_title(title) if is_live else title,
164 'formats': formats,
165 'thumbnail': info.get('image'),
166 'subtitles': subtitles,
167 'duration': float_or_none(info.get('duration'), 1000),
168 'is_live': is_live,
169 'series': info.get('programTitle'),
170 }
171
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']
176 return dict(
177 (s['lang'], [{'ext': 'vtt', 'url': s['src']}])
178 for s in subs)
179
180
181 class RTVEInfantilIE(RTVEALaCartaIE):
182 IE_NAME = 'rtve.es:infantil'
183 IE_DESC = 'RTVE infantil'
184 _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/[^/]+/video/[^/]+/(?P<id>[0-9]+)/'
185
186 _TESTS = [{
187 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
188 'md5': '5747454717aedf9f9fdf212d1bcfc48d',
189 'info_dict': {
190 'id': '3040283',
191 'ext': 'mp4',
192 'title': 'Maneras de vivir',
193 'thumbnail': r're:https?://.+/1426182947956\.JPG',
194 'duration': 357.958,
195 },
196 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
197 }]
198
199
200 class RTVELiveIE(RTVEALaCartaIE):
201 IE_NAME = 'rtve.es:live'
202 IE_DESC = 'RTVE.es live streams'
203 _VALID_URL = r'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
204
205 _TESTS = [{
206 'url': 'http://www.rtve.es/directo/la-1/',
207 'info_dict': {
208 'id': 'la-1',
209 'ext': 'mp4',
210 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
211 },
212 'params': {
213 'skip_download': 'live stream',
214 }
215 }]
216
217 def _real_extract(self, url):
218 mobj = self._match_valid_url(url)
219 video_id = mobj.group('id')
220
221 webpage = self._download_webpage(url, video_id)
222 title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es')
223 title = remove_start(title, 'Estoy viendo ')
224
225 vidplayer_id = self._search_regex(
226 (r'playerId=player([0-9]+)',
227 r'class=["\'].*?\blive_mod\b.*?["\'][^>]+data-assetid=["\'](\d+)',
228 r'data-id=["\'](\d+)'),
229 webpage, 'internal video ID')
230
231 return {
232 'id': video_id,
233 'title': self._live_title(title),
234 'formats': self._extract_png_formats(vidplayer_id),
235 'is_live': True,
236 }
237
238
239 class RTVETelevisionIE(InfoExtractor):
240 IE_NAME = 'rtve.es:television'
241 _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml'
242
243 _TEST = {
244 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml',
245 'info_dict': {
246 'id': '3069778',
247 'ext': 'mp4',
248 'title': 'Documentos TV - La revolución del móvil',
249 'duration': 3496.948,
250 },
251 'params': {
252 'skip_download': True,
253 },
254 }
255
256 def _real_extract(self, url):
257 page_id = self._match_id(url)
258 webpage = self._download_webpage(url, page_id)
259
260 alacarta_url = self._search_regex(
261 r'data-location="alacarta_videos"[^<]+url&quot;:&quot;(http://www\.rtve\.es/alacarta.+?)&',
262 webpage, 'alacarta url', default=None)
263 if alacarta_url is None:
264 raise ExtractorError(
265 'The webpage doesn\'t contain any video', expected=True)
266
267 return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key())