[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / mediaset.py
index 4b1489524f21decada1bd358584b3e2caa5e9c29..933df14952d5cc16857485e306be07f2d32384d3 100644 (file)
 # coding: utf-8
 from __future__ import unicode_literals
 
-from .common import (
-    InfoExtractor,
-    ExtractorError
+import re
+
+from .theplatform import ThePlatformBaseIE
+from ..compat import (
+    compat_parse_qs,
+    compat_urllib_parse_urlparse,
 )
 from ..utils import (
-    determine_ext,
-    parse_duration,
-    unified_strdate
+    ExtractorError,
+    int_or_none,
+    update_url_query,
 )
 
 
-class MediasetIE(InfoExtractor):
-    _VALID_URL = r'https?://www\.video\.mediaset\.it/(?:(?:video|on-demand)/(?:.+)_|player/playerIFrame(?:Twitter)?\.shtml\?id=)(?P<id>[0-9]+)(?:.html|&.+)'
+class MediasetIE(ThePlatformBaseIE):
+    _TP_TLD = 'eu'
+    _VALID_URL = r'''(?x)
+                    (?:
+                        mediaset:|
+                        https?://
+                            (?:(?:www|static3)\.)?mediasetplay\.mediaset\.it/
+                            (?:
+                                (?:video|on-demand)/(?:[^/]+/)+[^/]+_|
+                                player/index\.html\?.*?\bprogramGuid=
+                            )
+                    )(?P<id>[0-9A-Z]{16,})
+                    '''
     _TESTS = [{
         # full episode
-        'url': 'http://www.video.mediaset.it/video/hello_goodbye/full/quarta-puntata_661824.html',
+        'url': 'https://www.mediasetplay.mediaset.it/video/hellogoodbye/quarta-puntata_FAFU000000661824',
         'md5': '9b75534d42c44ecef7bf1ffeacb7f85d',
         'info_dict': {
-            'id': '661824',
+            'id': 'FAFU000000661824',
             'ext': 'mp4',
             'title': 'Quarta puntata',
-            'thumbnail': 're:^https?://.*\.jpg$',
-            'description': 'md5:7183696d6df570e3412a5ef74b27c5e2',
-            'uploader': 'mediaset'
-        }
+            'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
+            'thumbnail': r're:^https?://.*\.jpg$',
+            'duration': 1414.26,
+            'upload_date': '20161107',
+            'series': 'Hello Goodbye',
+            'timestamp': 1478532900,
+            'uploader': 'Rete 4',
+            'uploader_id': 'R4',
+        },
     }, {
-        # on demand
-        'url': 'http://www.video.mediaset.it/video/domenica_live/interviste/il-fenomeno-elettra-lamborghini_716283.html',
-        'md5': '81c57566bf2ee02e995f5342f079ca25',
+        'url': 'https://www.mediasetplay.mediaset.it/video/matrix/puntata-del-25-maggio_F309013801000501',
+        'md5': '288532f0ad18307705b01e581304cd7b',
         'info_dict': {
-            'id': '716283',
+            'id': 'F309013801000501',
             'ext': 'mp4',
-            'title': 'Il fenomeno Elettra Lamborghini',
-            'thumbnail': 're:^https?://.*\.jpg$',
-            'description': 'md5:dabf0e7cf48fc6d0a3417b989028748a',
-            'uploader': 'mediaset'
-        }
+            'title': 'Puntata del 25 maggio',
+            'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
+            'thumbnail': r're:^https?://.*\.jpg$',
+            'duration': 6565.007,
+            'upload_date': '20180526',
+            'series': 'Matrix',
+            'timestamp': 1527326245,
+            'uploader': 'Canale 5',
+            'uploader_id': 'C5',
+        },
     }, {
         # clip
-        'url': 'http://www.video.mediaset.it/video/gogglebox/clip/un-grande-classico-della-commedia-sexy_661680.html',
-        'md5': '189ca72fe399db80dbfa595a4abf42d0',
-        'info_dict': {
-            'id': '661680',
-            'ext': 'mp4',
-            'title': 'Un grande classico della commedia sexy',
-            'thumbnail': 're:^https?://.*\.jpg$',
-            'description': 'Un film che riesce a risvegliare i sensi di Gigi.',
-            'uploader': 'mediaset'
-        }
+        'url': 'https://www.mediasetplay.mediaset.it/video/gogglebox/un-grande-classico-della-commedia-sexy_FAFU000000661680',
+        'only_matching': True,
     }, {
         # iframe simple
-        'url': 'http://www.video.mediaset.it/player/playerIFrame.shtml?id=665924&autoplay=true',
-        'md5': '308430901e55e1ad83dddb4be2a4454a',
-        'info_dict': {
-            'id': '665924',
-            'ext': 'mp4',
-            'title': 'Gianna Nannini incontra i fan a Milano',
-            'thumbnail': 're:^https?://.*\.jpg$',
-            'description': 'La cantante parla del nuovo libro',
-            'uploader': 'mediaset'
-        }
+        'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
+        'only_matching': True,
     }, {
         # iframe twitter (from http://www.wittytv.it/se-prima-mi-fidavo-zero/)
-        'url': 'https://www.video.mediaset.it/player/playerIFrameTwitter.shtml?id=665104&playrelated=false&autoplay=false&related=true&hidesocial=true',
-        'md5': '6f53a834b3b5eac1ebc2037ccf7194d0',
-        'info_dict': {
-            'id': '665104',
-            'ext': 'mp4',
-            'title': '\"Se prima mi fidavo zero...\"',
-            'thumbnail': 're:^https?://.*\.jpg$',
-            'description': 'Una piccola anteprima della prossima puntata del Trono Classico',
-            'uploader': 'mediaset'
-        }
+        'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665104&id=665104',
+        'only_matching': True,
+    }, {
+        'url': 'mediaset:FAFU000000665924',
+        'only_matching': True,
+    }, {
+        'url': 'https://www.mediasetplay.mediaset.it/video/mediasethaacuoreilfuturo/palmieri-alicudi-lisola-dei-tre-bambini-felici--un-decreto-per-alicudi-e-tutte-le-microscuole_FD00000000102295',
+        'only_matching': True,
+    }, {
+        'url': 'https://www.mediasetplay.mediaset.it/video/cherryseason/anticipazioni-degli-episodi-del-23-ottobre_F306837101005C02',
+        'only_matching': True,
+    }, {
+        'url': 'https://www.mediasetplay.mediaset.it/video/tg5/ambiente-onda-umana-per-salvare-il-pianeta_F309453601079D01',
+        'only_matching': True,
+    }, {
+        'url': 'https://www.mediasetplay.mediaset.it/video/grandefratellovip/benedetta-una-doccia-gelata_F309344401044C135',
+        'only_matching': True,
     }]
 
-    def _real_extract(self, url):
-        video_id = self._match_id(url)
-        formats = []
-        uploader = None
-        categories = None
+    @staticmethod
+    def _extract_urls(ie, webpage):
+        def _qs(url):
+            return compat_parse_qs(compat_urllib_parse_urlparse(url).query)
 
-        mediainfo = self._download_json(
-            'http://plr.video.mediaset.it/html/metainfo.sjson?id=%s' % video_id,
-            video_id, 'Downloading video info JSON').get('video')
+        def _program_guid(qs):
+            return qs.get('programGuid', [None])[0]
 
-        if 'brand-info' in mediainfo:
-            uploader = mediainfo.get('brand-info').get('publisher')
-            categories = [mediainfo.get('brand-info').get('category')]
+        entries = []
+        for mobj in re.finditer(
+                r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml.*?)\1',
+                webpage):
+            embed_url = mobj.group('url')
+            embed_qs = _qs(embed_url)
+            program_guid = _program_guid(embed_qs)
+            if program_guid:
+                entries.append(embed_url)
+                continue
+            video_id = embed_qs.get('id', [None])[0]
+            if not video_id:
+                continue
+            urlh = ie._request_webpage(
+                embed_url, video_id, note='Following embed URL redirect')
+            embed_url = urlh.geturl()
+            program_guid = _program_guid(_qs(embed_url))
+            if program_guid:
+                entries.append(embed_url)
+        return entries
 
-        cnd = self._download_json(
-            'http://cdnsel01.mediaset.net/GetCdn.aspx?streamid=%s&format=json' % video_id,
-            video_id, 'Downloading video CND JSON')
+    def _parse_smil_formats(self, smil, smil_url, video_id, namespace=None, f4m_params=None, transform_rtmp_url=None):
+        for video in smil.findall(self._xpath_ns('.//video', namespace)):
+            video.attrib['src'] = re.sub(r'(https?://vod05)t(-mediaset-it\.akamaized\.net/.+?.mpd)\?.+', r'\1\2', video.attrib['src'])
+        return super(MediasetIE, self)._parse_smil_formats(smil, smil_url, video_id, namespace, f4m_params, transform_rtmp_url)
 
-        if not cnd.get('videoList'):
-            raise ExtractorError('Video not found')
+    def _real_extract(self, url):
+        guid = self._match_id(url)
+        tp_path = 'PR1GhC/media/guid/2702976343/' + guid
+        info = self._extract_theplatform_metadata(tp_path, guid)
 
-        for media_url in cnd.get('videoList'):
-            formats.append({
-                'url': media_url,
-                'ext': determine_ext(media_url)
-            })
+        formats = []
+        subtitles = {}
+        first_e = None
+        for asset_type in ('SD', 'HD'):
+            # TODO: fixup ISM+none manifest URLs
+            for f in ('MPEG4', 'MPEG-DASH+none', 'M3U+none'):
+                try:
+                    tp_formats, tp_subtitles = self._extract_theplatform_smil(
+                        update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
+                            'mbr': 'true',
+                            'formats': f,
+                            'assetTypes': asset_type,
+                        }), guid, 'Downloading %s %s SMIL data' % (f.split('+')[0], asset_type))
+                except ExtractorError as e:
+                    if not first_e:
+                        first_e = e
+                    break
+                for tp_f in tp_formats:
+                    tp_f['quality'] = 1 if asset_type == 'HD' else 0
+                formats.extend(tp_formats)
+                subtitles = self._merge_subtitles(subtitles, tp_subtitles)
+        if first_e and not formats:
+            raise first_e
         self._sort_formats(formats)
 
-        return {
-            'id': video_id,
-            'title': mediainfo.get('title'),
+        fields = []
+        for templ, repls in (('tvSeason%sNumber', ('', 'Episode')), ('mediasetprogram$%s', ('brandTitle', 'numberOfViews', 'publishInfo'))):
+            fields.extend(templ % repl for repl in repls)
+        feed_data = self._download_json(
+            'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs/guid/-/' + guid,
+            guid, fatal=False, query={'fields': ','.join(fields)})
+        if feed_data:
+            publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
+            info.update({
+                'episode_number': int_or_none(feed_data.get('tvSeasonEpisodeNumber')),
+                'season_number': int_or_none(feed_data.get('tvSeasonNumber')),
+                'series': feed_data.get('mediasetprogram$brandTitle'),
+                'uploader': publish_info.get('description'),
+                'uploader_id': publish_info.get('channel'),
+                'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
+            })
+
+        info.update({
+            'id': guid,
             'formats': formats,
-            'description': mediainfo.get('short-description'),
-            'uploader': uploader,
-            'thumbnail': mediainfo.get('thumbnail'),
-            'duration': parse_duration(mediainfo.get('duration')),
-            'release_date': unified_strdate(mediainfo.get('production-date')),
-            'webpage_url': mediainfo.get('url'),
-            'categories': categories
-        }
+            'subtitles': subtitles,
+        })
+        return info