[ie/soundcloud] Various fixes (#11820)
[yt-dlp.git] / yt_dlp / extractor / mediaset.py
blob8cb18e6096f3af2c95e0d6a16c6fc35c82fa694d
1 import functools
2 import re
4 from .theplatform import ThePlatformBaseIE
5 from ..utils import (
6 ExtractorError,
7 GeoRestrictedError,
8 OnDemandPagedList,
9 int_or_none,
10 try_get,
11 update_url_query,
12 urljoin,
16 class MediasetIE(ThePlatformBaseIE):
17 _TP_TLD = 'eu'
18 _GUID_RE = r'F[0-9A-Z]{15}'
19 _VALID_URL = rf'''(?x)
20 (?:
21 mediaset:|
22 https?://
23 (?:\w+\.)+mediaset\.it/
24 (?:
25 (?:video|on-demand|movie)/(?:[^/]+/)+[^/]+_|
26 player/(?:v\d+/)?index\.html\?\S*?\bprogramGuid=
28 )(?P<id>{_GUID_RE})
29 '''
31 _EMBED_REGEX = [
32 rf'<iframe[^>]+src=[\'"](?P<url>(?:https?:)?//(?:\w+\.)+mediaset\.it/player/(?:v\d+/)?index\.html\?\S*?programGuid={_GUID_RE})[\'"&]',
34 _TESTS = [{
35 # full episode
36 'url': 'https://mediasetinfinity.mediaset.it/video/mrwronglezionidamore/episodio-1_F310575103000102',
37 'md5': 'a7e75c6384871f322adb781d3bd72c26',
38 'info_dict': {
39 'id': 'F310575103000102',
40 'ext': 'mp4',
41 'title': 'Episodio 1',
42 'description': 'md5:e8017b7d7194e9bfb75299c2b8d81e02',
43 'thumbnail': r're:^https?://.*\.jpg$',
44 'duration': 2682.0,
45 'upload_date': '20210530',
46 'series': 'Mr Wrong - Lezioni d\'amore',
47 'timestamp': 1622413946,
48 'uploader': 'Canale 5',
49 'uploader_id': 'C5',
50 'season': 'Season 1',
51 'episode': 'Episode 1',
52 'season_number': 1,
53 'episode_number': 1,
54 'chapters': [{'start_time': 0.0, 'end_time': 439.88}, {'start_time': 439.88, 'end_time': 1685.84}, {'start_time': 1685.84, 'end_time': 2682.0}],
56 }, {
57 'url': 'https://mediasetinfinity.mediaset.it/video/matrix/puntata-del-25-maggio_F309013801000501',
58 'md5': '1276f966ac423d16ba255ce867de073e',
59 'info_dict': {
60 'id': 'F309013801000501',
61 'ext': 'mp4',
62 'title': 'Puntata del 25 maggio',
63 'description': 'md5:ee2e456e3eb1dba5e814596655bb5296',
64 'thumbnail': r're:^https?://.*\.jpg$',
65 'duration': 6565.008,
66 'upload_date': '20200903',
67 'series': 'Matrix',
68 'timestamp': 1599172492,
69 'uploader': 'Canale 5',
70 'uploader_id': 'C5',
71 'season': 'Season 5',
72 'episode': 'Episode 5',
73 'season_number': 5,
74 'episode_number': 5,
75 'chapters': [{'start_time': 0.0, 'end_time': 3409.08}, {'start_time': 3409.08, 'end_time': 6565.008}],
76 'categories': ['Informazione'],
78 }, {
79 # DRM
80 'url': 'https://mediasetinfinity.mediaset.it/movie/selvaggi/selvaggi_F006474501000101',
81 'info_dict': {
82 'id': 'F006474501000101',
83 'ext': 'mp4',
84 'title': 'Selvaggi',
85 'description': 'md5:cfdedbbfdd12d4d0e5dcf1fa1b75284f',
86 'thumbnail': r're:^https?://.*\.jpg$',
87 'duration': 5233.01,
88 'upload_date': '20210729',
89 'timestamp': 1627594716,
90 'uploader': 'Cine34',
91 'uploader_id': 'B6',
92 'chapters': [{'start_time': 0.0, 'end_time': 1938.56}, {'start_time': 1938.56, 'end_time': 5233.01}],
94 'params': {
95 'ignore_no_formats_error': True,
97 'expected_warnings': [
98 'None of the available releases match the specified AssetType, ProtectionScheme, and/or Format preferences',
99 'Content behind paywall and DRM',
101 'skip': True,
102 }, {
103 # old domain
104 'url': 'https://www.mediasetplay.mediaset.it/video/mrwronglezionidamore/episodio-1_F310575103000102',
105 'only_matching': True,
106 }, {
107 # iframe
108 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
109 'only_matching': True,
110 }, {
111 'url': 'mediaset:FAFU000000665924',
112 'only_matching': True,
114 _WEBPAGE_TESTS = [{
115 # Mediaset embed
116 'url': 'http://www.tgcom24.mediaset.it/politica/serracchiani-voglio-vivere-in-una-societa-aperta-reazioni-sproporzionate-_3071354-201702a.shtml',
117 'info_dict': {
118 'id': 'FD00000000004929',
119 'ext': 'mp4',
120 'title': 'Serracchiani: "Voglio vivere in una società aperta, con tutela del patto di fiducia"',
121 'duration': 67.013,
122 'thumbnail': r're:^https?://.*\.jpg$',
123 'uploader': 'Mediaset Play',
124 'uploader_id': 'QY',
125 'upload_date': '20201005',
126 'timestamp': 1601866168,
127 'chapters': [],
129 'params': {
130 'skip_download': True,
132 'skip': 'Dead link',
133 }, {
134 # WittyTV embed
135 'url': 'https://www.wittytv.it/mauriziocostanzoshow/ultima-puntata-venerdi-25-novembre/',
136 'info_dict': {
137 'id': 'F312172801000801',
138 'ext': 'mp4',
139 'title': 'Ultima puntata - Venerdì 25 novembre',
140 'description': 'Una serata all\'insegna della musica e del buonumore ma non priva di spunti di riflessione',
141 'duration': 6203.01,
142 'thumbnail': r're:^https?://.*\.jpg$',
143 'uploader': 'Canale 5',
144 'uploader_id': 'C5',
145 'upload_date': '20221126',
146 'timestamp': 1669428689,
147 'chapters': list,
148 'series': 'Maurizio Costanzo Show',
149 'season': 'Season 12',
150 'season_number': 12,
151 'episode': 'Episode 8',
152 'episode_number': 8,
153 'categories': ['Intrattenimento'],
155 'params': {
156 'skip_download': True,
160 def _parse_smil_formats_and_subtitles(
161 self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
162 for video in smil.findall(self._xpath_ns('.//video', namespace)):
163 video.attrib['src'] = re.sub(r'(https?://vod05)t(-mediaset-it\.akamaized\.net/.+?.mpd)\?.+', r'\1\2', video.attrib['src'])
164 return super()._parse_smil_formats_and_subtitles(
165 smil, smil_url, video_id, namespace, f4m_params, transform_rtmp_url)
167 def _check_drm_formats(self, tp_formats, video_id):
168 has_nondrm, drm_manifest = False, ''
169 for f in tp_formats:
170 if '_sampleaes/' in (f.get('manifest_url') or ''):
171 drm_manifest = drm_manifest or f['manifest_url']
172 f['has_drm'] = True
173 if not f.get('has_drm') and f.get('manifest_url'):
174 has_nondrm = True
176 nodrm_manifest = re.sub(r'_sampleaes/(\w+)_fp_', r'/\1_no_', drm_manifest)
177 if has_nondrm or nodrm_manifest == drm_manifest:
178 return
180 tp_formats.extend(self._extract_m3u8_formats(
181 nodrm_manifest, video_id, m3u8_id='hls', fatal=False) or [])
183 def _real_extract(self, url):
184 guid = self._match_id(url)
185 tp_path = f'PR1GhC/media/guid/2702976343/{guid}'
186 info = self._extract_theplatform_metadata(tp_path, guid)
188 formats = []
189 subtitles = {}
190 first_e = geo_e = None
191 asset_type = 'geoNo:HD,browser,geoIT|geoNo:HD,geoIT|geoNo:SD,browser,geoIT|geoNo:SD,geoIT|geoNo|HD|SD'
192 # TODO: fixup ISM+none manifest URLs
193 for f in ('MPEG4', 'MPEG-DASH', 'M3U'):
194 try:
195 tp_formats, tp_subtitles = self._extract_theplatform_smil(
196 update_url_query(f'http://link.theplatform.{self._TP_TLD}/s/{tp_path}', {
197 'mbr': 'true',
198 'formats': f,
199 'assetTypes': asset_type,
200 }), guid, f'Downloading {f.split("+")[0]} SMIL data')
201 except ExtractorError as e:
202 if e.orig_msg == 'None of the available releases match the specified AssetType, ProtectionScheme, and/or Format preferences':
203 e.orig_msg = 'This video is DRM protected'
204 if not geo_e and isinstance(e, GeoRestrictedError):
205 geo_e = e
206 if not first_e:
207 first_e = e
208 continue
209 self._check_drm_formats(tp_formats, guid)
210 formats.extend(tp_formats)
211 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
213 # check for errors and report them
214 if (first_e or geo_e) and not formats:
215 raise geo_e or first_e
217 feed_data = self._download_json(
218 f'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs-v2/guid/-/{guid}',
219 guid, fatal=False)
220 if feed_data:
221 publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
222 thumbnails = feed_data.get('thumbnails') or {}
223 thumbnail = None
224 for key, value in thumbnails.items():
225 if key.startswith('image_keyframe_poster-'):
226 thumbnail = value.get('url')
227 break
229 info.update({
230 'description': info.get('description') or feed_data.get('description') or feed_data.get('longDescription'),
231 'uploader': publish_info.get('description'),
232 'uploader_id': publish_info.get('channel'),
233 'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
234 'thumbnail': thumbnail,
237 if feed_data.get('programType') == 'episode':
238 info.update({
239 'episode_number': int_or_none(
240 feed_data.get('tvSeasonEpisodeNumber')),
241 'season_number': int_or_none(
242 feed_data.get('tvSeasonNumber')),
243 'series': feed_data.get('mediasetprogram$brandTitle'),
246 info.update({
247 'id': guid,
248 'formats': formats,
249 'subtitles': subtitles,
251 return info
254 class MediasetShowIE(MediasetIE): # XXX: Do not subclass from concrete IE
255 _VALID_URL = r'''(?x)
257 https?://
258 (\w+\.)+mediaset\.it/
260 (?:fiction|programmi-tv|serie-tv|kids)/(?:.+?/)?
261 (?:[a-z-]+)_SE(?P<id>\d{12})
262 (?:,ST(?P<st>\d{12}))?
263 (?:,sb(?P<sb>\d{9}))?$
267 _TESTS = [{
268 # TV Show webpage (general webpage)
269 'url': 'https://mediasetinfinity.mediaset.it/programmi-tv/leiene/leiene_SE000000000061',
270 'info_dict': {
271 'id': '000000000061',
272 'title': 'Le Iene 2022/2023',
274 'playlist_mincount': 6,
275 }, {
276 # TV Show webpage (specific season)
277 'url': 'https://mediasetinfinity.mediaset.it/programmi-tv/leiene/leiene_SE000000000061,ST000000002763',
278 'info_dict': {
279 'id': '000000002763',
280 'title': 'Le Iene 2021/2022',
282 'playlist_mincount': 7,
283 }, {
284 # TV Show specific playlist (with multiple pages)
285 'url': 'https://mediasetinfinity.mediaset.it/programmi-tv/leiene/iservizi_SE000000000061,ST000000002763,sb100013375',
286 'info_dict': {
287 'id': '100013375',
288 'title': 'I servizi',
290 'playlist_mincount': 50,
293 _BY_SUBBRAND = 'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs-v2?byCustomValue={subBrandId}{%s}&sort=:publishInfo_lastPublished|desc,tvSeasonEpisodeNumber|desc&range=%d-%d'
294 _PAGE_SIZE = 25
296 def _fetch_page(self, sb, page):
297 lower_limit = page * self._PAGE_SIZE + 1
298 upper_limit = lower_limit + self._PAGE_SIZE - 1
299 content = self._download_json(
300 self._BY_SUBBRAND % (sb, lower_limit, upper_limit), sb)
301 for entry in content.get('entries') or []:
302 yield self.url_result(
303 'mediaset:' + entry['guid'],
304 playlist_title=entry['mediasetprogram$subBrandDescription'])
306 def _real_extract(self, url):
307 playlist_id, st, sb = self._match_valid_url(url).group('id', 'st', 'sb')
308 if not sb:
309 page = self._download_webpage(url, st or playlist_id)
310 entries = [self.url_result(urljoin('https://mediasetinfinity.mediaset.it', url))
311 for url in re.findall(r'href="([^<>=]+SE\d{12},ST\d{12},sb\d{9})">[^<]+<', page)]
312 title = self._html_extract_title(page).split('|')[0].strip()
313 return self.playlist_result(entries, st or playlist_id, title)
315 entries = OnDemandPagedList(
316 functools.partial(self._fetch_page, sb),
317 self._PAGE_SIZE)
318 title = try_get(entries, lambda x: x[0]['playlist_title'])
320 return self.playlist_result(entries, sb, title)