2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
7 from ..compat
import compat_str
19 class SVTBaseIE(InfoExtractor
):
20 _GEO_COUNTRIES
= ['SE']
22 def _extract_video(self
, video_info
, video_id
):
23 is_live
= dict_get(video_info
, ('live', 'simulcast'), default
=False)
24 m3u8_protocol
= 'm3u8' if is_live
else 'm3u8_native'
27 for vr
in video_info
['videoReferences']:
28 player_type
= vr
.get('playerType') or vr
.get('format')
30 ext
= determine_ext(vurl
)
32 fmts
, subs
= self
._extract
_m
3u8_formats
_and
_subtitles
(
34 ext
='mp4', entry_protocol
=m3u8_protocol
,
35 m3u8_id
=player_type
, fatal
=False)
37 self
._merge
_subtitles
(subs
, target
=subtitles
)
39 formats
.extend(self
._extract
_f
4m
_formats
(
40 vurl
+ '?hdcore=3.3.0', video_id
,
41 f4m_id
=player_type
, fatal
=False))
43 fmts
, subs
= self
._extract
_mpd
_formats
_and
_subtitles
(
44 vurl
, video_id
, mpd_id
=player_type
, fatal
=False)
46 self
._merge
_subtitles
(subs
, target
=subtitles
)
49 'format_id': player_type
,
52 rights
= try_get(video_info
, lambda x
: x
['rights'], dict) or {}
53 if not formats
and rights
.get('geoBlockedSweden'):
54 self
.raise_geo_restricted(
55 'This video is only available in Sweden',
56 countries
=self
._GEO
_COUNTRIES
, metadata_available
=True)
57 self
._sort
_formats
(formats
)
59 subtitle_references
= dict_get(video_info
, ('subtitles', 'subtitleReferences'))
60 if isinstance(subtitle_references
, list):
61 for sr
in subtitle_references
:
62 subtitle_url
= sr
.get('url')
63 subtitle_lang
= sr
.get('language', 'sv')
68 if determine_ext(subtitle_url
) == 'm3u8':
69 # XXX: no way of testing, is it ever hit?
71 subtitles
.setdefault(subtitle_lang
, []).append(sub
)
73 title
= video_info
.get('title')
75 series
= video_info
.get('programTitle')
76 season_number
= int_or_none(video_info
.get('season'))
77 episode
= video_info
.get('episodeTitle')
78 episode_number
= int_or_none(video_info
.get('episodeNumber'))
80 timestamp
= unified_timestamp(rights
.get('validFrom'))
81 duration
= int_or_none(dict_get(video_info
, ('materialLength', 'contentDuration')))
84 video_info
, ('inappropriateForChildren', 'blockedForChildren'),
85 skip_false_values
=False)
87 age_limit
= 18 if adult
else 0
93 'subtitles': subtitles
,
95 'timestamp': timestamp
,
96 'age_limit': age_limit
,
98 'season_number': season_number
,
100 'episode_number': episode_number
,
105 class SVTIE(SVTBaseIE
):
106 _VALID_URL
= r
'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
108 'url': 'http://www.svt.se/wd?widgetId=23991§ionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
109 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
113 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
120 def _extract_url(webpage
):
122 r
'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE
._VALID
_URL
, webpage
)
124 return mobj
.group('url')
126 def _real_extract(self
, url
):
127 mobj
= self
._match
_valid
_url
(url
)
128 widget_id
= mobj
.group('widget_id')
129 article_id
= mobj
.group('id')
131 info
= self
._download
_json
(
132 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id
, article_id
),
135 info_dict
= self
._extract
_video
(info
['video'], article_id
)
136 info_dict
['title'] = info
['context']['title']
140 class SVTPlayBaseIE(SVTBaseIE
):
141 _SVTPLAY_RE
= r
'root\s*\[\s*(["\'])_
*svtplay\
1\s
*\
]\s
*=\s
*(?P
<json
>{.+?}
)\s
*;\s
*\n'
144 class SVTPlayIE(SVTPlayBaseIE):
145 IE_DESC = 'SVT Play
and Öppet arkiv
'
146 _VALID_URL = r'''(?x)
150 https?://(?:www\.)?svt\.se/barnkanalen/barnplay/[^/]+/
152 (?P<svt_id>[^/?#&]+)|
153 https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp|kanaler)/(?P<id>[^/?#&]+)
154 (?:.*?(?:modalId|id)=(?P<modal_id>[\da-zA-Z-]+))?
158 'url
': 'https
://www
.svtplay
.se
/video
/30479064',
159 'md5
': '2382036fd6f8c994856c323fe51c426e
',
163 'title
': 'Designdrömmar i Stenungsund
',
164 'timestamp
': 1615770000,
165 'upload_date
': '20210315',
167 'thumbnail
': r're
:^https?
://(?
:.*[\
.-]jpg|www
.svtstatic
.se
/image
/.*)$
',
176 # skip for now due to download test asserts that segment is > 10000 bytes and svt uses
177 # init segments that are smaller
178 # AssertionError: Expected test_SVTPlay_jNwpV9P.mp4 to be at least 9.77KiB, but it's only
864.00B
179 'skip_download': True,
182 'url': 'https://www.svtplay.se/video/30479064/husdrommar/husdrommar-sasong-8-designdrommar-i-stenungsund?modalId=8zVbDPA',
183 'only_matching': True,
185 'url': 'https://www.svtplay.se/video/30684086/rapport/rapport-24-apr-18-00-7?id=e72gVpa',
186 'only_matching': True,
188 # geo restricted to Sweden
189 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
190 'only_matching': True,
192 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg',
193 'only_matching': True,
195 'url': 'https://www.svtplay.se/kanaler/svt1',
196 'only_matching': True,
198 'url': 'svt:1376446-003A',
199 'only_matching': True,
201 'url': 'svt:14278044',
202 'only_matching': True,
204 'url': 'https://www.svt.se/barnkanalen/barnplay/kar/eWv5MLX/',
205 'only_matching': True,
207 'url': 'svt:eWv5MLX',
208 'only_matching': True,
211 def _extract_by_video_id(self
, video_id
, webpage
=None):
212 data
= self
._download
_json
(
213 'https://api.svt.se/videoplayer-api/video/%s' % video_id
,
214 video_id
, headers
=self
.geo_verification_headers())
215 info_dict
= self
._extract
_video
(data
, video_id
)
216 if not info_dict
.get('title'):
217 title
= dict_get(info_dict
, ('episode', 'series'))
218 if not title
and webpage
:
220 r
'\s*\|\s*.+?$', '', self
._og
_search
_title
(webpage
))
223 info_dict
['title'] = title
226 def _real_extract(self
, url
):
227 mobj
= self
._match
_valid
_url
(url
)
228 video_id
= mobj
.group('id')
229 svt_id
= mobj
.group('svt_id') or mobj
.group('modal_id')
232 return self
._extract
_by
_video
_id
(svt_id
)
234 webpage
= self
._download
_webpage
(url
, video_id
)
236 data
= self
._parse
_json
(
238 self
._SVTPLAY
_RE
, webpage
, 'embedded data', default
='{}',
240 video_id
, fatal
=False)
242 thumbnail
= self
._og
_search
_thumbnail
(webpage
)
245 video_info
= try_get(
246 data
, lambda x
: x
['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'],
249 info_dict
= self
._extract
_video
(video_info
, video_id
)
251 'title': data
['context']['dispatcher']['stores']['MetaStore']['title'],
252 'thumbnail': thumbnail
,
257 data
, lambda x
: x
['statistics']['dataLake']['content']['id'],
261 svt_id
= self
._search
_regex
(
262 (r
'<video[^>]+data-video-id=["\']([\da
-zA
-Z
-]+)',
263 r'<[^
>]+\bdata
-rt
=["\']top-area-play-button["\'][^
>]+\bhref
=["\'][^"\']*video
/%s/[^
"\']*\b(?:modalId|id)=([\da-zA-Z-]+)' % re.escape(video_id),
264 r'["\']videoSvtId
["\']\s*:\s*["\']([\da
-zA
-Z
-]+)',
265 r'["\']videoSvtId\\?["\']\s
*:\s
*\\?
["\']([\da-zA-Z-]+)',
266 r'"content
"\s*:\s*{.*?"id"\s*:\s*"([\da
-zA
-Z
-]+)"',
267 r'["\']svtId
["\']\s*:\s*["\']([\da
-zA
-Z
-]+)',
268 r'["\']svtId\\?["\']\s
*:\s
*\\?
["\']([\da-zA-Z-]+)'),
271 info_dict = self._extract_by_video_id(svt_id, webpage)
272 info_dict['thumbnail'] = thumbnail
277 class SVTSeriesIE(SVTPlayBaseIE):
278 _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)(?:.+?\btab=(?P<season_slug>[^&#]+))?'
280 'url': 'https://www.svtplay.se/rederiet',
284 'description': 'md5:d9fdfff17f5d8f73468176ecd2836039',
286 'playlist_mincount': 318,
288 'url': 'https://www.svtplay.se/rederiet?tab=season-2-14445680',
290 'id': 'season-2-14445680',
291 'title': 'Rederiet - Säsong 2',
292 'description': 'md5:d9fdfff17f5d8f73468176ecd2836039',
294 'playlist_mincount': 12,
298 def suitable(cls, url):
299 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url)
301 def _real_extract(self, url):
302 series_slug, season_id = self._match_valid_url(url).groups()
304 series = self._download_json(
305 'https://api.svt.se/contento/graphql', series_slug,
306 'Downloading series page', query={
308 listablesBySlug(slugs: ["%s"]) {
309 associatedContent(include: [productionPeriod, season]) {
326 })['data']['listablesBySlug'][0]
331 for season in series['associatedContent']:
332 if not isinstance(season, dict):
335 if season.get('id') != season_id:
337 season_name = season.get('name')
338 items = season.get('items')
339 if not isinstance(items, list):
342 video = item.get('item') or {}
343 content_id = video.get('videoSvtId')
344 if not content_id or not isinstance(content_id, compat_str):
346 entries.append(self.url_result(
347 'svt:' + content_id, SVTPlayIE.ie_key(), content_id))
349 title = series.get('name')
350 season_name = season_name or season_id
352 if title and season_name:
353 title = '%s - %s' % (title, season_name)
357 return self.playlist_result(
358 entries, season_id or series.get('id'), title,
359 dict_get(series, ('longDescription', 'shortDescription')))
362 class SVTPageIE(InfoExtractor):
363 _VALID_URL = r'https?://(?:www\.)?svt\.se/(?P<path>(?:[^/]+/)*(?P<id>[^/?&#]+))'
365 'url': 'https://www.svt.se/sport/ishockey/bakom-masken-lehners-kamp-mot-mental-ohalsa',
368 'title': 'Bakom masken – Lehners kamp mot mental ohälsa',
372 'url': 'https://www.svt.se/nyheter/utrikes/svenska-andrea-ar-en-mil-fran-branderna-i-kalifornien',
375 'title': 'Svenska Andrea redo att fly sitt hem i Kalifornien',
380 'url': 'http://www.svt.se/sport/ishockey/jagr-tacklar-giroux-under-intervjun',
384 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
389 'url': 'https://www.svt.se/nyheter/lokalt/vast/svt-testar-tar-nagon-upp-skrapet-1',
390 'only_matching': True,
392 'url': 'https://www.svt.se/vader/manadskronikor/maj2018',
393 'only_matching': True,
397 def suitable(cls, url):
398 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTPageIE, cls).suitable(url)
400 def _real_extract(self, url):
401 path, display_id = self._match_valid_url(url).groups()
403 article = self._download_json(
404 'https://api.svt.se/nss-api/page/' + path, display_id,
405 query={'q': 'articles'})['articles']['content'][0]
409 def _process_content(content):
410 if content.get('_type') in ('VIDEOCLIP', 'VIDEOEPISODE'):
411 video_id = compat_str(content['image']['svtId'])
412 entries.append(self.url_result(
413 'svt:' + video_id, SVTPlayIE.ie_key(), video_id))
415 for media in article.get('media', []):
416 _process_content(media)
418 for obj in article.get('structuredBody', []):
419 _process_content(obj.get('content') or {})
421 return self.playlist_result(
422 entries, str_or_none(article.get('id')),
423 strip_or_none(article.get('title')))