]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
91a6adde JMF |
2 | from __future__ import unicode_literals |
3 | ||
91a6adde | 4 | import base64 |
2b9faf55 PH |
5 | import re |
6 | import time | |
91a6adde JMF |
7 | |
8 | from .common import InfoExtractor | |
dab0daee | 9 | from ..compat import ( |
edaa23f8 | 10 | compat_struct_unpack, |
dab0daee | 11 | ) |
91a6adde | 12 | from ..utils import ( |
ce73839f | 13 | ExtractorError, |
f3bff94c | 14 | float_or_none, |
2b9faf55 | 15 | remove_end, |
3e769682 | 16 | remove_start, |
5c2266df | 17 | sanitized_Request, |
e9f65f87 | 18 | std_headers, |
91a6adde JMF |
19 | ) |
20 | ||
21 | ||
2b9faf55 | 22 | def _decrypt_url(png): |
afe8b594 | 23 | encrypted_data = base64.b64decode(png.encode('utf-8')) |
2b9faf55 PH |
24 | text_index = encrypted_data.find(b'tEXt') |
25 | text_chunk = encrypted_data[text_index - 4:] | |
edaa23f8 | 26 | length = compat_struct_unpack('!I', text_chunk[:4])[0] |
2b9faf55 PH |
27 | # Use bytearray to get integers when iterating in both python 2.x and 3.x |
28 | data = bytearray(text_chunk[8:8 + length]) | |
29 | data = [chr(b) for b in data if b != 0] | |
30 | hash_index = data.index('#') | |
31 | alphabet_data = data[:hash_index] | |
32 | url_data = data[hash_index + 1:] | |
33 | ||
34 | alphabet = [] | |
35 | e = 0 | |
36 | d = 0 | |
37 | for l in alphabet_data: | |
38 | if d == 0: | |
39 | alphabet.append(l) | |
40 | d = e = (e + 1) % 4 | |
41 | else: | |
42 | d -= 1 | |
43 | url = '' | |
44 | f = 0 | |
45 | e = 3 | |
46 | b = 1 | |
47 | for letter in url_data: | |
48 | if f == 0: | |
49 | l = int(letter) * 10 | |
50 | f = 1 | |
51 | else: | |
52 | if e == 0: | |
53 | l += int(letter) | |
54 | url += alphabet[l] | |
55 | e = (b + 3) % 4 | |
56 | f = 0 | |
57 | b += 1 | |
58 | else: | |
59 | e -= 1 | |
60 | ||
61 | return url | |
62 | ||
63 | ||
91a6adde JMF |
64 | class RTVEALaCartaIE(InfoExtractor): |
65 | IE_NAME = 'rtve.es:alacarta' | |
66 | IE_DESC = 'RTVE a la carta' | |
92519402 | 67 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)' |
91a6adde | 68 | |
2b9faf55 | 69 | _TESTS = [{ |
91a6adde | 70 | 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/', |
9aeaf730 | 71 | 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43', |
91a6adde JMF |
72 | 'info_dict': { |
73 | 'id': '2491869', | |
74 | 'ext': 'mp4', | |
75 | 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia', | |
f3bff94c | 76 | 'duration': 5024.566, |
91a6adde | 77 | }, |
2b9faf55 PH |
78 | }, { |
79 | 'note': 'Live stream', | |
80 | 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/', | |
81 | 'info_dict': { | |
82 | 'id': '1694255', | |
83 | 'ext': 'flv', | |
84 | 'title': 'TODO', | |
cc57bd33 JMF |
85 | }, |
86 | 'skip': 'The f4m manifest can\'t be used yet', | |
cd596028 JMF |
87 | }, { |
88 | 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve', | |
89 | 'only_matching': True, | |
4c718d3c JMF |
90 | }, { |
91 | 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/', | |
92 | 'only_matching': True, | |
2b9faf55 | 93 | }] |
91a6adde | 94 | |
e9f65f87 JMF |
95 | def _real_initialize(self): |
96 | user_agent_b64 = base64.b64encode(std_headers['User-Agent'].encode('utf-8')).decode('utf-8') | |
97 | manager_info = self._download_json( | |
98 | 'http://www.rtve.es/odin/loki/' + user_agent_b64, | |
99 | None, 'Fetching manager info') | |
100 | self._manager = manager_info['manager'] | |
101 | ||
91a6adde JMF |
102 | def _real_extract(self, url): |
103 | mobj = re.match(self._VALID_URL, url) | |
104 | video_id = mobj.group('id') | |
105 | info = self._download_json( | |
106 | 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id, | |
107 | video_id)['page']['items'][0] | |
ce73839f JMF |
108 | if info['state'] == 'DESPU': |
109 | raise ExtractorError('The video is no longer available', expected=True) | |
e9f65f87 | 110 | png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self._manager, video_id) |
5c2266df | 111 | png_request = sanitized_Request(png_url) |
080997b8 JMF |
112 | png_request.add_header('Referer', url) |
113 | png = self._download_webpage(png_request, video_id, 'Downloading url information') | |
2b9faf55 | 114 | video_url = _decrypt_url(png) |
cc57bd33 | 115 | if not video_url.endswith('.f4m'): |
da0baba5 JMF |
116 | if '?' not in video_url: |
117 | video_url = video_url.replace('resources/', 'auth/resources/') | |
118 | video_url = video_url.replace('.net.rtve', '.multimedia.cdn.rtve') | |
91a6adde | 119 | |
25ac63ed JMF |
120 | subtitles = None |
121 | if info.get('sbtFile') is not None: | |
122 | subtitles = self.extract_subtitles(video_id, info['sbtFile']) | |
123 | ||
91a6adde JMF |
124 | return { |
125 | 'id': video_id, | |
126 | 'title': info['title'], | |
127 | 'url': video_url, | |
2b9faf55 PH |
128 | 'thumbnail': info.get('image'), |
129 | 'page_url': url, | |
25ac63ed | 130 | 'subtitles': subtitles, |
f3bff94c | 131 | 'duration': float_or_none(info.get('duration'), scale=1000), |
2b9faf55 PH |
132 | } |
133 | ||
25ac63ed JMF |
134 | def _get_subtitles(self, video_id, sub_file): |
135 | subs = self._download_json( | |
136 | sub_file + '.json', video_id, | |
137 | 'Downloading subtitles info')['page']['items'] | |
9c665ab7 PH |
138 | return dict( |
139 | (s['lang'], [{'ext': 'vtt', 'url': s['src']}]) | |
25ac63ed JMF |
140 | for s in subs) |
141 | ||
b68eedba | 142 | |
d5b55939 | 143 | class RTVEInfantilIE(InfoExtractor): |
b68eedba JMF |
144 | IE_NAME = 'rtve.es:infantil' |
145 | IE_DESC = 'RTVE infantil' | |
146 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/(?P<show>[^/]*)/video/(?P<short_title>[^/]*)/(?P<id>[0-9]+)/' | |
d5b55939 EF |
147 | |
148 | _TESTS = [{ | |
149 | 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/', | |
150 | 'md5': '915319587b33720b8e0357caaa6617e6', | |
151 | 'info_dict': { | |
152 | 'id': '3040283', | |
153 | 'ext': 'mp4', | |
154 | 'title': 'Maneras de vivir', | |
155 | 'thumbnail': 'http://www.rtve.es/resources/jpg/6/5/1426182947956.JPG', | |
156 | 'duration': 357.958, | |
157 | }, | |
b68eedba | 158 | }] |
d5b55939 EF |
159 | |
160 | def _real_extract(self, url): | |
b68eedba | 161 | video_id = self._match_id(url) |
d5b55939 EF |
162 | info = self._download_json( |
163 | 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id, | |
164 | video_id)['page']['items'][0] | |
165 | ||
166 | webpage = self._download_webpage(url, video_id) | |
167 | vidplayer_id = self._search_regex( | |
168 | r' id="vidplayer([0-9]+)"', webpage, 'internal video ID') | |
169 | ||
170 | png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id | |
171 | png = self._download_webpage(png_url, video_id, 'Downloading url information') | |
172 | video_url = _decrypt_url(png) | |
173 | ||
174 | return { | |
175 | 'id': video_id, | |
176 | 'ext': 'mp4', | |
177 | 'title': info['title'], | |
178 | 'url': video_url, | |
179 | 'thumbnail': info.get('image'), | |
180 | 'duration': float_or_none(info.get('duration'), scale=1000), | |
181 | } | |
182 | ||
183 | ||
2b9faf55 PH |
184 | class RTVELiveIE(InfoExtractor): |
185 | IE_NAME = 'rtve.es:live' | |
186 | IE_DESC = 'RTVE.es live streams' | |
92519402 | 187 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)' |
2b9faf55 PH |
188 | |
189 | _TESTS = [{ | |
3e769682 | 190 | 'url': 'http://www.rtve.es/directo/la-1/', |
2b9faf55 | 191 | 'info_dict': { |
3e769682 JMF |
192 | 'id': 'la-1', |
193 | 'ext': 'mp4', | |
194 | 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$', | |
2b9faf55 PH |
195 | }, |
196 | 'params': { | |
197 | 'skip_download': 'live stream', | |
198 | } | |
199 | }] | |
200 | ||
201 | def _real_extract(self, url): | |
202 | mobj = re.match(self._VALID_URL, url) | |
203 | start_time = time.gmtime() | |
204 | video_id = mobj.group('id') | |
205 | ||
206 | webpage = self._download_webpage(url, video_id) | |
3e769682 JMF |
207 | title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es') |
208 | title = remove_start(title, 'Estoy viendo ') | |
2b9faf55 PH |
209 | title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time) |
210 | ||
211 | vidplayer_id = self._search_regex( | |
b63005f5 S |
212 | (r'playerId=player([0-9]+)', |
213 | r'class=["\'].*?\blive_mod\b.*?["\'][^>]+data-assetid=["\'](\d+)', | |
214 | r'data-id=["\'](\d+)'), | |
215 | webpage, 'internal video ID') | |
3e769682 | 216 | png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/amonet/videos/%s.png' % vidplayer_id |
2b9faf55 | 217 | png = self._download_webpage(png_url, video_id, 'Downloading url information') |
3e769682 JMF |
218 | m3u8_url = _decrypt_url(png) |
219 | formats = self._extract_m3u8_formats(m3u8_url, video_id, ext='mp4') | |
19dbaeec | 220 | self._sort_formats(formats) |
2b9faf55 | 221 | |
2b9faf55 PH |
222 | return { |
223 | 'id': video_id, | |
2b9faf55 | 224 | 'title': title, |
3e769682 JMF |
225 | 'formats': formats, |
226 | 'is_live': True, | |
91a6adde | 227 | } |
233b58de JMF |
228 | |
229 | ||
230 | class RTVETelevisionIE(InfoExtractor): | |
231 | IE_NAME = 'rtve.es:television' | |
92519402 | 232 | _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml' |
233b58de JMF |
233 | |
234 | _TEST = { | |
235 | 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml', | |
236 | 'info_dict': { | |
237 | 'id': '3069778', | |
238 | 'ext': 'mp4', | |
239 | 'title': 'Documentos TV - La revolución del móvil', | |
240 | 'duration': 3496.948, | |
241 | }, | |
242 | 'params': { | |
243 | 'skip_download': True, | |
244 | }, | |
245 | } | |
246 | ||
247 | def _real_extract(self, url): | |
248 | page_id = self._match_id(url) | |
249 | webpage = self._download_webpage(url, page_id) | |
250 | ||
251 | alacarta_url = self._search_regex( | |
252 | r'data-location="alacarta_videos"[^<]+url":"(http://www\.rtve\.es/alacarta.+?)&', | |
253 | webpage, 'alacarta url', default=None) | |
254 | if alacarta_url is None: | |
255 | raise ExtractorError( | |
256 | 'The webpage doesn\'t contain any video', expected=True) | |
257 | ||
258 | return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key()) |