[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / srgssr.py
blobc01fd12ccaf3fea04b976b80a60cbdeb462cc0df
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 float_or_none,
5 int_or_none,
6 join_nonempty,
7 parse_iso8601,
8 qualities,
9 try_get,
13 class SRGSSRIE(InfoExtractor):
14 _VALID_URL = r'''(?x)
15 (?:
16 https?://tp\.srgssr\.ch/p(?:/[^/]+)+\?urn=urn|
17 srgssr
19 (?P<bu>
20 srf|rts|rsi|rtr|swi
21 ):(?:[^:]+:)?
22 (?P<type>
23 video|audio
25 (?P<id>
26 [0-9a-f\-]{36}|\d+
28 '''
29 _GEO_BYPASS = False
30 _GEO_COUNTRIES = ['CH']
32 _ERRORS = {
33 'AGERATING12': 'To protect children under the age of 12, this video is only available between 8 p.m. and 6 a.m.',
34 'AGERATING18': 'To protect children under the age of 18, this video is only available between 11 p.m. and 5 a.m.',
35 # 'ENDDATE': 'For legal reasons, this video was only available for a specified period of time.',
36 'GEOBLOCK': 'For legal reasons, this video is only available in Switzerland.',
37 'LEGAL': 'The video cannot be transmitted for legal reasons.',
38 'STARTDATE': 'This video is not yet available. Please try again later.',
40 _DEFAULT_LANGUAGE_CODES = {
41 'srf': 'de',
42 'rts': 'fr',
43 'rsi': 'it',
44 'rtr': 'rm',
45 'swi': 'en',
48 def _get_tokenized_src(self, url, video_id, format_id):
49 token = self._download_json(
50 'http://tp.srgssr.ch/akahd/token?acl=*',
51 video_id, f'Downloading {format_id} token', fatal=False) or {}
52 auth_params = try_get(token, lambda x: x['token']['authparams'])
53 if auth_params:
54 url += ('?' if '?' not in url else '&') + auth_params
55 return url
57 def _get_media_data(self, bu, media_type, media_id):
58 query = {'onlyChapters': True} if media_type == 'video' else {}
59 full_media_data = self._download_json(
60 f'https://il.srgssr.ch/integrationlayer/2.0/{bu}/mediaComposition/{media_type}/{media_id}.json',
61 media_id, query=query)['chapterList']
62 try:
63 media_data = next(
64 x for x in full_media_data if x.get('id') == media_id)
65 except StopIteration:
66 raise ExtractorError('No media information found')
68 block_reason = media_data.get('blockReason')
69 if block_reason and block_reason in self._ERRORS:
70 message = self._ERRORS[block_reason]
71 if block_reason == 'GEOBLOCK':
72 self.raise_geo_restricted(
73 msg=message, countries=self._GEO_COUNTRIES)
74 raise ExtractorError(
75 f'{self.IE_NAME} said: {message}', expected=True)
77 return media_data
79 def _real_extract(self, url):
80 bu, media_type, media_id = self._match_valid_url(url).groups()
81 media_data = self._get_media_data(bu, media_type, media_id)
82 title = media_data['title']
84 formats = []
85 subtitles = {}
86 q = qualities(['SD', 'HD'])
87 for source in (media_data.get('resourceList') or []):
88 format_url = source.get('url')
89 if not format_url:
90 continue
91 protocol = source.get('protocol')
92 quality = source.get('quality')
93 format_id = join_nonempty(protocol, source.get('encoding'), quality)
95 if protocol in ('HDS', 'HLS'):
96 if source.get('tokenType') == 'AKAMAI':
97 format_url = self._get_tokenized_src(
98 format_url, media_id, format_id)
99 fmts, subs = self._extract_akamai_formats_and_subtitles(
100 format_url, media_id)
101 formats.extend(fmts)
102 subtitles = self._merge_subtitles(subtitles, subs)
103 elif protocol == 'HLS':
104 m3u8_fmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles(
105 format_url, media_id, 'mp4', 'm3u8_native',
106 m3u8_id=format_id, fatal=False)
107 formats.extend(m3u8_fmts)
108 subtitles = self._merge_subtitles(subtitles, m3u8_subs)
109 elif protocol in ('HTTP', 'HTTPS'):
110 formats.append({
111 'format_id': format_id,
112 'url': format_url,
113 'quality': q(quality),
116 # This is needed because for audio medias the podcast url is usually
117 # always included, even if is only an audio segment and not the
118 # whole episode.
119 if int_or_none(media_data.get('position')) == 0:
120 for p in ('S', 'H'):
121 podcast_url = media_data.get(f'podcast{p}dUrl')
122 if not podcast_url:
123 continue
124 quality = p + 'D'
125 formats.append({
126 'format_id': 'PODCAST-' + quality,
127 'url': podcast_url,
128 'quality': q(quality),
131 if media_type == 'video':
132 for sub in (media_data.get('subtitleList') or []):
133 sub_url = sub.get('url')
134 if not sub_url:
135 continue
136 lang = sub.get('locale') or self._DEFAULT_LANGUAGE_CODES[bu]
137 subtitles.setdefault(lang, []).append({
138 'url': sub_url,
141 return {
142 'id': media_id,
143 'title': title,
144 'description': media_data.get('description'),
145 'timestamp': parse_iso8601(media_data.get('date')),
146 'thumbnail': media_data.get('imageUrl'),
147 'duration': float_or_none(media_data.get('duration'), 1000),
148 'subtitles': subtitles,
149 'formats': formats,
153 class SRGSSRPlayIE(InfoExtractor):
154 IE_DESC = 'srf.ch, rts.ch, rsi.ch, rtr.ch and swissinfo.ch play sites'
155 _VALID_URL = r'''(?x)
156 https?://
157 (?:(?:www|play)\.)?
158 (?P<bu>srf|rts|rsi|rtr|swissinfo)\.ch/play/(?:tv|radio)/
160 [^/]+/(?P<type>video|audio)/[^?]+|
161 popup(?P<type_2>video|audio)player
163 \?.*?\b(?:id=|urn=urn:[^:]+:video:)(?P<id>[0-9a-f\-]{36}|\d+)
166 _TESTS = [{
167 'url': 'http://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?id=28e1a57d-5b76-4399-8ab3-9097f071e6c5',
168 'md5': '6db2226ba97f62ad42ce09783680046c',
169 'info_dict': {
170 'id': '28e1a57d-5b76-4399-8ab3-9097f071e6c5',
171 'ext': 'mp4',
172 'upload_date': '20130701',
173 'title': 'Snowden beantragt Asyl in Russland',
174 'timestamp': 1372708215,
175 'duration': 113.827,
176 'thumbnail': r're:^https?://.*1383719781\.png$',
178 'expected_warnings': ['Unable to download f4m manifest'],
179 }, {
180 'url': 'http://www.rtr.ch/play/radio/actualitad/audio/saira-tujetsch-tuttina-cuntinuar-cun-sedrun-muster-turissem?id=63cb0778-27f8-49af-9284-8c7a8c6d15fc',
181 'info_dict': {
182 'id': '63cb0778-27f8-49af-9284-8c7a8c6d15fc',
183 'ext': 'mp3',
184 'upload_date': '20151013',
185 'title': 'Saira: Tujetsch - tuttina cuntinuar cun Sedrun Mustér Turissem',
186 'timestamp': 1444709160,
187 'duration': 336.816,
189 'params': {
190 # rtmp download
191 'skip_download': True,
193 }, {
194 'url': 'http://www.rts.ch/play/tv/-/video/le-19h30?id=6348260',
195 'md5': '67a2a9ae4e8e62a68d0e9820cc9782df',
196 'info_dict': {
197 'id': '6348260',
198 'display_id': '6348260',
199 'ext': 'mp4',
200 'duration': 1796.76,
201 'title': 'Le 19h30',
202 'upload_date': '20141201',
203 'timestamp': 1417458600,
204 'thumbnail': r're:^https?://.*\.image',
206 'params': {
207 # m3u8 download
208 'skip_download': True,
210 }, {
211 'url': 'http://play.swissinfo.ch/play/tv/business/video/why-people-were-against-tax-reforms?id=42960270',
212 'info_dict': {
213 'id': '42960270',
214 'ext': 'mp4',
215 'title': 'Why people were against tax reforms',
216 'description': 'md5:7ac442c558e9630e947427469c4b824d',
217 'duration': 94.0,
218 'upload_date': '20170215',
219 'timestamp': 1487173560,
220 'thumbnail': r're:https?://www\.swissinfo\.ch/srgscalableimage/42961964',
221 'subtitles': 'count:9',
223 'params': {
224 'skip_download': True,
226 }, {
227 'url': 'https://www.srf.ch/play/tv/popupvideoplayer?id=c4dba0ca-e75b-43b2-a34f-f708a4932e01',
228 'only_matching': True,
229 }, {
230 'url': 'https://www.srf.ch/play/tv/10vor10/video/snowden-beantragt-asyl-in-russland?urn=urn:srf:video:28e1a57d-5b76-4399-8ab3-9097f071e6c5',
231 'only_matching': True,
232 }, {
233 'url': 'https://www.rts.ch/play/tv/19h30/video/le-19h30?urn=urn:rts:video:6348260',
234 'only_matching': True,
235 }, {
236 # audio segment, has podcastSdUrl of the full episode
237 'url': 'https://www.srf.ch/play/radio/popupaudioplayer?id=50b20dc8-f05b-4972-bf03-e438ff2833eb',
238 'only_matching': True,
241 def _real_extract(self, url):
242 mobj = self._match_valid_url(url)
243 bu = mobj.group('bu')
244 media_type = mobj.group('type') or mobj.group('type_2')
245 media_id = mobj.group('id')
246 return self.url_result(f'srgssr:{bu[:3]}:{media_type}:{media_id}', 'SRGSSR')