]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/svt.py
[ie/motherless] Support uploader playlists (#8994)
[yt-dlp.git] / yt_dlp / extractor / svt.py
CommitLineData
2301b5c1 1import json
df5ae3eb
S
2import re
3
1309b396 4from .common import InfoExtractor
8e4d3f83 5from ..compat import compat_str
1309b396
PH
6from ..utils import (
7 determine_ext,
e4f90ea0 8 dict_get,
23bdae09 9 int_or_none,
43e79947 10 str_or_none,
7b393f9c 11 strip_or_none,
2301b5c1 12 traverse_obj,
23bdae09 13 try_get,
2301b5c1 14 unified_timestamp,
1309b396
PH
15)
16
17
79998cd5 18class SVTBaseIE(InfoExtractor):
4248dad9 19 _GEO_COUNTRIES = ['SE']
6d4c2597 20
23bdae09 21 def _extract_video(self, video_info, video_id):
488ff2dd 22 is_live = dict_get(video_info, ('live', 'simulcast'), default=False)
23 m3u8_protocol = 'm3u8' if is_live else 'm3u8_native'
1309b396 24 formats = []
3f047fc4 25 subtitles = {}
1309b396 26 for vr in video_info['videoReferences']:
21d21b0c 27 player_type = vr.get('playerType') or vr.get('format')
1309b396 28 vurl = vr['url']
df5ae3eb
S
29 ext = determine_ext(vurl)
30 if ext == 'm3u8':
3f047fc4 31 fmts, subs = self._extract_m3u8_formats_and_subtitles(
1309b396 32 vurl, video_id,
488ff2dd 33 ext='mp4', entry_protocol=m3u8_protocol,
3f047fc4
F
34 m3u8_id=player_type, fatal=False)
35 formats.extend(fmts)
36 self._merge_subtitles(subs, target=subtitles)
df5ae3eb
S
37 elif ext == 'f4m':
38 formats.extend(self._extract_f4m_formats(
39 vurl + '?hdcore=3.3.0', video_id,
edfd9351 40 f4m_id=player_type, fatal=False))
41 elif ext == 'mpd':
3f047fc4
F
42 fmts, subs = self._extract_mpd_formats_and_subtitles(
43 vurl, video_id, mpd_id=player_type, fatal=False)
44 formats.extend(fmts)
45 self._merge_subtitles(subs, target=subtitles)
1309b396
PH
46 else:
47 formats.append({
edfd9351 48 'format_id': player_type,
1309b396
PH
49 'url': vurl,
50 })
a0566bbf 51 rights = try_get(video_info, lambda x: x['rights'], dict) or {}
52 if not formats and rights.get('geoBlockedSweden'):
04d906ea 53 self.raise_geo_restricted(
4248dad9 54 'This video is only available in Sweden',
b7da73eb 55 countries=self._GEO_COUNTRIES, metadata_available=True)
1309b396 56
e4f90ea0 57 subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences'))
594c4d79
S
58 if isinstance(subtitle_references, list):
59 for sr in subtitle_references:
60 subtitle_url = sr.get('url')
e4f90ea0 61 subtitle_lang = sr.get('language', 'sv')
594c4d79 62 if subtitle_url:
3f047fc4
F
63 sub = {
64 'url': subtitle_url,
65 }
e4f90ea0 66 if determine_ext(subtitle_url) == 'm3u8':
3f047fc4
F
67 # XXX: no way of testing, is it ever hit?
68 sub['ext'] = 'vtt'
69 subtitles.setdefault(subtitle_lang, []).append(sub)
1f16b958 70
23bdae09
S
71 title = video_info.get('title')
72
73 series = video_info.get('programTitle')
74 season_number = int_or_none(video_info.get('season'))
75 episode = video_info.get('episodeTitle')
76 episode_number = int_or_none(video_info.get('episodeNumber'))
77
a0566bbf 78 timestamp = unified_timestamp(rights.get('validFrom'))
23bdae09
S
79 duration = int_or_none(dict_get(video_info, ('materialLength', 'contentDuration')))
80 age_limit = None
81 adult = dict_get(
82 video_info, ('inappropriateForChildren', 'blockedForChildren'),
83 skip_false_values=False)
84 if adult is not None:
85 age_limit = 18 if adult else 0
1309b396
PH
86
87 return {
88 'id': video_id,
23bdae09 89 'title': title,
1309b396 90 'formats': formats,
1f16b958 91 'subtitles': subtitles,
1309b396 92 'duration': duration,
a0566bbf 93 'timestamp': timestamp,
df5ae3eb 94 'age_limit': age_limit,
23bdae09
S
95 'series': series,
96 'season_number': season_number,
97 'episode': episode,
98 'episode_number': episode_number,
488ff2dd 99 'is_live': is_live,
1309b396 100 }
79998cd5
S
101
102
103class SVTIE(SVTBaseIE):
104 _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
bfd973ec 105 _EMBED_REGEX = [r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % _VALID_URL]
79998cd5
S
106 _TEST = {
107 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
e4f90ea0 108 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
79998cd5
S
109 'info_dict': {
110 'id': '2900353',
e4f90ea0
YCH
111 'ext': 'mp4',
112 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
79998cd5
S
113 'duration': 27,
114 'age_limit': 0,
115 },
116 }
117
118 def _real_extract(self, url):
5ad28e7f 119 mobj = self._match_valid_url(url)
79998cd5
S
120 widget_id = mobj.group('widget_id')
121 article_id = mobj.group('id')
e4f90ea0
YCH
122
123 info = self._download_json(
79998cd5
S
124 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
125 article_id)
126
23bdae09 127 info_dict = self._extract_video(info['video'], article_id)
e4f90ea0
YCH
128 info_dict['title'] = info['context']['title']
129 return info_dict
130
79998cd5 131
1236ac6b
S
132class SVTPlayBaseIE(SVTBaseIE):
133 _SVTPLAY_RE = r'root\s*\[\s*(["\'])_*svtplay\1\s*\]\s*=\s*(?P<json>{.+?})\s*;\s*\n'
134
135
136class SVTPlayIE(SVTPlayBaseIE):
79998cd5 137 IE_DESC = 'SVT Play and Öppet arkiv'
7b393f9c
S
138 _VALID_URL = r'''(?x)
139 (?:
a0566bbf 140 (?:
141 svt:|
142 https?://(?:www\.)?svt\.se/barnkanalen/barnplay/[^/]+/
143 )
144 (?P<svt_id>[^/?#&]+)|
7b393f9c 145 https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp|kanaler)/(?P<id>[^/?#&]+)
41d1cca3 146 (?:.*?(?:modalId|id)=(?P<modal_id>[\da-zA-Z-]+))?
7b393f9c
S
147 )
148 '''
23bdae09 149 _TESTS = [{
421a4595 150 'url': 'https://www.svtplay.se/video/30479064',
a0566bbf 151 'md5': '2382036fd6f8c994856c323fe51c426e',
79998cd5 152 'info_dict': {
421a4595 153 'id': '8zVbDPA',
594c4d79 154 'ext': 'mp4',
421a4595 155 'title': 'Designdrömmar i Stenungsund',
156 'timestamp': 1615770000,
157 'upload_date': '20210315',
158 'duration': 3519,
a0566bbf 159 'thumbnail': r're:^https?://(?:.*[\.-]jpg|www.svtstatic.se/image/.*)$',
79998cd5 160 'age_limit': 0,
594c4d79
S
161 'subtitles': {
162 'sv': [{
a0566bbf 163 'ext': 'vtt',
594c4d79
S
164 }]
165 },
79998cd5 166 },
a0566bbf 167 'params': {
2301b5c1
MW
168 'skip_download': 'm3u8',
169 },
170 'skip': 'Episode is no longer available',
171 }, {
172 'url': 'https://www.svtplay.se/video/emBxBQj',
173 'md5': '2382036fd6f8c994856c323fe51c426e',
174 'info_dict': {
175 'id': 'eyBd9aj',
176 'ext': 'mp4',
177 'title': '1. Farlig kryssning',
178 'timestamp': 1491019200,
179 'upload_date': '20170401',
180 'duration': 2566,
181 'thumbnail': r're:^https?://(?:.*[\.-]jpg|www.svtstatic.se/image/.*)$',
182 'age_limit': 0,
183 'episode': '1. Farlig kryssning',
184 'series': 'Rederiet',
185 'subtitles': {
186 'sv': 'count:3'
187 },
188 },
189 'params': {
190 'skip_download': 'm3u8',
191 },
192 }, {
193 'url': 'https://www.svtplay.se/video/jz2rYz7/anders-hansen-moter/james-fallon?info=visa',
194 'info_dict': {
195 'id': 'jvXAGVb',
196 'ext': 'mp4',
197 'title': 'James Fallon',
198 'timestamp': 1673917200,
199 'upload_date': '20230117',
200 'duration': 1081,
201 'thumbnail': r're:^https?://(?:.*[\.-]jpg|www.svtstatic.se/image/.*)$',
202 'age_limit': 0,
203 'episode': 'James Fallon',
204 'series': 'Anders Hansen möter...',
205 },
206 'params': {
207 'skip_download': 'dash',
a0566bbf 208 },
421a4595 209 }, {
210 'url': 'https://www.svtplay.se/video/30479064/husdrommar/husdrommar-sasong-8-designdrommar-i-stenungsund?modalId=8zVbDPA',
211 'only_matching': True,
41d1cca3 212 }, {
213 'url': 'https://www.svtplay.se/video/30684086/rapport/rapport-24-apr-18-00-7?id=e72gVpa',
214 'only_matching': True,
23bdae09
S
215 }, {
216 # geo restricted to Sweden
217 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
218 'only_matching': True,
3b34ab53
S
219 }, {
220 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg',
221 'only_matching': True,
488ff2dd 222 }, {
223 'url': 'https://www.svtplay.se/kanaler/svt1',
224 'only_matching': True,
7b393f9c
S
225 }, {
226 'url': 'svt:1376446-003A',
227 'only_matching': True,
228 }, {
229 'url': 'svt:14278044',
230 'only_matching': True,
a0566bbf 231 }, {
232 'url': 'https://www.svt.se/barnkanalen/barnplay/kar/eWv5MLX/',
233 'only_matching': True,
234 }, {
235 'url': 'svt:eWv5MLX',
236 'only_matching': True,
23bdae09 237 }]
e4f90ea0 238
7b393f9c
S
239 def _extract_by_video_id(self, video_id, webpage=None):
240 data = self._download_json(
e6a25fea 241 'https://api.svt.se/videoplayer-api/video/%s' % video_id,
7b393f9c
S
242 video_id, headers=self.geo_verification_headers())
243 info_dict = self._extract_video(data, video_id)
244 if not info_dict.get('title'):
245 title = dict_get(info_dict, ('episode', 'series'))
246 if not title and webpage:
247 title = re.sub(
248 r'\s*\|\s*.+?$', '', self._og_search_title(webpage))
249 if not title:
250 title = video_id
251 info_dict['title'] = title
7b393f9c
S
252 return info_dict
253
79998cd5 254 def _real_extract(self, url):
5ad28e7f 255 mobj = self._match_valid_url(url)
421a4595 256 video_id = mobj.group('id')
257 svt_id = mobj.group('svt_id') or mobj.group('modal_id')
7b393f9c
S
258
259 if svt_id:
260 return self._extract_by_video_id(svt_id)
e4f90ea0
YCH
261
262 webpage = self._download_webpage(url, video_id)
263
23bdae09
S
264 data = self._parse_json(
265 self._search_regex(
1236ac6b
S
266 self._SVTPLAY_RE, webpage, 'embedded data', default='{}',
267 group='json'),
23bdae09 268 video_id, fatal=False)
e4f90ea0
YCH
269
270 thumbnail = self._og_search_thumbnail(webpage)
271
23bdae09
S
272 if data:
273 video_info = try_get(
274 data, lambda x: x['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'],
275 dict)
276 if video_info:
277 info_dict = self._extract_video(video_info, video_id)
278 info_dict.update({
279 'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
280 'thumbnail': thumbnail,
281 })
282 return info_dict
283
5ed05f26
S
284 svt_id = try_get(
285 data, lambda x: x['statistics']['dataLake']['content']['id'],
286 compat_str)
287
2301b5c1
MW
288 if not svt_id:
289 nextjs_data = self._search_nextjs_data(webpage, video_id, fatal=False)
290 svt_id = traverse_obj(nextjs_data, (
291 'props', 'urqlState', ..., 'data', {json.loads}, 'detailsPageByPath',
292 'video', 'svtId', {str}), get_all=False)
293
5ed05f26
S
294 if not svt_id:
295 svt_id = self._search_regex(
296 (r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
2301b5c1 297 r'<[^>]+\bdata-rt=["\']top-area-play-button["\'][^>]+\bhref=["\'][^"\']*video/[\w-]+/[^"\']*\b(?:modalId|id)=([\w-]+)'),
5ed05f26 298 webpage, 'video id')
23bdae09 299
a0566bbf 300 info_dict = self._extract_by_video_id(svt_id, webpage)
301 info_dict['thumbnail'] = thumbnail
302
303 return info_dict
fd97fa7b
MW
304
305
1236ac6b 306class SVTSeriesIE(SVTPlayBaseIE):
8e4d3f83 307 _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)(?:.+?\btab=(?P<season_slug>[^&#]+))?'
fd97fa7b
MW
308 _TESTS = [{
309 'url': 'https://www.svtplay.se/rederiet',
310 'info_dict': {
8e4d3f83 311 'id': '14445680',
fd97fa7b 312 'title': 'Rederiet',
8e4d3f83 313 'description': 'md5:d9fdfff17f5d8f73468176ecd2836039',
fd97fa7b
MW
314 },
315 'playlist_mincount': 318,
df146eb2 316 }, {
8e4d3f83 317 'url': 'https://www.svtplay.se/rederiet?tab=season-2-14445680',
df146eb2 318 'info_dict': {
8e4d3f83 319 'id': 'season-2-14445680',
df146eb2 320 'title': 'Rederiet - Säsong 2',
8e4d3f83 321 'description': 'md5:d9fdfff17f5d8f73468176ecd2836039',
df146eb2 322 },
8e4d3f83 323 'playlist_mincount': 12,
fd97fa7b
MW
324 }]
325
326 @classmethod
327 def suitable(cls, url):
b71bb3ba 328 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url)
fd97fa7b
MW
329
330 def _real_extract(self, url):
5ad28e7f 331 series_slug, season_id = self._match_valid_url(url).groups()
8e4d3f83
RA
332
333 series = self._download_json(
334 'https://api.svt.se/contento/graphql', series_slug,
335 'Downloading series page', query={
336 'query': '''{
337 listablesBySlug(slugs: ["%s"]) {
338 associatedContent(include: [productionPeriod, season]) {
339 items {
340 item {
341 ... on Episode {
342 videoSvtId
343 }
344 }
345 }
346 id
347 name
348 }
349 id
350 longDescription
351 name
352 shortDescription
353 }
354}''' % series_slug,
355 })['data']['listablesBySlug'][0]
df146eb2
S
356
357 season_name = None
fd97fa7b
MW
358
359 entries = []
8e4d3f83 360 for season in series['associatedContent']:
df146eb2
S
361 if not isinstance(season, dict):
362 continue
8e4d3f83
RA
363 if season_id:
364 if season.get('id') != season_id:
df146eb2
S
365 continue
366 season_name = season.get('name')
8e4d3f83
RA
367 items = season.get('items')
368 if not isinstance(items, list):
fd97fa7b 369 continue
8e4d3f83
RA
370 for item in items:
371 video = item.get('item') or {}
372 content_id = video.get('videoSvtId')
373 if not content_id or not isinstance(content_id, compat_str):
fd97fa7b 374 continue
8e4d3f83
RA
375 entries.append(self.url_result(
376 'svt:' + content_id, SVTPlayIE.ie_key(), content_id))
b71bb3ba 377
8e4d3f83
RA
378 title = series.get('name')
379 season_name = season_name or season_id
df146eb2
S
380
381 if title and season_name:
382 title = '%s - %s' % (title, season_name)
8e4d3f83
RA
383 elif season_id:
384 title = season_id
df146eb2 385
fd97fa7b 386 return self.playlist_result(
8e4d3f83
RA
387 entries, season_id or series.get('id'), title,
388 dict_get(series, ('longDescription', 'shortDescription')))
7b393f9c
S
389
390
391class SVTPageIE(InfoExtractor):
43e79947 392 _VALID_URL = r'https?://(?:www\.)?svt\.se/(?P<path>(?:[^/]+/)*(?P<id>[^/?&#]+))'
7b393f9c 393 _TESTS = [{
43e79947 394 'url': 'https://www.svt.se/sport/ishockey/bakom-masken-lehners-kamp-mot-mental-ohalsa',
7b393f9c 395 'info_dict': {
43e79947
RA
396 'id': '25298267',
397 'title': 'Bakom masken – Lehners kamp mot mental ohälsa',
7b393f9c 398 },
43e79947 399 'playlist_count': 4,
7b393f9c 400 }, {
43e79947 401 'url': 'https://www.svt.se/nyheter/utrikes/svenska-andrea-ar-en-mil-fran-branderna-i-kalifornien',
7b393f9c 402 'info_dict': {
43e79947
RA
403 'id': '24243746',
404 'title': 'Svenska Andrea redo att fly sitt hem i Kalifornien',
7b393f9c 405 },
43e79947 406 'playlist_count': 2,
7b393f9c
S
407 }, {
408 # only programTitle
409 'url': 'http://www.svt.se/sport/ishockey/jagr-tacklar-giroux-under-intervjun',
410 'info_dict': {
43e79947 411 'id': '8439V2K',
7b393f9c
S
412 'ext': 'mp4',
413 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
414 'duration': 27,
415 'age_limit': 0,
416 },
417 }, {
418 'url': 'https://www.svt.se/nyheter/lokalt/vast/svt-testar-tar-nagon-upp-skrapet-1',
419 'only_matching': True,
420 }, {
421 'url': 'https://www.svt.se/vader/manadskronikor/maj2018',
422 'only_matching': True,
423 }]
424
425 @classmethod
426 def suitable(cls, url):
a0566bbf 427 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTPageIE, cls).suitable(url)
7b393f9c
S
428
429 def _real_extract(self, url):
5ad28e7f 430 path, display_id = self._match_valid_url(url).groups()
7b393f9c 431
43e79947
RA
432 article = self._download_json(
433 'https://api.svt.se/nss-api/page/' + path, display_id,
434 query={'q': 'articles'})['articles']['content'][0]
7b393f9c 435
43e79947 436 entries = []
7b393f9c 437
43e79947
RA
438 def _process_content(content):
439 if content.get('_type') in ('VIDEOCLIP', 'VIDEOEPISODE'):
440 video_id = compat_str(content['image']['svtId'])
441 entries.append(self.url_result(
442 'svt:' + video_id, SVTPlayIE.ie_key(), video_id))
7b393f9c 443
43e79947
RA
444 for media in article.get('media', []):
445 _process_content(media)
446
447 for obj in article.get('structuredBody', []):
448 _process_content(obj.get('content') or {})
449
450 return self.playlist_result(
451 entries, str_or_none(article.get('id')),
452 strip_or_none(article.get('title')))