]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/rtve.py
[youtube:comments] Add more options for limiting number of comments extracted (#1626)
[yt-dlp.git] / yt_dlp / extractor / rtve.py
CommitLineData
dcdb292f 1# coding: utf-8
91a6adde
JMF
2from __future__ import unicode_literals
3
91a6adde 4import base64
10db0d2f 5import io
10db0d2f 6import sys
91a6adde
JMF
7
8from .common import InfoExtractor
dab0daee 9from ..compat import (
cf282071 10 compat_b64decode,
edaa23f8 11 compat_struct_unpack,
dab0daee 12)
91a6adde 13from ..utils import (
2c53bd51 14 determine_ext,
ce73839f 15 ExtractorError,
f3bff94c 16 float_or_none,
10db0d2f 17 qualities,
2b9faf55 18 remove_end,
3e769682 19 remove_start,
e9f65f87 20 std_headers,
91a6adde
JMF
21)
22
10db0d2f 23_bytes_to_chr = (lambda x: x) if sys.version_info[0] == 2 else (lambda x: map(chr, x))
2b9faf55
PH
24
25
91a6adde
JMF
26class RTVEALaCartaIE(InfoExtractor):
27 IE_NAME = 'rtve.es:alacarta'
28 IE_DESC = 'RTVE a la carta'
92519402 29 _VALID_URL = r'https?://(?:www\.)?rtve\.es/(m/)?(alacarta/videos|filmoteca)/[^/]+/[^/]+/(?P<id>\d+)'
91a6adde 30
2b9faf55 31 _TESTS = [{
91a6adde 32 'url': 'http://www.rtve.es/alacarta/videos/balonmano/o-swiss-cup-masculina-final-espana-suecia/2491869/',
9aeaf730 33 'md5': '1d49b7e1ca7a7502c56a4bf1b60f1b43',
91a6adde
JMF
34 'info_dict': {
35 'id': '2491869',
36 'ext': 'mp4',
37 'title': 'Balonmano - Swiss Cup masculina. Final: España-Suecia',
f3bff94c 38 'duration': 5024.566,
10db0d2f 39 'series': 'Balonmano',
91a6adde 40 },
10db0d2f 41 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
2b9faf55
PH
42 }, {
43 'note': 'Live stream',
44 'url': 'http://www.rtve.es/alacarta/videos/television/24h-live/1694255/',
45 'info_dict': {
46 'id': '1694255',
10db0d2f 47 'ext': 'mp4',
48 'title': 're:^24H LIVE [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
49 'is_live': True,
50 },
51 'params': {
52 'skip_download': 'live stream',
cc57bd33 53 },
2c53bd51
GF
54 }, {
55 'url': 'http://www.rtve.es/alacarta/videos/servir-y-proteger/servir-proteger-capitulo-104/4236788/',
10db0d2f 56 'md5': 'd850f3c8731ea53952ebab489cf81cbf',
2c53bd51
GF
57 'info_dict': {
58 'id': '4236788',
59 'ext': 'mp4',
10db0d2f 60 'title': 'Servir y proteger - Capítulo 104',
2c53bd51
GF
61 'duration': 3222.0,
62 },
10db0d2f 63 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
cd596028
JMF
64 }, {
65 'url': 'http://www.rtve.es/m/alacarta/videos/cuentame-como-paso/cuentame-como-paso-t16-ultimo-minuto-nuestra-vida-capitulo-276/2969138/?media=tve',
66 'only_matching': True,
4c718d3c
JMF
67 }, {
68 'url': 'http://www.rtve.es/filmoteca/no-do/not-1-introduccion-primer-noticiario-espanol/1465256/',
69 'only_matching': True,
2b9faf55 70 }]
91a6adde 71
e9f65f87
JMF
72 def _real_initialize(self):
73 user_agent_b64 = base64.b64encode(std_headers['User-Agent'].encode('utf-8')).decode('utf-8')
10db0d2f 74 self._manager = self._download_json(
e9f65f87 75 'http://www.rtve.es/odin/loki/' + user_agent_b64,
10db0d2f 76 None, 'Fetching manager info')['manager']
77
78 @staticmethod
79 def _decrypt_url(png):
80 encrypted_data = io.BytesIO(compat_b64decode(png)[8:])
81 while True:
82 length = compat_struct_unpack('!I', encrypted_data.read(4))[0]
83 chunk_type = encrypted_data.read(4)
84 if chunk_type == b'IEND':
85 break
86 data = encrypted_data.read(length)
87 if chunk_type == b'tEXt':
88 alphabet_data, text = data.split(b'\0')
89 quality, url_data = text.split(b'%%')
90 alphabet = []
91 e = 0
92 d = 0
93 for l in _bytes_to_chr(alphabet_data):
94 if d == 0:
95 alphabet.append(l)
96 d = e = (e + 1) % 4
97 else:
98 d -= 1
99 url = ''
100 f = 0
101 e = 3
102 b = 1
103 for letter in _bytes_to_chr(url_data):
104 if f == 0:
105 l = int(letter) * 10
106 f = 1
107 else:
108 if e == 0:
109 l += int(letter)
110 url += alphabet[l]
111 e = (b + 3) % 4
112 f = 0
113 b += 1
114 else:
115 e -= 1
116
117 yield quality.decode(), url
118 encrypted_data.read(4) # CRC
119
120 def _extract_png_formats(self, video_id):
121 png = self._download_webpage(
122 'http://www.rtve.es/ztnr/movil/thumbnail/%s/videos/%s.png' % (self._manager, video_id),
123 video_id, 'Downloading url information', query={'q': 'v2'})
124 q = qualities(['Media', 'Alta', 'HQ', 'HD_READY', 'HD_FULL'])
125 formats = []
126 for quality, video_url in self._decrypt_url(png):
127 ext = determine_ext(video_url)
128 if ext == 'm3u8':
129 formats.extend(self._extract_m3u8_formats(
130 video_url, video_id, 'mp4', 'm3u8_native',
131 m3u8_id='hls', fatal=False))
132 elif ext == 'mpd':
133 formats.extend(self._extract_mpd_formats(
134 video_url, video_id, 'dash', fatal=False))
135 else:
136 formats.append({
137 'format_id': quality,
138 'quality': q(quality),
139 'url': video_url,
140 })
141 self._sort_formats(formats)
142 return formats
e9f65f87 143
91a6adde 144 def _real_extract(self, url):
10db0d2f 145 video_id = self._match_id(url)
91a6adde
JMF
146 info = self._download_json(
147 'http://www.rtve.es/api/videos/%s/config/alacarta_videos.json' % video_id,
148 video_id)['page']['items'][0]
ce73839f
JMF
149 if info['state'] == 'DESPU':
150 raise ExtractorError('The video is no longer available', expected=True)
10db0d2f 151 title = info['title'].strip()
152 formats = self._extract_png_formats(video_id)
2c53bd51 153
25ac63ed 154 subtitles = None
10db0d2f 155 sbt_file = info.get('sbtFile')
156 if sbt_file:
157 subtitles = self.extract_subtitles(video_id, sbt_file)
158
159 is_live = info.get('live') is True
25ac63ed 160
91a6adde
JMF
161 return {
162 'id': video_id,
10db0d2f 163 'title': self._live_title(title) if is_live else title,
2c53bd51 164 'formats': formats,
2b9faf55 165 'thumbnail': info.get('image'),
25ac63ed 166 'subtitles': subtitles,
10db0d2f 167 'duration': float_or_none(info.get('duration'), 1000),
168 'is_live': is_live,
169 'series': info.get('programTitle'),
2b9faf55
PH
170 }
171
25ac63ed
JMF
172 def _get_subtitles(self, video_id, sub_file):
173 subs = self._download_json(
174 sub_file + '.json', video_id,
175 'Downloading subtitles info')['page']['items']
9c665ab7
PH
176 return dict(
177 (s['lang'], [{'ext': 'vtt', 'url': s['src']}])
25ac63ed
JMF
178 for s in subs)
179
b68eedba 180
10db0d2f 181class RTVEInfantilIE(RTVEALaCartaIE):
b68eedba
JMF
182 IE_NAME = 'rtve.es:infantil'
183 IE_DESC = 'RTVE infantil'
10db0d2f 184 _VALID_URL = r'https?://(?:www\.)?rtve\.es/infantil/serie/[^/]+/video/[^/]+/(?P<id>[0-9]+)/'
d5b55939
EF
185
186 _TESTS = [{
187 'url': 'http://www.rtve.es/infantil/serie/cleo/video/maneras-vivir/3040283/',
10db0d2f 188 'md5': '5747454717aedf9f9fdf212d1bcfc48d',
d5b55939
EF
189 'info_dict': {
190 'id': '3040283',
191 'ext': 'mp4',
192 'title': 'Maneras de vivir',
10db0d2f 193 'thumbnail': r're:https?://.+/1426182947956\.JPG',
d5b55939
EF
194 'duration': 357.958,
195 },
10db0d2f 196 'expected_warnings': ['Failed to download MPD manifest', 'Failed to download m3u8 information'],
b68eedba 197 }]
d5b55939 198
d5b55939 199
10db0d2f 200class RTVELiveIE(RTVEALaCartaIE):
2b9faf55
PH
201 IE_NAME = 'rtve.es:live'
202 IE_DESC = 'RTVE.es live streams'
92519402 203 _VALID_URL = r'https?://(?:www\.)?rtve\.es/directo/(?P<id>[a-zA-Z0-9-]+)'
2b9faf55
PH
204
205 _TESTS = [{
3e769682 206 'url': 'http://www.rtve.es/directo/la-1/',
2b9faf55 207 'info_dict': {
3e769682
JMF
208 'id': 'la-1',
209 'ext': 'mp4',
10db0d2f 210 'title': 're:^La 1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
2b9faf55
PH
211 },
212 'params': {
213 'skip_download': 'live stream',
214 }
215 }]
216
217 def _real_extract(self, url):
5ad28e7f 218 mobj = self._match_valid_url(url)
2b9faf55
PH
219 video_id = mobj.group('id')
220
221 webpage = self._download_webpage(url, video_id)
3e769682
JMF
222 title = remove_end(self._og_search_title(webpage), ' en directo en RTVE.es')
223 title = remove_start(title, 'Estoy viendo ')
2b9faf55
PH
224
225 vidplayer_id = self._search_regex(
b63005f5
S
226 (r'playerId=player([0-9]+)',
227 r'class=["\'].*?\blive_mod\b.*?["\'][^>]+data-assetid=["\'](\d+)',
228 r'data-id=["\'](\d+)'),
229 webpage, 'internal video ID')
2b9faf55 230
2b9faf55
PH
231 return {
232 'id': video_id,
10db0d2f 233 'title': self._live_title(title),
234 'formats': self._extract_png_formats(vidplayer_id),
3e769682 235 'is_live': True,
91a6adde 236 }
233b58de
JMF
237
238
239class RTVETelevisionIE(InfoExtractor):
240 IE_NAME = 'rtve.es:television'
92519402 241 _VALID_URL = r'https?://(?:www\.)?rtve\.es/television/[^/]+/[^/]+/(?P<id>\d+).shtml'
233b58de
JMF
242
243 _TEST = {
244 'url': 'http://www.rtve.es/television/20160628/revolucion-del-movil/1364141.shtml',
245 'info_dict': {
246 'id': '3069778',
247 'ext': 'mp4',
248 'title': 'Documentos TV - La revolución del móvil',
249 'duration': 3496.948,
250 },
251 'params': {
252 'skip_download': True,
253 },
254 }
255
256 def _real_extract(self, url):
257 page_id = self._match_id(url)
258 webpage = self._download_webpage(url, page_id)
259
260 alacarta_url = self._search_regex(
261 r'data-location="alacarta_videos"[^<]+url&quot;:&quot;(http://www\.rtve\.es/alacarta.+?)&',
262 webpage, 'alacarta url', default=None)
263 if alacarta_url is None:
264 raise ExtractorError(
265 'The webpage doesn\'t contain any video', expected=True)
266
267 return self.url_result(alacarta_url, ie=RTVEALaCartaIE.ie_key())