]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/rtve.py
Add support for https for all extractors as preventive and future-proof measure
[yt-dlp.git] / youtube_dl / extractor / rtve.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import base64
5 import re
6 import time
7
8 from .common import InfoExtractor
9 from ..utils import (
10 ExtractorError,
11 float_or_none,
12 remove_end,
13 remove_start,
14 sanitized_Request,
15 std_headers,
16 struct_unpack,
17 )
18
19
20 def _decrypt_url(png):
21 encrypted_data = base64.b64decode(png.encode('utf-8'))
22 text_index = encrypted_data.find(b'tEXt')
23 text_chunk = encrypted_data[text_index - 4:]
24 length = struct_unpack('!I', text_chunk[:4])[0]
25 # Use bytearray to get integers when iterating in both python 2.x and 3.x
26 data = bytearray(text_chunk[8:8 + length])
27 data = [chr(b) for b in data if b != 0]
28 hash_index = data.index('#')
29 alphabet_data = data[:hash_index]
30 url_data = data[hash_index + 1:]
31
32 alphabet = []
33 e = 0
34 d = 0
35 for l in alphabet_data:
36 if d == 0:
37 alphabet.append(l)
38 d = e = (e + 1) % 4
39 else:
40 d -= 1
41 url = ''
42 f = 0
43 e = 3
44 b = 1
45 for letter in url_data:
46 if f == 0:
47 l = int(letter) * 10
48 f = 1
49 else:
50 if e == 0:
51 l += int(letter)
52 url += alphabet[l]
53 e = (b + 3) % 4
54 f = 0
55 b += 1
56 else:
57 e -= 1
58
59 return url
60
61
62 class RTVEALaCartaIE(InfoExtractor):
63 IE_NAME = 'rtve.es:alacarta'
64 IE_DESC = 'RTVE a la carta'
65 _VALID_URL = r'https?://www\.rtve\.es/(m/)?alacarta/videos/[^/]+/[^/]+/(?P<id>\d+)'
66
67 _TESTS = [{
68 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
69 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
70 'info_dict': {
71 'id': '2491869',
72 'ext': 'mp4',
73 'title': 'Balonmano - Swiss Cup masculina. Final: EspaƱa-Suecia',
74 'duration': 5024.566,
75 },
76 }, {
77 'note': 'Live stream',
78 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
79 'info_dict': {
80 'id': '1694255',
81 'ext': 'flv',
82 'title': 'TODO',
83 },
84 'skip': 'The f4m manifest can\'t be used yet',
85 }, {
86 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
87 'only_matching': True,
88 }]
89
90 def _real_initialize(self):
91 user_agent_b64 = base64.b64encode(std_headers['User-Agent'].encode('utf-8')).decode('utf-8')
92 manager_info = self._download_json(
93 'http://www.rtve.es/odin/loki/' + user_agent_b64,
94 None, 'Fetching manager info')
95 self._manager = manager_info['manager']
96
97 def _real_extract(self, url):
98 mobj = re.match(self._VALID_URL, url)
99 video_id = mobj.group('id')
100 info = self._download_json(
101 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
102 video_id)['page']['items'][0]
103 if info['state'] == 'DESPU':
104 raise ExtractorError('The video is no longer available', expected=True)
105 png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self._manager, video_id)
106 png_request = sanitized_Request(png_url)
107 png_request.add_header('Referer', url)
108 png = self._download_webpage(png_request, video_id, 'Downloading url information')
109 video_url = _decrypt_url(png)
110 if not video_url.endswith('.f4m'):
111 video_url = video_url.replace(
112 'resources/', 'auth/resources/'
113 ).replace('.net.rtve', '.multimedia.cdn.rtve')
114
115 subtitles = None
116 if info.get('sbtFile') is not None:
117 subtitles = self.extract_subtitles(video_id, info['sbtFile'])
118
119 return {
120 'id': video_id,
121 'title': info['title'],
122 'url': video_url,
123 'thumbnail': info.get('image'),
124 'page_url': url,
125 'subtitles': subtitles,
126 'duration': float_or_none(info.get('duration'), scale=1000),
127 }
128
129 def _get_subtitles(self, video_id, sub_file):
130 subs = self._download_json(
131 sub_file + '.json', video_id,
132 'Downloading subtitles info')['page']['items']
133 return dict(
134 (s['lang'], [{'ext': 'vtt', 'url': s['src']}])
135 for s in subs)
136
137
138 class RTVEInfantilIE(InfoExtractor):
139 IE_NAME = 'rtve.es:infantil'
140 IE_DESC = 'RTVE infantil'
141 _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/(?P<show>[^/]*)/video/(?P<short_title>[^/]*)/(?P<id>[0-9]+)/'
142
143 _TESTS = [{
144 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
145 'md5': '915319587b33720b8e0357caaa6617e6',
146 'info_dict': {
147 'id': '3040283',
148 'ext': 'mp4',
149 'title': 'Maneras de vivir',
150 'thumbnail': 'http://www.rtve.es/resources/jpg/6/5/1426182947956.JPG',
151 'duration': 357.958,
152 },
153 }]
154
155 def _real_extract(self, url):
156 video_id = self._match_id(url)
157 info = self._download_json(
158 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
159 video_id)['page']['items'][0]
160
161 webpage = self._download_webpage(url, video_id)
162 vidplayer_id = self._search_regex(
163 r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
164
165 png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
166 png = self._download_webpage(png_url, video_id, 'Downloading url information')
167 video_url = _decrypt_url(png)
168
169 return {
170 'id': video_id,
171 'ext': 'mp4',
172 'title': info['title'],
173 'url': video_url,
174 'thumbnail': info.get('image'),
175 'duration': float_or_none(info.get('duration'), scale=1000),
176 }
177
178
179 class RTVELiveIE(InfoExtractor):
180 IE_NAME = 'rtve.es:live'
181 IE_DESC = 'RTVE.es live streams'
182 _VALID_URL = r'https?://www\.rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
183
184 _TESTS = [{
185 'url': 'http://www.rtve.es/directo/la-1/',
186 'info_dict': {
187 'id': 'la-1',
188 'ext': 'mp4',
189 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
190 },
191 'params': {
192 'skip_download': 'live stream',
193 }
194 }]
195
196 def _real_extract(self, url):
197 mobj = re.match(self._VALID_URL, url)
198 start_time = time.gmtime()
199 video_id = mobj.group('id')
200
201 webpage = self._download_webpage(url, video_id)
202 title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es')
203 title = remove_start(title, 'Estoy viendo ')
204 title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
205
206 vidplayer_id = self._search_regex(
207 r'playerId=player([0-9]+)', webpage, 'internal video ID')
208 png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/amonet/videos/%s.png' % vidplayer_id
209 png = self._download_webpage(png_url, video_id, 'Downloading url information')
210 m3u8_url = _decrypt_url(png)
211 formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4')
212
213 return {
214 'id': video_id,
215 'title': title,
216 'formats': formats,
217 'is_live': True,
218 }