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