mediaset.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .theplatform import ThePlatformBaseIE
  5. from ..compat import (
  6. compat_parse_qs,
  7. compat_urllib_parse_urlparse,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. int_or_none,
  12. update_url_query,
  13. )
  14. class MediasetIE(ThePlatformBaseIE):
  15. _TP_TLD = 'eu'
  16. _VALID_URL = r'''(?x)
  17. (?:
  18. mediaset:|
  19. https?://
  20. (?:(?:www|static3)\.)?mediasetplay\.mediaset\.it/
  21. (?:
  22. (?:video|on-demand)/(?:[^/]+/)+[^/]+_|
  23. player/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/hellogoodbye/quarta-puntata_FAFU000000661824',
  30. 'md5': '9b75534d42c44ecef7bf1ffeacb7f85d',
  31. 'info_dict': {
  32. 'id': 'FAFU000000661824',
  33. 'ext': 'mp4',
  34. 'title': 'Quarta puntata',
  35. 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
  36. 'thumbnail': r're:^https?://.*\.jpg$',
  37. 'duration': 1414.26,
  38. 'upload_date': '20161107',
  39. 'series': 'Hello Goodbye',
  40. 'timestamp': 1478532900,
  41. 'uploader': 'Rete 4',
  42. 'uploader_id': 'R4',
  43. },
  44. }, {
  45. 'url': 'https://www.mediasetplay.mediaset.it/video/matrix/puntata-del-25-maggio_F309013801000501',
  46. 'md5': '288532f0ad18307705b01e581304cd7b',
  47. 'info_dict': {
  48. 'id': 'F309013801000501',
  49. 'ext': 'mp4',
  50. 'title': 'Puntata del 25 maggio',
  51. 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
  52. 'thumbnail': r're:^https?://.*\.jpg$',
  53. 'duration': 6565.007,
  54. 'upload_date': '20180526',
  55. 'series': 'Matrix',
  56. 'timestamp': 1527326245,
  57. 'uploader': 'Canale 5',
  58. 'uploader_id': 'C5',
  59. },
  60. }, {
  61. # clip
  62. 'url': 'https://www.mediasetplay.mediaset.it/video/gogglebox/un-grande-classico-della-commedia-sexy_FAFU000000661680',
  63. 'only_matching': True,
  64. }, {
  65. # iframe simple
  66. 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
  67. 'only_matching': True,
  68. }, {
  69. # iframe twitter (from http://www.wittytv.it/se-prima-mi-fidavo-zero/)
  70. 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665104&id=665104',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'mediaset:FAFU000000665924',
  74. 'only_matching': True,
  75. }, {
  76. 'url': 'https://www.mediasetplay.mediaset.it/video/mediasethaacuoreilfuturo/palmieri-alicudi-lisola-dei-tre-bambini-felici--un-decreto-per-alicudi-e-tutte-le-microscuole_FD00000000102295',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://www.mediasetplay.mediaset.it/video/cherryseason/anticipazioni-degli-episodi-del-23-ottobre_F306837101005C02',
  80. 'only_matching': True,
  81. }, {
  82. 'url': 'https://www.mediasetplay.mediaset.it/video/tg5/ambiente-onda-umana-per-salvare-il-pianeta_F309453601079D01',
  83. 'only_matching': True,
  84. }, {
  85. 'url': 'https://www.mediasetplay.mediaset.it/video/grandefratellovip/benedetta-una-doccia-gelata_F309344401044C135',
  86. 'only_matching': True,
  87. }]
  88. @staticmethod
  89. def _extract_urls(ie, webpage):
  90. def _qs(url):
  91. return compat_parse_qs(compat_urllib_parse_urlparse(url).query)
  92. def _program_guid(qs):
  93. return qs.get('programGuid', [None])[0]
  94. entries = []
  95. for mobj in re.finditer(
  96. r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml.*?)\1',
  97. webpage):
  98. embed_url = mobj.group('url')
  99. embed_qs = _qs(embed_url)
  100. program_guid = _program_guid(embed_qs)
  101. if program_guid:
  102. entries.append(embed_url)
  103. continue
  104. video_id = embed_qs.get('id', [None])[0]
  105. if not video_id:
  106. continue
  107. urlh = ie._request_webpage(
  108. embed_url, video_id, note='Following embed URL redirect')
  109. embed_url = urlh.geturl()
  110. program_guid = _program_guid(_qs(embed_url))
  111. if program_guid:
  112. entries.append(embed_url)
  113. return entries
  114. def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
  115. for video in smil.findall(self._xpath_ns('.//video', namespace)):
  116. video.attrib['src'] = re.sub(r'(https?://vod05)t(-mediaset-it\.akamaized\.net/.+?.mpd)\?.+', r'\1\2', video.attrib['src'])
  117. return super(MediasetIE, self)._parse_smil_formats(smil, smil_url, video_id, namespace, f4m_params, transform_rtmp_url)
  118. def _real_extract(self, url):
  119. guid = self._match_id(url)
  120. tp_path = 'PR1GhC/media/guid/2702976343/' + guid
  121. info = self._extract_theplatform_metadata(tp_path, guid)
  122. formats = []
  123. subtitles = {}
  124. first_e = None
  125. for asset_type in ('SD', 'HD'):
  126. # TODO: fixup ISM+none manifest URLs
  127. for f in ('MPEG4', 'MPEG-DASH+none', 'M3U+none'):
  128. try:
  129. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  130. update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
  131. 'mbr': 'true',
  132. 'formats': f,
  133. 'assetTypes': asset_type,
  134. }), guid, 'Downloading %s %s SMIL data' % (f.split('+')[0], asset_type))
  135. except ExtractorError as e:
  136. if not first_e:
  137. first_e = e
  138. break
  139. for tp_f in tp_formats:
  140. tp_f['quality'] = 1 if asset_type == 'HD' else 0
  141. formats.extend(tp_formats)
  142. subtitles = self._merge_subtitles(subtitles, tp_subtitles)
  143. if first_e and not formats:
  144. raise first_e
  145. self._sort_formats(formats)
  146. fields = []
  147. for templ, repls in (('tvSeason%sNumber', ('', 'Episode')), ('mediasetprogram$%s', ('brandTitle', 'numberOfViews', 'publishInfo'))):
  148. fields.extend(templ % repl for repl in repls)
  149. feed_data = self._download_json(
  150. 'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs/guid/-/' + guid,
  151. guid, fatal=False, query={'fields': ','.join(fields)})
  152. if feed_data:
  153. publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
  154. info.update({
  155. 'episode_number': int_or_none(feed_data.get('tvSeasonEpisodeNumber')),
  156. 'season_number': int_or_none(feed_data.get('tvSeasonNumber')),
  157. 'series': feed_data.get('mediasetprogram$brandTitle'),
  158. 'uploader': publish_info.get('description'),
  159. 'uploader_id': publish_info.get('channel'),
  160. 'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
  161. })
  162. info.update({
  163. 'id': guid,
  164. 'formats': formats,
  165. 'subtitles': subtitles,
  166. })
  167. return info