2 from __future__ import unicode_literals
8 from .common import InfoExtractor
9 from ..compat import compat_urlparse
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:]
32 for l in alphabet_data:
42 for letter in url_data:
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+)'
65 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
66 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
70 'title': 'Balonmano - Swiss Cup masculina. Final: EspaƱa-Suecia',
74 'note': 'Live stream',
75 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
81 'skip': 'The f4m manifest can\'t be used yet',
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,
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)
108 if info.get('sbtFile') is not None:
109 subtitles = self.extract_subtitles(video_id, info['sbtFile'])
113 'title': info['title'],
115 'thumbnail': info.get('image'),
117 'subtitles': subtitles,
118 'duration': float_or_none(info.get('duration'), scale=1000),
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']
126 (s['lang'], [{'ext': 'vtt', 'url': s['src']}])
130 class RTVELiveIE(InfoExtractor):
131 IE_NAME = 'rtve.es:live'
132 IE_DESC = 'RTVE.es live streams'
133 _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
136 'url': 'http://www.rtve.es/noticias/directo-la-1/',
138 'id': 'directo-la-1',
140 'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
143 'skip_download': 'live stream',
147 def _real_extract(self, url):
148 mobj = re.match(self._VALID_URL, url)
149 start_time = time.gmtime()
150 video_id = mobj.group('id')
152 webpage = self._download_webpage(url, video_id)
153 player_url = self._search_regex(
154 r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
155 title = remove_end(self._og_search_title(webpage), ' en directo')
156 title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
158 vidplayer_id = self._search_regex(
159 r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
160 png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
161 png = self._download_webpage(png_url, video_id, 'Downloading url information')
162 video_url = _decrypt_url(png)
169 'app': 'rtve-live-live?ovpfv=2.1.2',
170 'player_url': player_url,