[rtve] Fix extraction
[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 ..utils import (
10     struct_unpack,
11     remove_end,
12 )
13
14
15 def _decrypt_url(png):
16     encrypted_data = base64.b64decode(png)
17     text_index = encrypted_data.find(b'tEXt')
18     text_chunk = encrypted_data[text_index - 4:]
19     length = struct_unpack('!I', text_chunk[:4])[0]
20     # Use bytearray to get integers when iterating in both python 2.x and 3.x
21     data = bytearray(text_chunk[8:8 + length])
22     data = [chr(b) for b in data if b != 0]
23     hash_index = data.index('#')
24     alphabet_data = data[:hash_index]
25     url_data = data[hash_index + 1:]
26
27     alphabet = []
28     e = 0
29     d = 0
30     for l in alphabet_data:
31         if d == 0:
32             alphabet.append(l)
33             d = e = (e + 1) % 4
34         else:
35             d -= 1
36     url = ''
37     f = 0
38     e = 3
39     b = 1
40     for letter in url_data:
41         if f == 0:
42             l = int(letter) * 10
43             f = 1
44         else:
45             if e == 0:
46                 l += int(letter)
47                 url += alphabet[l]
48                 e = (b + 3) % 4
49                 f = 0
50                 b += 1
51             else:
52                 e -= 1
53
54     return url
55
56
57 class RTVEALaCartaIE(InfoExtractor):
58     IE_NAME = 'rtve.es:alacarta'
59     IE_DESC = 'RTVE a la carta'
60     _VALID_URL = r'http://www\.rtve\.es/alacarta/videos/[^/]+/[^/]+/(?P<id>\d+)'
61
62     _TESTS = [{
63         'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
64         'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
65         'info_dict': {
66             'id': '2491869',
67             'ext': 'mp4',
68             'title': 'Balonmano - Swiss Cup masculina. Final: EspaƱa-Suecia',
69         },
70     }, {
71         'note': 'Live stream',
72         'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
73         'info_dict': {
74             'id': '1694255',
75             'ext': 'flv',
76             'title': 'TODO',
77         },
78         'skip': 'The f4m manifest can\'t be used yet',
79     }]
80
81     def _real_extract(self, url):
82         mobj = re.match(self._VALID_URL, url)
83         video_id = mobj.group('id')
84         info = self._download_json(
85             'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
86             video_id)['page']['items'][0]
87         png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % video_id
88         png = self._download_webpage(png_url, video_id, 'Downloading url information')
89         video_url = _decrypt_url(png)
90         if not video_url.endswith('.f4m'):
91             auth_url = video_url.replace(
92                 'resources/', 'auth/resources/'
93             ).replace('.net.rtve', '.multimedia.cdn.rtve')
94             video_path = self._download_webpage(
95                 auth_url, video_id, 'Getting video url')
96             # Use mvod.akcdn instead of flash.akamaihd.multimedia.cdn to get
97             # the right Content-Length header and the mp4 format
98             video_url = (
99                 'http://mvod.akcdn.rtve.es/{0}&v=2.6.8'
100                 '&fp=MAC%2016,0,0,296&r=MRUGG&g=OEOJWFXNFGCP'.format(video_path)
101             )
102
103         return {
104             'id': video_id,
105             'title': info['title'],
106             'url': video_url,
107             'thumbnail': info.get('image'),
108             'page_url': url,
109         }
110
111
112 class RTVELiveIE(InfoExtractor):
113     IE_NAME = 'rtve.es:live'
114     IE_DESC = 'RTVE.es live streams'
115     _VALID_URL = r'http://www\.rtve\.es/(?:deportes/directo|noticias|television)/(?P<id>[a-zA-Z0-9-]+)'
116
117     _TESTS = [{
118         'url': 'http://www.rtve.es/noticias/directo-la-1/',
119         'info_dict': {
120             'id': 'directo-la-1',
121             'ext': 'flv',
122             'title': 're:^La 1 de TVE [0-9]{4}-[0-9]{2}-[0-9]{2}Z[0-9]{6}$',
123         },
124         'params': {
125             'skip_download': 'live stream',
126         }
127     }]
128
129     def _real_extract(self, url):
130         mobj = re.match(self._VALID_URL, url)
131         start_time = time.gmtime()
132         video_id = mobj.group('id')
133
134         webpage = self._download_webpage(url, video_id)
135         player_url = self._search_regex(
136             r'<param name="movie" value="([^"]+)"/>', webpage, 'player URL')
137         title = remove_end(self._og_search_title(webpage), ' en directo')
138         title += ' ' + time.strftime('%Y-%m-%dZ%H%M%S', start_time)
139
140         vidplayer_id = self._search_regex(
141             r' id="vidplayer([0-9]+)"', webpage, 'internal video ID')
142         png_url = 'http://www.rtve.es/ztnr/movil/thumbnail/default/videos/%s.png' % vidplayer_id
143         png = self._download_webpage(png_url, video_id, 'Downloading url information')
144         video_url = _decrypt_url(png)
145
146         return {
147             'id': video_id,
148             'ext': 'flv',
149             'title': title,
150             'url': video_url,
151             'app': 'rtve-live-live?ovpfv=2.1.2',
152             'player_url': player_url,
153             'rtmp_live': True,
154         }