[mediaset] Fix issues and extract all formats (closes #16568)
[youtube-dl] / youtube_dl / extractor / mediaset.py
index 4b1489524f21decada1bd358584b3e2caa5e9c29..9f2b60dcc5b62f83800101822bfb3da79d43badc 100644 (file)
@@ -1,19 +1,30 @@
 # coding: utf-8
 from __future__ import unicode_literals
 
-from .common import (
-    InfoExtractor,
-    ExtractorError
-)
+import re
+
+from .common import InfoExtractor
+from ..compat import compat_str
 from ..utils import (
     determine_ext,
     parse_duration,
-    unified_strdate
+    try_get,
+    unified_strdate,
 )
 
 
 class MediasetIE(InfoExtractor):
-    _VALID_URL = r'https?://www\.video\.mediaset\.it/(?:(?:video|on-demand)/(?:.+)_|player/playerIFrame(?:Twitter)?\.shtml\?id=)(?P<id>[0-9]+)(?:.html|&.+)'
+    _VALID_URL = r'''(?x)
+                    (?:
+                        mediaset:|
+                        https?://
+                            (?:www\.)?video\.mediaset\.it/
+                            (?:
+                                (?:video|on-demand)/(?:[^/]+/)+[^/]+_|
+                                player/playerIFrame(?:Twitter)?\.shtml\?.*?\bid=
+                            )
+                    )(?P<id>[0-9]+)
+                    '''
     _TESTS = [{
         # full episode
         'url': 'http://www.video.mediaset.it/video/hello_goodbye/full/quarta-puntata_661824.html',
@@ -22,97 +33,112 @@ class MediasetIE(InfoExtractor):
             'id': '661824',
             'ext': 'mp4',
             'title': 'Quarta puntata',
-            'thumbnail': 're:^https?://.*\.jpg$',
             'description': 'md5:7183696d6df570e3412a5ef74b27c5e2',
-            'uploader': 'mediaset'
-        }
+            'thumbnail': r're:^https?://.*\.jpg$',
+            'duration': 1414,
+            'creator': 'mediaset',
+            'upload_date': '20161107',
+            'series': 'Hello Goodbye',
+            'categories': ['reality'],
+        },
+        'expected_warnings': ['is not a supported codec'],
     }, {
-        # on demand
-        'url': 'http://www.video.mediaset.it/video/domenica_live/interviste/il-fenomeno-elettra-lamborghini_716283.html',
-        'md5': '81c57566bf2ee02e995f5342f079ca25',
+        'url': 'http://www.video.mediaset.it/video/matrix/full_chiambretti/puntata-del-25-maggio_846685.html',
+        'md5': '1276f966ac423d16ba255ce867de073e',
         'info_dict': {
-            'id': '716283',
+            'id': '846685',
             'ext': 'mp4',
-            'title': 'Il fenomeno Elettra Lamborghini',
-            'thumbnail': 're:^https?://.*\.jpg$',
-            'description': 'md5:dabf0e7cf48fc6d0a3417b989028748a',
-            'uploader': 'mediaset'
-        }
+            'title': 'Puntata del 25 maggio',
+            'description': 'md5:ee2e456e3eb1dba5e814596655bb5296',
+            'thumbnail': r're:^https?://.*\.jpg$',
+            'duration': 6565,
+            'creator': 'mediaset',
+            'upload_date': '20180525',
+            'series': 'Matrix',
+            'categories': ['infotainment'],
+        },
+        'expected_warnings': ['HTTP Error 403: Forbidden'],
     }, {
         # 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'
-        }
+        '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'
-        }
+        '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'
-        }
+        'only_matching': True,
+    }, {
+        'url': 'mediaset:661824',
+        'only_matching': True,
     }]
 
+    @staticmethod
+    def _extract_urls(webpage):
+        return [
+            mobj.group('url')
+            for mobj in re.finditer(
+                r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>https?://(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml\?.*?\bid=\d+.*?)\1',
+                webpage)]
+
     def _real_extract(self, url):
         video_id = self._match_id(url)
-        formats = []
-        uploader = None
-        categories = None
-
-        mediainfo = self._download_json(
-            'http://plr.video.mediaset.it/html/metainfo.sjson?id=%s' % video_id,
-            video_id, 'Downloading video info JSON').get('video')
 
-        if 'brand-info' in mediainfo:
-            uploader = mediainfo.get('brand-info').get('publisher')
-            categories = [mediainfo.get('brand-info').get('category')]
+        video = self._download_json(
+            'https://www.video.mediaset.it/html/metainfo.sjson',
+            video_id, 'Downloading media info', query={
+                'id': video_id
+            })['video']
 
-        cnd = self._download_json(
-            'http://cdnsel01.mediaset.net/GetCdn.aspx?streamid=%s&format=json' % video_id,
-            video_id, 'Downloading video CND JSON')
+        title = video['title']
+        media_id = video.get('guid') or video_id
 
-        if not cnd.get('videoList'):
-            raise ExtractorError('Video not found')
+        video_list = self._download_json(
+            'http://cdnsel01.mediaset.net/GetCdn2018.aspx',
+            video_id, 'Downloading video CDN JSON', query={
+                'streamid': media_id,
+                'format': 'json',
+            })['videoList']
 
-        for media_url in cnd.get('videoList'):
-            formats.append({
-                'url': media_url,
-                'ext': determine_ext(media_url)
-            })
+        formats = []
+        for format_url in video_list:
+            ext = determine_ext(format_url)
+            if ext == 'm3u8':
+                formats.extend(self._extract_m3u8_formats(
+                    format_url, video_id, 'mp4', entry_protocol='m3u8_native',
+                    m3u8_id='hls', fatal=False))
+            elif ext == 'mpd':
+                formats.extend(self._extract_mpd_formats(
+                    format_url, video_id, mpd_id='dash', fatal=False))
+            elif ext == 'ism' or '.ism' in format_url:
+                formats.extend(self._extract_ism_formats(
+                    format_url, video_id, ism_id='mss', fatal=False))
+            else:
+                formats.append({
+                    'url': format_url,
+                    'format_id': determine_ext(format_url),
+                })
         self._sort_formats(formats)
 
+        creator = try_get(
+            video, lambda x: x['brand-info']['publisher'], compat_str)
+        category = try_get(
+            video, lambda x: x['brand-info']['category'], compat_str)
+        categories = [category] if category else None
+
         return {
             'id': video_id,
-            'title': mediainfo.get('title'),
+            'title': title,
+            'description': video.get('short-description'),
+            'thumbnail': video.get('thumbnail'),
+            'duration': parse_duration(video.get('duration')),
+            'creator': creator,
+            'upload_date': unified_strdate(video.get('production-date')),
+            'webpage_url': video.get('url'),
+            'series': video.get('brand-value'),
+            'season': video.get('season'),
+            'categories': categories,
             '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
         }