]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/mediaset.py
[acast] add support for URLs with episode id(closes #21444)
[yt-dlp.git] / youtube_dl / extractor / mediaset.py
CommitLineData
0de13634
T
1# coding: utf-8
2from __future__ import unicode_literals
3
ca04de46
S
4import re
5
38f1eb0a 6from .theplatform import ThePlatformBaseIE
8fd12a08
S
7from ..compat import (
8 compat_parse_qs,
9 compat_str,
10 compat_urllib_parse_urlparse,
11)
0de13634 12from ..utils import (
38f1eb0a
RA
13 ExtractorError,
14 int_or_none,
15 update_url_query,
0de13634
T
16)
17
18
38f1eb0a
RA
19class MediasetIE(ThePlatformBaseIE):
20 _TP_TLD = 'eu'
56f9c77f 21 _VALID_URL = r'''(?x)
ca04de46
S
22 (?:
23 mediaset:|
24 https?://
38f1eb0a 25 (?:(?:www|static3)\.)?mediasetplay\.mediaset\.it/
ca04de46
S
26 (?:
27 (?:video|on-demand)/(?:[^/]+/)+[^/]+_|
38f1eb0a 28 player/index\.html\?.*?\bprogramGuid=
ca04de46 29 )
38f1eb0a 30 )(?P<id>[0-9A-Z]{16})
56f9c77f 31 '''
0de13634
T
32 _TESTS = [{
33 # full episode
38f1eb0a 34 'url': 'https://www.mediasetplay.mediaset.it/video/hellogoodbye/quarta-puntata_FAFU000000661824',
0de13634
T
35 'md5': '9b75534d42c44ecef7bf1ffeacb7f85d',
36 'info_dict': {
38f1eb0a 37 'id': 'FAFU000000661824',
0de13634
T
38 'ext': 'mp4',
39 'title': 'Quarta puntata',
38f1eb0a 40 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
56f9c77f 41 'thumbnail': r're:^https?://.*\.jpg$',
38f1eb0a 42 'duration': 1414.26,
d105a7ed 43 'upload_date': '20161107',
56f9c77f 44 'series': 'Hello Goodbye',
38f1eb0a
RA
45 'timestamp': 1478532900,
46 'uploader': 'Rete 4',
47 'uploader_id': 'R4',
56f9c77f 48 },
9cf648c9 49 }, {
38f1eb0a
RA
50 'url': 'https://www.mediasetplay.mediaset.it/video/matrix/puntata-del-25-maggio_F309013801000501',
51 'md5': '288532f0ad18307705b01e581304cd7b',
9cf648c9 52 'info_dict': {
38f1eb0a 53 'id': 'F309013801000501',
9cf648c9
T
54 'ext': 'mp4',
55 'title': 'Puntata del 25 maggio',
38f1eb0a 56 'description': 'md5:d41d8cd98f00b204e9800998ecf8427e',
9cf648c9 57 'thumbnail': r're:^https?://.*\.jpg$',
38f1eb0a
RA
58 'duration': 6565.007,
59 'upload_date': '20180526',
9cf648c9 60 'series': 'Matrix',
38f1eb0a
RA
61 'timestamp': 1527326245,
62 'uploader': 'Canale 5',
63 'uploader_id': 'C5',
9cf648c9 64 },
267d8196 65 'expected_warnings': ['HTTP Error 403: Forbidden'],
0de13634
T
66 }, {
67 # clip
38f1eb0a 68 'url': 'https://www.mediasetplay.mediaset.it/video/gogglebox/un-grande-classico-della-commedia-sexy_FAFU000000661680',
56f9c77f 69 'only_matching': True,
0de13634
T
70 }, {
71 # iframe simple
38f1eb0a 72 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665924&id=665924',
56f9c77f 73 'only_matching': True,
0de13634
T
74 }, {
75 # iframe twitter (from http://www.wittytv.it/se-prima-mi-fidavo-zero/)
38f1eb0a 76 'url': 'https://static3.mediasetplay.mediaset.it/player/index.html?appKey=5ad3966b1de1c4000d5cec48&programGuid=FAFU000000665104&id=665104',
56f9c77f 77 'only_matching': True,
ca04de46 78 }, {
38f1eb0a 79 'url': 'mediaset:FAFU000000665924',
ca04de46 80 'only_matching': True,
0de13634
T
81 }]
82
5d29af3d 83 @staticmethod
8fd12a08
S
84 def _extract_urls(ie, webpage):
85 def _qs(url):
86 return compat_parse_qs(compat_urllib_parse_urlparse(url).query)
87
88 def _program_guid(qs):
89 return qs.get('programGuid', [None])[0]
90
91 entries = []
92 for mobj in re.finditer(
93 r'<iframe\b[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?video\.mediaset\.it/player/playerIFrame(?:Twitter)?\.shtml.*?)\1',
94 webpage):
95 embed_url = mobj.group('url')
96 embed_qs = _qs(embed_url)
97 program_guid = _program_guid(embed_qs)
98 if program_guid:
99 entries.append(embed_url)
100 continue
101 video_id = embed_qs.get('id', [None])[0]
102 if not video_id:
103 continue
104 urlh = ie._request_webpage(
105 embed_url, video_id, note='Following embed URL redirect')
106 embed_url = compat_str(urlh.geturl())
107 program_guid = _program_guid(_qs(embed_url))
108 if program_guid:
109 entries.append(embed_url)
110 return entries
5d29af3d 111
0de13634 112 def _real_extract(self, url):
38f1eb0a
RA
113 guid = self._match_id(url)
114 tp_path = 'PR1GhC/media/guid/2702976343/' + guid
115 info = self._extract_theplatform_metadata(tp_path, guid)
0de13634 116
56f9c77f 117 formats = []
38f1eb0a
RA
118 subtitles = {}
119 first_e = None
120 for asset_type in ('SD', 'HD'):
121 for f in ('MPEG4', 'MPEG-DASH', 'M3U', 'ISM'):
122 try:
123 tp_formats, tp_subtitles = self._extract_theplatform_smil(
124 update_url_query('http://link.theplatform.%s/s/%s' % (self._TP_TLD, tp_path), {
125 'mbr': 'true',
126 'formats': f,
127 'assetTypes': asset_type,
128 }), guid, 'Downloading %s %s SMIL data' % (f, asset_type))
129 except ExtractorError as e:
130 if not first_e:
131 first_e = e
132 break
133 for tp_f in tp_formats:
134 tp_f['quality'] = 1 if asset_type == 'HD' else 0
135 formats.extend(tp_formats)
136 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
137 if first_e and not formats:
138 raise first_e
56f9c77f 139 self._sort_formats(formats)
0de13634 140
38f1eb0a
RA
141 fields = []
142 for templ, repls in (('tvSeason%sNumber', ('', 'Episode')), ('mediasetprogram$%s', ('brandTitle', 'numberOfViews', 'publishInfo'))):
143 fields.extend(templ % repl for repl in repls)
144 feed_data = self._download_json(
145 'https://feed.entertainment.tv.theplatform.eu/f/PR1GhC/mediaset-prod-all-programs/guid/-/' + guid,
146 guid, fatal=False, query={'fields': ','.join(fields)})
147 if feed_data:
148 publish_info = feed_data.get('mediasetprogram$publishInfo') or {}
149 info.update({
150 'episode_number': int_or_none(feed_data.get('tvSeasonEpisodeNumber')),
151 'season_number': int_or_none(feed_data.get('tvSeasonNumber')),
152 'series': feed_data.get('mediasetprogram$brandTitle'),
153 'uploader': publish_info.get('description'),
154 'uploader_id': publish_info.get('channel'),
155 'view_count': int_or_none(feed_data.get('mediasetprogram$numberOfViews')),
156 })
0de13634 157
38f1eb0a
RA
158 info.update({
159 'id': guid,
56f9c77f 160 'formats': formats,
38f1eb0a
RA
161 'subtitles': subtitles,
162 })
163 return info