mediaset.py 13 KB

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