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