]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/mediaset.py
[iPrima] Fix extractor (#1541)
[yt-dlp.git] / yt_dlp / extractor / mediaset.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import functools
5 import re
6
7 from .theplatform import ThePlatformBaseIE
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 OnDemandPagedList,
12 parse_qs,
13 try_get,
14 urljoin,
15 update_url_query,
16 )
17
18
19 class MediasetIE(ThePlatformBaseIE):
20 _TP_TLD = 'eu'
21 _VALID_URL = r'''(?x)
22 (?:
23 mediaset:|
24 https?://
25 (?:(?:www|static3)\.)?mediasetplay\.mediaset\.it/
26 (?:
27 (?:video|on-demand|movie)/(?:[^/]+/)+[^/]+_|
28 player/index\.html\?.*?\bprogramGuid=
29 )
30 )(?P<id>[0-9A-Z]{16,})
31 '''
32 _TESTS = [{
33 # full episode
34 'url': 'https://www.mediasetplay.mediaset.it/video/mrwronglezionidamore/episodio-1_F310575103000102',
35 'md5': 'a7e75c6384871f322adb781d3bd72c26',
36 'info_dict': {
37 'id': 'F310575103000102',
38 'ext': 'mp4',
39 'title': 'Episodio 1',
40 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
41 'thumbnail': r're:^https?://.*\.jpg$',
42 'duration': 2682.0,
43 'upload_date': '20210530',
44 'series': 'Mr Wrong - Lezioni d\'amore',
45 'timestamp': 1622413946,
46 'uploader': 'Canale 5',
47 'uploader_id': 'C5',
48 },
49 }, {
50 'url': 'https://www.mediasetplay.mediaset.it/video/matrix/puntata-del-25-maggio_F309013801000501',
51 'md5': '1276f966ac423d16ba255ce867de073e',
52 'info_dict': {
53 'id': 'F309013801000501',
54 'ext': 'mp4',
55 'title': 'Puntata del 25 maggio',
56 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
57 'thumbnail': r're:^https?://.*\.jpg$',
58 'duration': 6565.008,
59 'upload_date': '20200903',
60 'series': 'Matrix',
61 'timestamp': 1599172492,
62 'uploader': 'Canale 5',
63 'uploader_id': 'C5',
64 },
65 }, {
66 'url': 'https://www.mediasetplay.mediaset.it/video/cameracafe5/episodio-69-pezzo-di-luna_F303843101017801',
67 'md5': 'd1650ac9ff944f185556126a736df148',
68 'info_dict': {
69 'id': 'F303843101017801',
70 'ext': 'mp4',
71 'title': 'Episodio 69 - Pezzo di luna',
72 'description': '',
73 'thumbnail': r're:^https?://.*\.jpg$',
74 'duration': 263.008,
75 'upload_date': '20200902',
76 'series': 'Camera Café 5',
77 'timestamp': 1599064700,
78 'uploader': 'Italia 1',
79 'uploader_id': 'I1',
80 },
81 }, {
82 'url': 'https://www.mediasetplay.mediaset.it/video/cameracafe5/episodio-51-tu-chi-sei_F303843107000601',
83 'md5': '567e9ad375b7a27a0e370650f572a1e3',
84 'info_dict': {
85 'id': 'F303843107000601',
86 'ext': 'mp4',
87 'title': 'Episodio 51 - Tu chi sei?',
88 'description': '',
89 'thumbnail': r're:^https?://.*\.jpg$',
90 'duration': 367.021,
91 'upload_date': '20200902',
92 'series': 'Camera Café 5',
93 'timestamp': 1599069817,
94 'uploader': 'Italia 1',
95 'uploader_id': 'I1',
96 },
97 }, {
98 # clip
99 'url': 'https://www.mediasetplay.mediaset.it/video/gogglebox/un-grande-classico-della-commedia-sexy_FAFU000000661680',
100 'only_matching': True,
101 }, {
102 # iframe simple
103 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
104 'only_matching': True,
105 }, {
106 # iframe twitter (from http://www.wittytv.it/se-prima-mi-fidavo-zero/)
107 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665104&id=665104',
108 'only_matching': True,
109 }, {
110 'url': 'mediaset:FAFU000000665924',
111 'only_matching': True,
112 }, {
113 'url': 'https://www.mediasetplay.mediaset.it/video/mediasethaacuoreilfuturo/palmieri-alicudi-lisola-dei-tre-bambini-felici--un-decreto-per-alicudi-e-tutte-le-microscuole_FD00000000102295',
114 'only_matching': True,
115 }, {
116 'url': 'https://www.mediasetplay.mediaset.it/video/cherryseason/anticipazioni-degli-episodi-del-23-ottobre_F306837101005C02',
117 'only_matching': True,
118 }, {
119 'url': 'https://www.mediasetplay.mediaset.it/video/tg5/ambiente-onda-umana-per-salvare-il-pianeta_F309453601079D01',
120 'only_matching': True,
121 }, {
122 'url': 'https://www.mediasetplay.mediaset.it/video/grandefratellovip/benedetta-una-doccia-gelata_F309344401044C135',
123 'only_matching': True,
124 }, {
125 'url': 'https://www.mediasetplay.mediaset.it/movie/herculeslaleggendahainizio/hercules-la-leggenda-ha-inizio_F305927501000102',
126 'only_matching': True,
127 }]
128
129 @staticmethod
130 def _extract_urls(ie, webpage):
131 def _qs(url):
132 return parse_qs(url)
133
134 def _program_guid(qs):
135 return qs.get('programGuid', [None])[0]
136
137 entries = []
138 for mobj in re.finditer(
139 r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml.*?)\1',
140 webpage):
141 embed_url = mobj.group('url')
142 embed_qs = _qs(embed_url)
143 program_guid = _program_guid(embed_qs)
144 if program_guid:
145 entries.append(embed_url)
146 continue
147 video_id = embed_qs.get('id', [None])[0]
148 if not video_id:
149 continue
150 urlh = ie._request_webpage(
151 embed_url, video_id, note='Following embed URL redirect')
152 embed_url = urlh.geturl()
153 program_guid = _program_guid(_qs(embed_url))
154 if program_guid:
155 entries.append(embed_url)
156 return entries
157
158 def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
159 for video in smil.findall(self._xpath_ns('.//video', namespace)):
160 video.attrib['src'] = re.sub(r'(https?://vod05)t(-mediaset-it\.akamaized\.net/.+?.mpd)\?.+', r'\1\2', video.attrib['src'])
161 return super(MediasetIE, self)._parse_smil_formats(smil, smil_url, video_id, namespace, f4m_params, transform_rtmp_url)
162
163 def _real_extract(self, url):
164 guid = self._match_id(url)
165 tp_path = 'PR1GhC/media/guid/2702976343/' + guid
166 info = self._extract_theplatform_metadata(tp_path, guid)
167
168 formats = []
169 subtitles = {}
170 first_e = None
171 asset_type = 'geoNo:HD,browser,geoIT|geoNo:HD,geoIT|geoNo:SD,browser,geoIT|geoNo:SD,geoIT|geoNo|HD|SD'
172 # TODO: fixup ISM+none manifest URLs
173 for f in ('MPEG4', 'MPEG-DASH+none', 'M3U+none'):
174 try:
175 tp_formats, tp_subtitles = self._extract_theplatform_smil(
176 update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
177 'mbr': 'true',
178 'formats': f,
179 'assetTypes': asset_type,
180 }), guid, 'Downloading %s SMIL data' % (f.split('+')[0]))
181 except ExtractorError as e:
182 if not first_e:
183 first_e = e
184 break
185 formats.extend(tp_formats)
186 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
187 if first_e and not formats:
188 raise first_e
189 self._sort_formats(formats)
190
191 feed_data = self._download_json(
192 'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs-v2/guid/-/' + guid,
193 guid, fatal=False)
194 if feed_data:
195 publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
196 thumbnails = feed_data.get('thumbnails') or {}
197 thumbnail = None
198 for key, value in thumbnails.items():
199 if key.startswith('image_keyframe_poster-'):
200 thumbnail = value.get('url')
201 break
202
203 info.update({
204 'episode_number': int_or_none(feed_data.get('tvSeasonEpisodeNumber')),
205 'season_number': int_or_none(feed_data.get('tvSeasonNumber')),
206 'series': feed_data.get('mediasetprogram$brandTitle'),
207 'uploader': publish_info.get('description'),
208 'uploader_id': publish_info.get('channel'),
209 'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
210 'thumbnail': thumbnail,
211 })
212
213 info.update({
214 'id': guid,
215 'formats': formats,
216 'subtitles': subtitles,
217 })
218 return info
219
220
221 class MediasetShowIE(MediasetIE):
222 _VALID_URL = r'''(?x)
223 (?:
224 https?://
225 (?:(?:www|static3)\.)?mediasetplay\.mediaset\.it/
226 (?:
227 (?:fiction|programmi-tv|serie-tv)/(?:.+?/)?
228 (?:[a-z]+)_SE(?P<id>\d{12})
229 (?:,ST(?P<st>\d{12}))?
230 (?:,sb(?P<sb>\d{9}))?$
231 )
232 )
233 '''
234 _TESTS = [{
235 # TV Show webpage (with a single playlist)
236 'url': 'https://www.mediasetplay.mediaset.it/serie-tv/fireforce/episodi_SE000000001556',
237 'info_dict': {
238 'id': '000000001556',
239 'title': 'Fire Force',
240 },
241 'playlist_count': 1,
242 }, {
243 # TV Show webpage (with multiple playlists)
244 'url': 'https://www.mediasetplay.mediaset.it/programmi-tv/leiene/leiene_SE000000000061,ST000000002763',
245 'info_dict': {
246 'id': '000000002763',
247 'title': 'Le Iene',
248 },
249 'playlist_count': 7,
250 }, {
251 # TV Show specific playlist (single page)
252 'url': 'https://www.mediasetplay.mediaset.it/serie-tv/fireforce/episodi_SE000000001556,ST000000002738,sb100013107',
253 'info_dict': {
254 'id': '100013107',
255 'title': 'Episodi',
256 },
257 'playlist_count': 4,
258 }, {
259 # TV Show specific playlist (with multiple pages)
260 'url': 'https://www.mediasetplay.mediaset.it/programmi-tv/leiene/iservizi_SE000000000061,ST000000002763,sb100013375',
261 'info_dict': {
262 'id': '100013375',
263 'title': 'I servizi',
264 },
265 'playlist_count': 53,
266 }]
267
268 _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'
269 _PAGE_SIZE = 25
270
271 def _fetch_page(self, sb, page):
272 lower_limit = page * self._PAGE_SIZE + 1
273 upper_limit = lower_limit + self._PAGE_SIZE - 1
274 content = self._download_json(
275 self._BY_SUBBRAND % (sb, lower_limit, upper_limit), sb)
276 for entry in content.get('entries') or []:
277 yield self.url_result(
278 'mediaset:' + entry['guid'],
279 playlist_title=entry['mediasetprogram$subBrandDescription'])
280
281 def _real_extract(self, url):
282 playlist_id, st, sb = self._match_valid_url(url).group('id', 'st', 'sb')
283 if not sb:
284 page = self._download_webpage(url, playlist_id)
285 entries = [self.url_result(urljoin('https://www.mediasetplay.mediaset.it', url))
286 for url in re.findall(r'href="([^<>=]+SE\d{12},ST\d{12},sb\d{9})">[^<]+<', page)]
287 title = (self._html_search_regex(r'(?s)<h1[^>]*>(.+?)</h1>', page, 'title', default=None)
288 or self._og_search_title(page))
289 return self.playlist_result(entries, st or playlist_id, title)
290
291 entries = OnDemandPagedList(
292 functools.partial(self._fetch_page, sb),
293 self._PAGE_SIZE)
294 title = try_get(entries, lambda x: x[0]['playlist_title'])
295
296 return self.playlist_result(entries, sb, title)