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