mediaset.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import functools
  2. import re
  3. from .theplatform import ThePlatformBaseIE
  4. from ..utils import (
  5. ExtractorError,
  6. GeoRestrictedError,
  7. int_or_none,
  8. OnDemandPagedList,
  9. parse_qs,
  10. try_get,
  11. urljoin,
  12. update_url_query,
  13. )
  14. class MediasetIE(ThePlatformBaseIE):
  15. _TP_TLD = 'eu'
  16. _VALID_URL = r'''(?x)
  17. (?:
  18. mediaset:|
  19. https?://
  20. (?:\w+\.)+mediaset\.it/
  21. (?:
  22. (?:video|on-demand|movie)/(?:[^/]+/)+[^/]+_|
  23. player/(?:v\d+/)?index\.html\?.*?\bprogramGuid=
  24. )
  25. )(?P<id>[0-9A-Z]{16,})
  26. '''
  27. _TESTS = [{
  28. # full episode
  29. 'url': 'https://www.mediasetplay.mediaset.it/video/mrwronglezionidamore/episodio-1_F310575103000102',
  30. 'md5': 'a7e75c6384871f322adb781d3bd72c26',
  31. 'info_dict': {
  32. 'id': 'F310575103000102',
  33. 'ext': 'mp4',
  34. 'title': 'Episodio 1',
  35. 'description': 'md5:e8017b7d7194e9bfb75299c2b8d81e02',
  36. 'thumbnail': r're:^https?://.*\.jpg$',
  37. 'duration': 2682.0,
  38. 'upload_date': '20210530',
  39. 'series': 'Mr Wrong - Lezioni d\'amore',
  40. 'timestamp': 1622413946,
  41. 'uploader': 'Canale 5',
  42. 'uploader_id': 'C5',
  43. 'season': 'Season 1',
  44. 'episode': 'Episode 1',
  45. 'season_number': 1,
  46. 'episode_number': 1,
  47. '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}],
  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:ee2e456e3eb1dba5e814596655bb5296',
  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. 'season': 'Season 5',
  65. 'episode': 'Episode 5',
  66. 'season_number': 5,
  67. 'episode_number': 5,
  68. 'chapters': [{'start_time': 0.0, 'end_time': 3409.08}, {'start_time': 3409.08, 'end_time': 6565.008}],
  69. },
  70. }, {
  71. 'url': 'https://www.mediasetplay.mediaset.it/video/cameracafe5/episodio-69-pezzo-di-luna_F303843101017801',
  72. 'md5': 'd1650ac9ff944f185556126a736df148',
  73. 'info_dict': {
  74. 'id': 'F303843101017801',
  75. 'ext': 'mp4',
  76. 'title': 'Episodio 69 - Pezzo di luna',
  77. 'description': 'md5:7c32c8ec4118b72588b9412f11353f73',
  78. 'thumbnail': r're:^https?://.*\.jpg$',
  79. 'duration': 263.008,
  80. 'upload_date': '20200902',
  81. 'series': 'Camera Café 5',
  82. 'timestamp': 1599064700,
  83. 'uploader': 'Italia 1',
  84. 'uploader_id': 'I1',
  85. 'season': 'Season 5',
  86. 'episode': 'Episode 178',
  87. 'season_number': 5,
  88. 'episode_number': 178,
  89. 'chapters': [{'start_time': 0.0, 'end_time': 261.88}, {'start_time': 261.88, 'end_time': 263.008}],
  90. },
  91. }, {
  92. 'url': 'https://www.mediasetplay.mediaset.it/video/cameracafe5/episodio-51-tu-chi-sei_F303843107000601',
  93. 'md5': '567e9ad375b7a27a0e370650f572a1e3',
  94. 'info_dict': {
  95. 'id': 'F303843107000601',
  96. 'ext': 'mp4',
  97. 'title': 'Episodio 51 - Tu chi sei?',
  98. 'description': 'md5:42ef006e56824cc31787a547590923f4',
  99. 'thumbnail': r're:^https?://.*\.jpg$',
  100. 'duration': 367.021,
  101. 'upload_date': '20200902',
  102. 'series': 'Camera Café 5',
  103. 'timestamp': 1599069817,
  104. 'uploader': 'Italia 1',
  105. 'uploader_id': 'I1',
  106. 'season': 'Season 5',
  107. 'episode': 'Episode 6',
  108. 'season_number': 5,
  109. 'episode_number': 6,
  110. 'chapters': [{'start_time': 0.0, 'end_time': 358.68}, {'start_time': 358.68, 'end_time': 367.021}],
  111. },
  112. }, {
  113. # movie
  114. 'url': 'https://www.mediasetplay.mediaset.it/movie/selvaggi/selvaggi_F006474501000101',
  115. 'md5': '720440187a2ae26af8148eb9e6b901ed',
  116. 'info_dict': {
  117. 'id': 'F006474501000101',
  118. 'ext': 'mp4',
  119. 'title': 'Selvaggi',
  120. 'description': 'md5:cfdedbbfdd12d4d0e5dcf1fa1b75284f',
  121. 'thumbnail': r're:^https?://.*\.jpg$',
  122. 'duration': 5233.01,
  123. 'upload_date': '20210729',
  124. 'timestamp': 1627594716,
  125. 'uploader': 'Cine34',
  126. 'uploader_id': 'B6',
  127. 'chapters': [{'start_time': 0.0, 'end_time': 1938.56}, {'start_time': 1938.56, 'end_time': 5233.01}],
  128. },
  129. }, {
  130. # clip
  131. 'url': 'https://www.mediasetplay.mediaset.it/video/gogglebox/un-grande-classico-della-commedia-sexy_FAFU000000661680',
  132. 'only_matching': True,
  133. }, {
  134. # iframe simple
  135. 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
  136. 'only_matching': True,
  137. }, {
  138. # iframe twitter (from http://www.wittytv.it/se-prima-mi-fidavo-zero/)
  139. 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665104&id=665104',
  140. 'only_matching': True,
  141. }, {
  142. # embedUrl (from https://www.wittytv.it/amici/est-ce-que-tu-maimes-gabriele-5-dicembre-copia/)
  143. 'url': 'https://static3.mediasetplay.mediaset.it/player/v2/index.html?partnerId=wittytv&configId=&programGuid=FD00000000153323&autoplay=true&purl=http://www.wittytv.it/amici/est-ce-que-tu-maimes-gabriele-5-dicembre-copia/',
  144. 'only_matching': True,
  145. }, {
  146. 'url': 'mediaset:FAFU000000665924',
  147. 'only_matching': True,
  148. }, {
  149. 'url': 'https://www.mediasetplay.mediaset.it/video/mediasethaacuoreilfuturo/palmieri-alicudi-lisola-dei-tre-bambini-felici--un-decreto-per-alicudi-e-tutte-le-microscuole_FD00000000102295',
  150. 'only_matching': True,
  151. }, {
  152. 'url': 'https://www.mediasetplay.mediaset.it/video/cherryseason/anticipazioni-degli-episodi-del-23-ottobre_F306837101005C02',
  153. 'only_matching': True,
  154. }, {
  155. 'url': 'https://www.mediasetplay.mediaset.it/video/tg5/ambiente-onda-umana-per-salvare-il-pianeta_F309453601079D01',
  156. 'only_matching': True,
  157. }, {
  158. 'url': 'https://www.mediasetplay.mediaset.it/video/grandefratellovip/benedetta-una-doccia-gelata_F309344401044C135',
  159. 'only_matching': True,
  160. }, {
  161. 'url': 'https://www.mediasetplay.mediaset.it/movie/herculeslaleggendahainizio/hercules-la-leggenda-ha-inizio_F305927501000102',
  162. 'only_matching': True,
  163. }, {
  164. 'url': 'https://mediasetinfinity.mediaset.it/video/braveandbeautiful/episodio-113_F310948005000402',
  165. 'only_matching': True,
  166. }, {
  167. 'url': 'https://static3.mediasetplay.mediaset.it/player/v2/index.html?partnerId=wittytv&configId=&programGuid=FD00000000153323',
  168. 'only_matching': True,
  169. }]
  170. def _extract_from_webpage(self, url, webpage):
  171. def _program_guid(qs):
  172. return qs.get('programGuid', [None])[0]
  173. for mobj in re.finditer(
  174. r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml.*?)\1',
  175. webpage):
  176. embed_url = mobj.group('url')
  177. embed_qs = parse_qs(embed_url)
  178. program_guid = _program_guid(embed_qs)
  179. if program_guid:
  180. yield self.url_result(embed_url)
  181. continue
  182. video_id = embed_qs.get('id', [None])[0]
  183. if not video_id:
  184. continue
  185. urlh = self._request_webpage(embed_url, video_id, note='Following embed URL redirect')
  186. embed_url = urlh.geturl()
  187. program_guid = _program_guid(parse_qs(embed_url))
  188. if program_guid:
  189. yield self.url_result(embed_url)
  190. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  191. for video in smil.findall(self._xpath_ns('.//video', namespace)):
  192. video.attrib['src'] = re.sub(r'(https?://vod05)t(-mediaset-it\.akamaized\.net/.+?.mpd)\?.+', r'\1\2', video.attrib['src'])
  193. return super(MediasetIE, self)._parse_smil_formats(smil, smil_url, video_id, namespace, f4m_params, transform_rtmp_url)
  194. def _check_drm_formats(self, tp_formats, video_id):
  195. has_nondrm, drm_manifest = False, ''
  196. for f in tp_formats:
  197. if '_sampleaes/' in (f.get('manifest_url') or ''):
  198. drm_manifest = drm_manifest or f['manifest_url']
  199. f['has_drm'] = True
  200. if not f.get('has_drm') and f.get('manifest_url'):
  201. has_nondrm = True
  202. nodrm_manifest = re.sub(r'_sampleaes/(\w+)_fp_', r'/\1_no_', drm_manifest)
  203. if has_nondrm or nodrm_manifest == drm_manifest:
  204. return
  205. tp_formats.extend(self._extract_m3u8_formats(
  206. nodrm_manifest, video_id, m3u8_id='hls', fatal=False) or [])
  207. def _real_extract(self, url):
  208. guid = self._match_id(url)
  209. tp_path = 'PR1GhC/media/guid/2702976343/' + guid
  210. info = self._extract_theplatform_metadata(tp_path, guid)
  211. formats = []
  212. subtitles = {}
  213. first_e = geo_e = None
  214. asset_type = 'geoNo:HD,browser,geoIT|geoNo:HD,geoIT|geoNo:SD,browser,geoIT|geoNo:SD,geoIT|geoNo|HD|SD'
  215. # TODO: fixup ISM+none manifest URLs
  216. for f in ('MPEG4', 'M3U'):
  217. try:
  218. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  219. update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
  220. 'mbr': 'true',
  221. 'formats': f,
  222. 'assetTypes': asset_type,
  223. }), guid, 'Downloading %s SMIL data' % (f.split('+')[0]))
  224. except ExtractorError as e:
  225. if not geo_e and isinstance(e, GeoRestrictedError):
  226. geo_e = e
  227. if not first_e:
  228. first_e = e
  229. continue
  230. self._check_drm_formats(tp_formats, guid)
  231. formats.extend(tp_formats)
  232. subtitles = self._merge_subtitles(subtitles, tp_subtitles)
  233. # check for errors and report them
  234. if (first_e or geo_e) and not formats:
  235. raise geo_e or first_e
  236. feed_data = self._download_json(
  237. 'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs-v2/guid/-/' + guid,
  238. guid, fatal=False)
  239. if feed_data:
  240. publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
  241. thumbnails = feed_data.get('thumbnails') or {}
  242. thumbnail = None
  243. for key, value in thumbnails.items():
  244. if key.startswith('image_keyframe_poster-'):
  245. thumbnail = value.get('url')
  246. break
  247. info.update({
  248. 'description': info.get('description') or feed_data.get('description') or feed_data.get('longDescription'),
  249. 'uploader': publish_info.get('description'),
  250. 'uploader_id': publish_info.get('channel'),
  251. 'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
  252. 'thumbnail': thumbnail,
  253. })
  254. if feed_data.get('programType') == 'episode':
  255. info.update({
  256. 'episode_number': int_or_none(
  257. feed_data.get('tvSeasonEpisodeNumber')),
  258. 'season_number': int_or_none(
  259. feed_data.get('tvSeasonNumber')),
  260. 'series': feed_data.get('mediasetprogram$brandTitle'),
  261. })
  262. info.update({
  263. 'id': guid,
  264. 'formats': formats,
  265. 'subtitles': subtitles,
  266. })
  267. return info
  268. class MediasetShowIE(MediasetIE): # XXX: Do not subclass from concrete IE
  269. _VALID_URL = r'''(?x)
  270. (?:
  271. https?://
  272. (\w+\.)+mediaset\.it/
  273. (?:
  274. (?:fiction|programmi-tv|serie-tv|kids)/(?:.+?/)?
  275. (?:[a-z-]+)_SE(?P<id>\d{12})
  276. (?:,ST(?P<st>\d{12}))?
  277. (?:,sb(?P<sb>\d{9}))?$
  278. )
  279. )
  280. '''
  281. _TESTS = [{
  282. # TV Show webpage (general webpage)
  283. 'url': 'https://www.mediasetplay.mediaset.it/programmi-tv/leiene/leiene_SE000000000061',
  284. 'info_dict': {
  285. 'id': '000000000061',
  286. 'title': 'Le Iene',
  287. },
  288. 'playlist_mincount': 7,
  289. }, {
  290. # TV Show webpage (specific season)
  291. 'url': 'https://www.mediasetplay.mediaset.it/programmi-tv/leiene/leiene_SE000000000061,ST000000002763',
  292. 'info_dict': {
  293. 'id': '000000002763',
  294. 'title': 'Le Iene',
  295. },
  296. 'playlist_mincount': 7,
  297. }, {
  298. # TV Show specific playlist (with multiple pages)
  299. 'url': 'https://www.mediasetplay.mediaset.it/programmi-tv/leiene/iservizi_SE000000000061,ST000000002763,sb100013375',
  300. 'info_dict': {
  301. 'id': '100013375',
  302. 'title': 'I servizi',
  303. },
  304. 'playlist_mincount': 50,
  305. }]
  306. _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'
  307. _PAGE_SIZE = 25
  308. def _fetch_page(self, sb, page):
  309. lower_limit = page * self._PAGE_SIZE + 1
  310. upper_limit = lower_limit + self._PAGE_SIZE - 1
  311. content = self._download_json(
  312. self._BY_SUBBRAND % (sb, lower_limit, upper_limit), sb)
  313. for entry in content.get('entries') or []:
  314. yield self.url_result(
  315. 'mediaset:' + entry['guid'],
  316. playlist_title=entry['mediasetprogram$subBrandDescription'])
  317. def _real_extract(self, url):
  318. playlist_id, st, sb = self._match_valid_url(url).group('id', 'st', 'sb')
  319. if not sb:
  320. page = self._download_webpage(url, st or playlist_id)
  321. entries = [self.url_result(urljoin('https://www.mediasetplay.mediaset.it', url))
  322. for url in re.findall(r'href="([^<>=]+SE\d{12},ST\d{12},sb\d{9})">[^<]+<', page)]
  323. title = (self._html_search_regex(r'(?s)<h1[^>]*>(.+?)</h1>', page, 'title', default=None)
  324. or self._og_search_title(page))
  325. return self.playlist_result(entries, st or playlist_id, title)
  326. entries = OnDemandPagedList(
  327. functools.partial(self._fetch_page, sb),
  328. self._PAGE_SIZE)
  329. title = try_get(entries, lambda x: x[0]['playlist_title'])
  330. return self.playlist_result(entries, sb, title)