]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/rai.py
[raiplay:playlist] Add extractor
[yt-dlp.git] / youtube_dl / extractor / rai.py
CommitLineData
b0adbe98
S
1from __future__ import unicode_literals
2
b8d8cced
S
3import re
4
afbdd3ac 5from .common import InfoExtractor
b8d8cced
S
6from ..compat import (
7 compat_urlparse,
8 compat_str,
9)
b0adbe98 10from ..utils import (
f1388739 11 ExtractorError,
51342717 12 determine_ext,
f1388739
YCH
13 find_xpath_attr,
14 fix_xml_ampersands,
b8d8cced 15 GeoRestrictedError,
f1388739 16 int_or_none,
b0adbe98 17 parse_duration,
b8d8cced
S
18 strip_or_none,
19 try_get,
b0adbe98 20 unified_strdate,
b8d8cced 21 unified_timestamp,
f1388739 22 update_url_query,
b8d8cced 23 urljoin,
06d5556d 24 xpath_text,
b0adbe98
S
25)
26
27
034a8849 28class RaiBaseIE(InfoExtractor):
b8d8cced
S
29 _UUID_RE = r'[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}'
30 _GEO_COUNTRIES = ['IT']
31 _GEO_BYPASS = False
32
33 def _extract_relinker_info(self, relinker_url, video_id):
034a8849 34 formats = []
b8d8cced
S
35 geoprotection = None
36 is_live = None
37 duration = None
034a8849
YCH
38
39 for platform in ('mon', 'flash', 'native'):
034a8849
YCH
40 relinker = self._download_xml(
41 relinker_url, video_id,
42 note='Downloading XML metadata for platform %s' % platform,
43 transform_source=fix_xml_ampersands,
38cce791
YCH
44 query={'output': 45, 'pl': platform},
45 headers=self.geo_verification_headers())
034a8849 46
b8d8cced
S
47 if not geoprotection:
48 geoprotection = xpath_text(
49 relinker, './geoprotection', default=None) == 'Y'
50
51 if not is_live:
52 is_live = xpath_text(
53 relinker, './is_live', default=None) == 'Y'
54 if not duration:
55 duration = parse_duration(xpath_text(
56 relinker, './duration', default=None))
57
58 url_elem = find_xpath_attr(relinker, './url', 'type', 'content')
59 if url_elem is None:
60 continue
61
62 media_url = url_elem.text
63
64 # This does not imply geo restriction (e.g.
65 # http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html)
034a8849 66 if media_url == 'http://download.rai.it/video_no_available.mp4':
b8d8cced 67 continue
034a8849
YCH
68
69 ext = determine_ext(media_url)
70 if (ext == 'm3u8' and platform != 'mon') or (ext == 'f4m' and platform != 'flash'):
71 continue
72
73 if ext == 'm3u8':
74 formats.extend(self._extract_m3u8_formats(
75 media_url, video_id, 'mp4', 'm3u8_native',
76 m3u8_id='hls', fatal=False))
77 elif ext == 'f4m':
78 manifest_url = update_url_query(
79 media_url.replace('manifest#live_hds.f4m', 'manifest.f4m'),
80 {'hdcore': '3.7.0', 'plugin': 'aasp-3.7.0.39.44'})
81 formats.extend(self._extract_f4m_formats(
82 manifest_url, video_id, f4m_id='hds', fatal=False))
83 else:
84 bitrate = int_or_none(xpath_text(relinker, 'bitrate'))
85 formats.append({
86 'url': media_url,
87 'tbr': bitrate if bitrate > 0 else None,
88 'format_id': 'http-%d' % bitrate if bitrate > 0 else 'http',
89 })
90
b8d8cced
S
91 if not formats and geoprotection is True:
92 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
93
94 return dict((k, v) for k, v in {
95 'is_live': is_live,
96 'duration': duration,
97 'formats': formats,
98 }.items() if v is not None)
034a8849 99
1b3feca0
S
100 @staticmethod
101 def _extract_subtitles(url, subtitle_url):
102 subtitles = {}
103 if subtitle_url and isinstance(subtitle_url, compat_str):
104 subtitle_url = urljoin(url, subtitle_url)
105 STL_EXT = '.stl'
106 SRT_EXT = '.srt'
107 subtitles['it'] = [{
108 'ext': 'stl',
109 'url': subtitle_url,
110 }]
111 if subtitle_url.endswith(STL_EXT):
112 srt_url = subtitle_url[:-len(STL_EXT)] + SRT_EXT
113 subtitles['it'].append({
114 'ext': 'srt',
115 'url': srt_url,
116 })
117 return subtitles
118
2b28b892 119
51342717 120class RaiPlayIE(RaiBaseIE):
b8d8cced 121 _VALID_URL = r'(?P<url>https?://(?:www\.)?raiplay\.it/.+?-(?P<id>%s)\.html)' % RaiBaseIE._UUID_RE
51342717
T
122 _TESTS = [{
123 'url': 'http://www.raiplay.it/video/2016/10/La-Casa-Bianca-e06118bb-59a9-4636-b914-498e4cfd2c66.html?source=twitter',
124 'md5': '340aa3b7afb54bfd14a8c11786450d76',
125 'info_dict': {
126 'id': 'e06118bb-59a9-4636-b914-498e4cfd2c66',
127 'ext': 'mp4',
128 'title': 'La Casa Bianca',
b8d8cced
S
129 'alt_title': 'S2016 - Puntata del 23/10/2016',
130 'description': 'md5:a09d45890850458077d1f68bb036e0a5',
51342717 131 'thumbnail': r're:^https?://.*\.jpg$',
b8d8cced
S
132 'uploader': 'Rai 3',
133 'creator': 'Rai 3',
134 'duration': 3278,
135 'timestamp': 1477764300,
136 'upload_date': '20161029',
137 'series': 'La Casa Bianca',
138 'season': '2016',
139 },
51342717
T
140 }, {
141 'url': 'http://www.raiplay.it/video/2014/04/Report-del-07042014-cb27157f-9dd0-4aee-b788-b1f67643a391.html',
142 'md5': '8970abf8caf8aef4696e7b1f2adfc696',
143 'info_dict': {
144 'id': 'cb27157f-9dd0-4aee-b788-b1f67643a391',
145 'ext': 'mp4',
b8d8cced
S
146 'title': 'Report del 07/04/2014',
147 'alt_title': 'S2013/14 - Puntata del 07/04/2014',
148 'description': 'md5:f27c544694cacb46a078db84ec35d2d9',
51342717 149 'thumbnail': r're:^https?://.*\.jpg$',
b8d8cced
S
150 'uploader': 'Rai 5',
151 'creator': 'Rai 5',
152 'duration': 6160,
153 'series': 'Report',
154 'season_number': 5,
155 'season': '2013/14',
156 },
157 'params': {
158 'skip_download': True,
159 },
160 }, {
161 'url': 'http://www.raiplay.it/video/2016/11/gazebotraindesi-efebe701-969c-4593-92f3-285f0d1ce750.html?',
162 'only_matching': True,
51342717 163 }]
2b28b892 164
51342717 165 def _real_extract(self, url):
b8d8cced
S
166 mobj = re.match(self._VALID_URL, url)
167 url, video_id = mobj.group('url', 'id')
2b28b892 168
b8d8cced
S
169 media = self._download_json(
170 '%s?json' % url, video_id, 'Downloading video JSON')
2b28b892 171
b8d8cced
S
172 title = media['name']
173
174 video = media['video']
175
176 relinker_info = self._extract_relinker_info(video['contentUrl'], video_id)
177 self._sort_formats(relinker_info['formats'])
2b28b892 178
51342717
T
179 thumbnails = []
180 if 'images' in media:
181 for _, value in media.get('images').items():
182 if value:
183 thumbnails.append({
b8d8cced 184 'url': value.replace('[RESOLUTION]', '600x400')
51342717 185 })
034a8849 186
b8d8cced
S
187 timestamp = unified_timestamp(try_get(
188 media, lambda x: x['availabilities'][0]['start'], compat_str))
b0adbe98 189
1b3feca0
S
190 subtitles = self._extract_subtitles(url, video.get('subtitles'))
191
b8d8cced 192 info = {
51342717 193 'id': video_id,
9c48b5a1
S
194 'title': self._live_title(title) if relinker_info.get(
195 'is_live') else title,
b8d8cced
S
196 'alt_title': media.get('subtitle'),
197 'description': media.get('description'),
9c48b5a1
S
198 'uploader': strip_or_none(media.get('channel')),
199 'creator': strip_or_none(media.get('editor')),
b8d8cced
S
200 'duration': parse_duration(video.get('duration')),
201 'timestamp': timestamp,
51342717 202 'thumbnails': thumbnails,
b8d8cced
S
203 'series': try_get(
204 media, lambda x: x['isPartOf']['name'], compat_str),
205 'season_number': int_or_none(try_get(
206 media, lambda x: x['isPartOf']['numeroStagioni'])),
207 'season': media.get('stagione') or None,
1b3feca0 208 'subtitles': subtitles,
51342717 209 }
b0adbe98 210
b8d8cced 211 info.update(relinker_info)
b8d8cced
S
212 return info
213
06d5556d 214
449c6657 215class RaiPlayLiveIE(RaiBaseIE):
9c48b5a1 216 _VALID_URL = r'https?://(?:www\.)?raiplay\.it/dirette/(?P<id>[^/?#&]+)'
449c6657 217 _TEST = {
9c48b5a1
S
218 'url': 'http://www.raiplay.it/dirette/rainews24',
219 'info_dict': {
220 'id': 'd784ad40-e0ae-4a69-aa76-37519d238a9c',
221 'display_id': 'rainews24',
222 'ext': 'mp4',
223 'title': 're:^Diretta di Rai News 24 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
224 'description': 'md5:6eca31500550f9376819f174e5644754',
225 'uploader': 'Rai News 24',
226 'creator': 'Rai News 24',
227 'is_live': True,
228 },
229 'params': {
230 'skip_download': True,
231 },
449c6657 232 }
233
234 def _real_extract(self, url):
9c48b5a1
S
235 display_id = self._match_id(url)
236
237 webpage = self._download_webpage(url, display_id)
449c6657 238
9c48b5a1
S
239 video_id = self._search_regex(
240 r'data-uniquename=["\']ContentItem-(%s)' % RaiBaseIE._UUID_RE,
241 webpage, 'content id')
449c6657 242
9c48b5a1
S
243 return {
244 '_type': 'url_transparent',
245 'ie_key': RaiPlayIE.ie_key(),
246 'url': 'http://www.raiplay.it/dirette/ContentItem-%s.html' % video_id,
247 'id': video_id,
248 'display_id': display_id,
249 }
449c6657 250
251
034a8849 252class RaiIE(RaiBaseIE):
b8d8cced 253 _VALID_URL = r'https?://[^/]+\.(?:rai\.(?:it|tv)|rainews\.it)/dl/.+?-(?P<id>%s)(?:-.+?)?\.html' % RaiBaseIE._UUID_RE
51342717 254 _TESTS = [{
b8d8cced
S
255 # var uniquename = "ContentItem-..."
256 # data-id="ContentItem-..."
51342717
T
257 'url': 'http://www.raisport.rai.it/dl/raiSport/media/rassegna-stampa-04a9f4bd-b563-40cf-82a6-aad3529cb4a9.html',
258 'info_dict': {
259 'id': '04a9f4bd-b563-40cf-82a6-aad3529cb4a9',
260 'ext': 'mp4',
261 'title': 'TG PRIMO TEMPO',
b8d8cced 262 'thumbnail': r're:^https?://.*\.jpg$',
51342717 263 'duration': 1758,
b8d8cced 264 'upload_date': '20140612',
51342717
T
265 }
266 }, {
b8d8cced 267 # with ContentItem in many metas
51342717
T
268 'url': 'http://www.rainews.it/dl/rainews/media/Weekend-al-cinema-da-Hollywood-arriva-il-thriller-di-Tate-Taylor-La-ragazza-del-treno-1632c009-c843-4836-bb65-80c33084a64b.html',
269 'info_dict': {
270 'id': '1632c009-c843-4836-bb65-80c33084a64b',
271 'ext': 'mp4',
b8d8cced
S
272 'title': 'Weekend al cinema, da Hollywood arriva il thriller di Tate Taylor "La ragazza del treno"',
273 'description': 'I film in uscita questa settimana.',
51342717 274 'thumbnail': r're:^https?://.*\.png$',
b8d8cced
S
275 'duration': 833,
276 'upload_date': '20161103',
51342717
T
277 }
278 }, {
b8d8cced 279 # with ContentItem in og:url
51342717
T
280 'url': 'http://www.rai.it/dl/RaiTV/programmi/media/ContentItem-efb17665-691c-45d5-a60c-5301333cbb0c.html',
281 'md5': '11959b4e44fa74de47011b5799490adf',
282 'info_dict': {
283 'id': 'efb17665-691c-45d5-a60c-5301333cbb0c',
284 'ext': 'mp4',
285 'title': 'TG1 ore 20:00 del 03/11/2016',
b8d8cced 286 'description': 'TG1 edizione integrale ore 20:00 del giorno 03/11/2016',
51342717 287 'thumbnail': r're:^https?://.*\.jpg$',
b8d8cced 288 'duration': 2214,
51342717 289 'upload_date': '20161103',
51342717
T
290 }
291 }, {
b8d8cced 292 # drawMediaRaiTV(...)
51342717
T
293 'url': 'http://www.report.rai.it/dl/Report/puntata/ContentItem-0c7a664b-d0f4-4b2c-8835-3f82e46f433e.html',
294 'md5': '2dd727e61114e1ee9c47f0da6914e178',
295 'info_dict': {
296 'id': '59d69d28-6bb6-409d-a4b5-ed44096560af',
297 'ext': 'mp4',
298 'title': 'Il pacco',
299 'description': 'md5:4b1afae1364115ce5d78ed83cd2e5b3a',
b8d8cced 300 'thumbnail': r're:^https?://.*\.jpg$',
51342717 301 'upload_date': '20141221',
034a8849 302 },
51342717 303 }, {
b8d8cced
S
304 # initEdizione('ContentItem-...'
305 'url': 'http://www.tg1.rai.it/dl/tg1/2010/edizioni/ContentSet-9b6e0cba-4bef-4aef-8cf0-9f7f665b7dfb-tg1.html?item=undefined',
306 'info_dict': {
307 'id': 'c2187016-8484-4e3a-8ac8-35e475b07303',
308 'ext': 'mp4',
309 'title': r're:TG1 ore \d{2}:\d{2} del \d{2}/\d{2}/\d{4}',
310 'duration': 2274,
311 'upload_date': '20170401',
312 },
313 'skip': 'Changes daily',
314 }, {
315 # HDS live stream with only relinker URL
51342717 316 'url': 'http://www.rai.tv/dl/RaiTV/dirette/PublishingBlock-1912dbbf-3f96-44c3-b4cf-523681fbacbc.html?channel=EuroNews',
51342717
T
317 'info_dict': {
318 'id': '1912dbbf-3f96-44c3-b4cf-523681fbacbc',
319 'ext': 'flv',
320 'title': 'EuroNews',
2b28b892 321 },
b8d8cced
S
322 'params': {
323 'skip_download': True,
2b28b892 324 },
51342717 325 }, {
b8d8cced 326 # HLS live stream with ContentItem in og:url
51342717 327 'url': 'http://www.rainews.it/dl/rainews/live/ContentItem-3156f2f2-dc70-4953-8e2f-70d7489d4ce9.html',
51342717
T
328 'info_dict': {
329 'id': '3156f2f2-dc70-4953-8e2f-70d7489d4ce9',
330 'ext': 'mp4',
331 'title': 'La diretta di Rainews24',
15e4b6b7 332 },
b8d8cced
S
333 'params': {
334 'skip_download': True,
335 },
51342717 336 }]
06d5556d 337
51342717
T
338 def _extract_from_content_id(self, content_id, url):
339 media = self._download_json(
340 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-%s.html?json' % content_id,
341 content_id, 'Downloading video JSON')
342
b8d8cced
S
343 title = media['name'].strip()
344
345 media_type = media['type']
346 if 'Audio' in media_type:
347 relinker_info = {
085d9dd9 348 'formats': [{
b8d8cced
S
349 'format_id': media.get('formatoAudio'),
350 'url': media['audioUrl'],
351 'ext': media.get('formatoAudio'),
085d9dd9 352 }]
b8d8cced
S
353 }
354 elif 'Video' in media_type:
355 relinker_info = self._extract_relinker_info(media['mediaUri'], content_id)
356 else:
357 raise ExtractorError('not a media file')
358
359 self._sort_formats(relinker_info['formats'])
360
51342717
T
361 thumbnails = []
362 for image_type in ('image', 'image_medium', 'image_300'):
363 thumbnail_url = media.get(image_type)
364 if thumbnail_url:
365 thumbnails.append({
366 'url': compat_urlparse.urljoin(url, thumbnail_url),
367 })
368
1b3feca0 369 subtitles = self._extract_subtitles(url, media.get('subtitlesUrl'))
51342717 370
b8d8cced 371 info = {
51342717 372 'id': content_id,
b8d8cced
S
373 'title': title,
374 'description': strip_or_none(media.get('desc')),
51342717
T
375 'thumbnails': thumbnails,
376 'uploader': media.get('author'),
377 'upload_date': unified_strdate(media.get('date')),
378 'duration': parse_duration(media.get('length')),
51342717
T
379 'subtitles': subtitles,
380 }
b8d8cced
S
381
382 info.update(relinker_info)
383
384 return info
385
386 def _real_extract(self, url):
387 video_id = self._match_id(url)
388
389 webpage = self._download_webpage(url, video_id)
390
391 content_item_id = None
392
393 content_item_url = self._html_search_meta(
394 ('og:url', 'og:video', 'og:video:secure_url', 'twitter:url',
395 'twitter:player', 'jsonlink'), webpage, default=None)
396 if content_item_url:
397 content_item_id = self._search_regex(
398 r'ContentItem-(%s)' % self._UUID_RE, content_item_url,
399 'content item id', default=None)
400
401 if not content_item_id:
402 content_item_id = self._search_regex(
403 r'''(?x)
404 (?:
405 (?:initEdizione|drawMediaRaiTV)\(|
406 <(?:[^>]+\bdata-id|var\s+uniquename)=
407 )
408 (["\'])
409 (?:(?!\1).)*\bContentItem-(?P<id>%s)
410 ''' % self._UUID_RE,
411 webpage, 'content item id', default=None, group='id')
412
413 content_item_ids = set()
361f293a
S
414 if content_item_id:
415 content_item_ids.add(content_item_id)
b8d8cced
S
416 if video_id not in content_item_ids:
417 content_item_ids.add(video_id)
418
419 for content_item_id in content_item_ids:
420 try:
421 return self._extract_from_content_id(content_item_id, url)
422 except GeoRestrictedError:
423 raise
424 except ExtractorError:
425 pass
426
427 relinker_url = self._search_regex(
428 r'''(?x)
429 (?:
430 var\s+videoURL|
431 mediaInfo\.mediaUri
432 )\s*=\s*
433 ([\'"])
434 (?P<url>
435 (?:https?:)?
436 //mediapolis(?:vod)?\.rai\.it/relinker/relinkerServlet\.htm\?
437 (?:(?!\1).)*\bcont=(?:(?!\1).)+)\1
438 ''',
439 webpage, 'relinker URL', group='url')
440
441 relinker_info = self._extract_relinker_info(
442 urljoin(url, relinker_url), video_id)
443 self._sort_formats(relinker_info['formats'])
444
445 title = self._search_regex(
446 r'var\s+videoTitolo\s*=\s*([\'"])(?P<title>[^\'"]+)\1',
447 webpage, 'title', group='title',
448 default=None) or self._og_search_title(webpage)
449
450 info = {
451 'id': video_id,
452 'title': title,
453 }
454
455 info.update(relinker_info)
456
457 return info
d21d0ba6
T
458
459
460class RaiPlaylistIE(InfoExtractor):
461 _VALID_URL = r'https?://(?:www\.)?raiplay\.it/programmi/(?P<id>[^/]+)'
462 _TESTS = [{
463 'url': 'http://www.raiplay.it/programmi/nondirloalmiocapo/',
464 'info_dict': {
465 'id': 'nondirloalmiocapo',
466 'title': 'Non dirlo al mio capo',
467 },
468 'playlist_mincount': 12,
469 }]
470
471 def _real_extract(self, url):
472 playlist_id = self._match_id(url)
473 webpage = self._download_webpage(url, playlist_id)
474 title = self._html_search_meta('programma', webpage, default=None)
475 video_urls = re.findall(' href="(/raiplay/video.+)"', webpage)
476 video_urls = [urljoin(url, video_url) for video_url in video_urls]
477 entries = [
478 self.url_result(
479 video_url,
480 RaiPlayIE.ie_key())
481 for video_url in video_urls if RaiPlayIE.suitable(video_url)
482 ]
483 return self.playlist_result(entries, playlist_id, title)