-# coding: utf-8
-from __future__ import unicode_literals
-
+import json
import re
from .common import InfoExtractor
determine_ext,
dict_get,
int_or_none,
- unified_timestamp,
- str_or_none,
- strip_or_none,
+ traverse_obj,
try_get,
+ unified_timestamp,
)
is_live = dict_get(video_info, ('live', 'simulcast'), default=False)
m3u8_protocol = 'm3u8' if is_live else 'm3u8_native'
formats = []
+ subtitles = {}
for vr in video_info['videoReferences']:
player_type = vr.get('playerType') or vr.get('format')
vurl = vr['url']
ext = determine_ext(vurl)
if ext == 'm3u8':
- formats.extend(self._extract_m3u8_formats(
+ fmts, subs = self._extract_m3u8_formats_and_subtitles(
vurl, video_id,
ext='mp4', entry_protocol=m3u8_protocol,
- m3u8_id=player_type, fatal=False))
+ m3u8_id=player_type, fatal=False)
+ formats.extend(fmts)
+ self._merge_subtitles(subs, target=subtitles)
elif ext == 'f4m':
formats.extend(self._extract_f4m_formats(
vurl + '?hdcore=3.3.0', video_id,
f4m_id=player_type, fatal=False))
elif ext == 'mpd':
- if player_type == 'dashhbbtv':
- formats.extend(self._extract_mpd_formats(
- vurl, video_id, mpd_id=player_type, fatal=False))
+ fmts, subs = self._extract_mpd_formats_and_subtitles(
+ vurl, video_id, mpd_id=player_type, fatal=False)
+ formats.extend(fmts)
+ self._merge_subtitles(subs, target=subtitles)
else:
formats.append({
'format_id': player_type,
self.raise_geo_restricted(
'This video is only available in Sweden',
countries=self._GEO_COUNTRIES, metadata_available=True)
- self._sort_formats(formats)
- subtitles = {}
subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences'))
if isinstance(subtitle_references, list):
for sr in subtitle_references:
subtitle_url = sr.get('url')
subtitle_lang = sr.get('language', 'sv')
if subtitle_url:
+ sub = {
+ 'url': subtitle_url,
+ }
if determine_ext(subtitle_url) == 'm3u8':
- # TODO(yan12125): handle WebVTT in m3u8 manifests
- continue
-
- subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url})
+ # XXX: no way of testing, is it ever hit?
+ sub['ext'] = 'vtt'
+ subtitles.setdefault(subtitle_lang, []).append(sub)
title = video_info.get('title')
class SVTIE(SVTBaseIE):
_VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
+ _EMBED_REGEX = [r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % _VALID_URL]
_TEST = {
'url': 'http://www.svt.se/wd?widgetId=23991§ionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
},
}
- @staticmethod
- def _extract_url(webpage):
- mobj = re.search(
- r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
- if mobj:
- return mobj.group('url')
-
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_valid_url(url)
widget_id = mobj.group('widget_id')
article_id = mobj.group('id')
)
(?P<svt_id>[^/?#&]+)|
https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp|kanaler)/(?P<id>[^/?#&]+)
- (?:.*?modalId=(?P<modal_id>[\da-zA-Z-]+))?
+ (?:.*?(?:modalId|id)=(?P<modal_id>[\da-zA-Z-]+))?
)
'''
_TESTS = [{
},
},
'params': {
- 'format': 'bestvideo',
- # skip for now due to download test asserts that segment is > 10000 bytes and svt uses
- # init segments that are smaller
- # AssertionError: Expected test_SVTPlay_jNwpV9P.mp4 to be at least 9.77KiB, but it's only 864.00B
- 'skip_download': True,
+ 'skip_download': 'm3u8',
+ },
+ 'skip': 'Episode is no longer available',
+ }, {
+ 'url': 'https://www.svtplay.se/video/emBxBQj',
+ 'md5': '2382036fd6f8c994856c323fe51c426e',
+ 'info_dict': {
+ 'id': 'eyBd9aj',
+ 'ext': 'mp4',
+ 'title': '1. Farlig kryssning',
+ 'timestamp': 1491019200,
+ 'upload_date': '20170401',
+ 'duration': 2566,
+ 'thumbnail': r're:^https?://(?:.*[\.-]jpg|www.svtstatic.se/image/.*)$',
+ 'age_limit': 0,
+ 'episode': '1. Farlig kryssning',
+ 'series': 'Rederiet',
+ 'subtitles': {
+ 'sv': 'count:3'
+ },
+ },
+ 'params': {
+ 'skip_download': 'm3u8',
+ },
+ }, {
+ 'url': 'https://www.svtplay.se/video/jz2rYz7/anders-hansen-moter/james-fallon?info=visa',
+ 'info_dict': {
+ 'id': 'jvXAGVb',
+ 'ext': 'mp4',
+ 'title': 'James Fallon',
+ 'timestamp': 1673917200,
+ 'upload_date': '20230117',
+ 'duration': 1081,
+ 'thumbnail': r're:^https?://(?:.*[\.-]jpg|www.svtstatic.se/image/.*)$',
+ 'age_limit': 0,
+ 'episode': 'James Fallon',
+ 'series': 'Anders Hansen möter...',
+ },
+ 'params': {
+ 'skip_download': 'dash',
},
}, {
'url': 'https://www.svtplay.se/video/30479064/husdrommar/husdrommar-sasong-8-designdrommar-i-stenungsund?modalId=8zVbDPA',
'only_matching': True,
+ }, {
+ 'url': 'https://www.svtplay.se/video/30684086/rapport/rapport-24-apr-18-00-7?id=e72gVpa',
+ 'only_matching': True,
}, {
# geo restricted to Sweden
'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
'only_matching': True,
}]
- def _adjust_title(self, info):
- if info['is_live']:
- info['title'] = self._live_title(info['title'])
-
def _extract_by_video_id(self, video_id, webpage=None):
data = self._download_json(
'https://api.svt.se/videoplayer-api/video/%s' % video_id,
if not title:
title = video_id
info_dict['title'] = title
- self._adjust_title(info_dict)
return info_dict
def _real_extract(self, url):
- mobj = re.match(self._VALID_URL, url)
+ mobj = self._match_valid_url(url)
video_id = mobj.group('id')
svt_id = mobj.group('svt_id') or mobj.group('modal_id')
'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
'thumbnail': thumbnail,
})
- self._adjust_title(info_dict)
return info_dict
svt_id = try_get(
data, lambda x: x['statistics']['dataLake']['content']['id'],
compat_str)
+ if not svt_id:
+ nextjs_data = self._search_nextjs_data(webpage, video_id, fatal=False)
+ svt_id = traverse_obj(nextjs_data, (
+ 'props', 'urqlState', ..., 'data', {json.loads}, 'detailsPageByPath',
+ 'video', 'svtId', {str}), get_all=False)
+
if not svt_id:
svt_id = self._search_regex(
(r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
- r'<[^>]+\bdata-rt=["\']top-area-play-button["\'][^>]+\bhref=["\'][^"\']*video/%s/[^"\']*\bmodalId=([\da-zA-Z-]+)' % re.escape(video_id),
- r'["\']videoSvtId["\']\s*:\s*["\']([\da-zA-Z-]+)',
- r'["\']videoSvtId\\?["\']\s*:\s*\\?["\']([\da-zA-Z-]+)',
- r'"content"\s*:\s*{.*?"id"\s*:\s*"([\da-zA-Z-]+)"',
- r'["\']svtId["\']\s*:\s*["\']([\da-zA-Z-]+)',
- r'["\']svtId\\?["\']\s*:\s*\\?["\']([\da-zA-Z-]+)'),
+ r'<[^>]+\bdata-rt=["\']top-area-play-button["\'][^>]+\bhref=["\'][^"\']*video/[\w-]+/[^"\']*\b(?:modalId|id)=([\w-]+)'),
webpage, 'video id')
info_dict = self._extract_by_video_id(svt_id, webpage)
return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url)
def _real_extract(self, url):
- series_slug, season_id = re.match(self._VALID_URL, url).groups()
+ series_slug, season_id = self._match_valid_url(url).groups()
series = self._download_json(
'https://api.svt.se/contento/graphql', series_slug,
dict_get(series, ('longDescription', 'shortDescription')))
-class SVTPageIE(InfoExtractor):
- _VALID_URL = r'https?://(?:www\.)?svt\.se/(?P<path>(?:[^/]+/)*(?P<id>[^/?&#]+))'
+class SVTPageIE(SVTBaseIE):
+ _VALID_URL = r'https?://(?:www\.)?svt\.se/(?:[^/?#]+/)*(?P<id>[^/?&#]+)'
_TESTS = [{
+ 'url': 'https://www.svt.se/nyheter/lokalt/skane/viktor-18-forlorade-armar-och-ben-i-sepsis-vill-ateruppta-karaten-och-bli-svetsare',
+ 'info_dict': {
+ 'title': 'Viktor, 18, förlorade armar och ben i sepsis – vill återuppta karaten och bli svetsare',
+ 'id': 'viktor-18-forlorade-armar-och-ben-i-sepsis-vill-ateruppta-karaten-och-bli-svetsare',
+ },
+ 'playlist_count': 2,
+ }, {
+ 'url': 'https://www.svt.se/nyheter/lokalt/skane/forsvarsmakten-om-trafikkaoset-pa-e22-kunde-inte-varit-dar-snabbare',
+ 'info_dict': {
+ 'id': 'jXvk42E',
+ 'title': 'Försvarsmakten om trafikkaoset på E22: Kunde inte varit där snabbare',
+ 'ext': 'mp4',
+ "duration": 80,
+ 'age_limit': 0,
+ 'timestamp': 1704370009,
+ 'episode': 'Försvarsmakten om trafikkaoset på E22: Kunde inte varit där snabbare',
+ 'series': 'Lokala Nyheter Skåne',
+ 'upload_date': '20240104'
+ },
+ 'params': {
+ 'skip_download': True,
+ }
+ }, {
+ 'url': 'https://www.svt.se/nyheter/svtforum/2023-tungt-ar-for-svensk-media',
+ 'info_dict': {
+ 'title': '2023 tungt år för svensk media',
+ 'id': 'ewqAZv4',
+ 'ext': 'mp4',
+ "duration": 3074,
+ 'age_limit': 0,
+ 'series': '',
+ 'timestamp': 1702980479,
+ 'upload_date': '20231219',
+ 'episode': 'Mediestudier'
+ },
+ 'params': {
+ 'skip_download': True,
+ }
+ }, {
'url': 'https://www.svt.se/sport/ishockey/bakom-masken-lehners-kamp-mot-mental-ohalsa',
'info_dict': {
'id': '25298267',
'title': 'Bakom masken – Lehners kamp mot mental ohälsa',
},
'playlist_count': 4,
+ 'skip': 'Video is gone'
}, {
'url': 'https://www.svt.se/nyheter/utrikes/svenska-andrea-ar-en-mil-fran-branderna-i-kalifornien',
'info_dict': {
'title': 'Svenska Andrea redo att fly sitt hem i Kalifornien',
},
'playlist_count': 2,
+ 'skip': 'Video is gone'
}, {
# only programTitle
'url': 'http://www.svt.se/sport/ishockey/jagr-tacklar-giroux-under-intervjun',
'duration': 27,
'age_limit': 0,
},
+ 'skip': 'Video is gone'
}, {
'url': 'https://www.svt.se/nyheter/lokalt/vast/svt-testar-tar-nagon-upp-skrapet-1',
'only_matching': True,
return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTPageIE, cls).suitable(url)
def _real_extract(self, url):
- path, display_id = re.match(self._VALID_URL, url).groups()
+ display_id = self._match_id(url)
- article = self._download_json(
- 'https://api.svt.se/nss-api/page/' + path, display_id,
- query={'q': 'articles'})['articles']['content'][0]
+ webpage = self._download_webpage(url, display_id)
+ title = self._og_search_title(webpage)
- entries = []
+ urql_state = self._search_json(
+ r'window\.svt\.nyh\.urqlState\s*=', webpage, 'json data', display_id)
- def _process_content(content):
- if content.get('_type') in ('VIDEOCLIP', 'VIDEOEPISODE'):
- video_id = compat_str(content['image']['svtId'])
- entries.append(self.url_result(
- 'svt:' + video_id, SVTPlayIE.ie_key(), video_id))
-
- for media in article.get('media', []):
- _process_content(media)
+ data = traverse_obj(urql_state, (..., 'data', {str}, {json.loads}), get_all=False) or {}
- for obj in article.get('structuredBody', []):
- _process_content(obj.get('content') or {})
+ def entries():
+ for video_id in set(traverse_obj(data, (
+ 'page', (('topMedia', 'svtId'), ('body', ..., 'video', 'svtId')), {str}
+ ))):
+ info = self._extract_video(
+ self._download_json(f'https://api.svt.se/video/{video_id}', video_id), video_id)
+ info['title'] = title
+ yield info
- return self.playlist_result(
- entries, str_or_none(article.get('id')),
- strip_or_none(article.get('title')))
+ return self.playlist_result(entries(), display_id, title)