2 from __future__ import unicode_literals
6 from .common import InfoExtractor
9 compat_urllib_parse_urlparse,
21 class SVTBaseIE(InfoExtractor):
22 _GEO_COUNTRIES = ['SE']
24 def _extract_video(self, video_info, video_id):
26 for vr in video_info['videoReferences']:
27 player_type = vr.get('playerType') or vr.get('format')
29 ext = determine_ext(vurl)
31 formats.extend(self._extract_m3u8_formats(
33 ext='mp4', entry_protocol='m3u8_native',
34 m3u8_id=player_type, fatal=False))
36 formats.extend(self._extract_f4m_formats(
37 vurl + '?hdcore=3.3.0', video_id,
38 f4m_id=player_type, fatal=False))
40 if player_type == 'dashhbbtv':
41 formats.extend(self._extract_mpd_formats(
42 vurl, video_id, mpd_id=player_type, fatal=False))
45 'format_id': player_type,
48 if not formats and video_info.get('rights', {}).get('geoBlockedSweden'):
49 self.raise_geo_restricted(
50 'This video is only available in Sweden',
51 countries=self._GEO_COUNTRIES)
52 self._sort_formats(formats)
55 subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences'))
56 if isinstance(subtitle_references, list):
57 for sr in subtitle_references:
58 subtitle_url = sr.get('url')
59 subtitle_lang = sr.get('language', 'sv')
61 if determine_ext(subtitle_url) == 'm3u8':
62 # TODO(yan12125): handle WebVTT in m3u8 manifests
65 subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url})
67 title = video_info.get('title')
69 series = video_info.get('programTitle')
70 season_number = int_or_none(video_info.get('season'))
71 episode = video_info.get('episodeTitle')
72 episode_number = int_or_none(video_info.get('episodeNumber'))
74 duration = int_or_none(dict_get(video_info, ('materialLength', 'contentDuration')))
77 video_info, ('inappropriateForChildren', 'blockedForChildren'),
78 skip_false_values=False)
80 age_limit = 18 if adult else 0
86 'subtitles': subtitles,
88 'age_limit': age_limit,
90 'season_number': season_number,
92 'episode_number': episode_number,
96 class SVTIE(SVTBaseIE):
97 _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
99 'url': 'http://www.svt.se/wd?widgetId=23991§ionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
100 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
104 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
111 def _extract_url(webpage):
113 r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
115 return mobj.group('url')
117 def _real_extract(self, url):
118 mobj = re.match(self._VALID_URL, url)
119 widget_id = mobj.group('widget_id')
120 article_id = mobj.group('id')
122 info = self._download_json(
123 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
126 info_dict = self._extract_video(info['video'], article_id)
127 info_dict['title'] = info['context']['title']
131 class SVTPlayIE(SVTBaseIE):
132 IE_DESC = 'SVT Play and Öppet arkiv'
133 _VALID_URL = r'https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp)/(?P<id>[0-9]+)'
135 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
136 'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
140 'title': 'Flygplan till Haile Selassie',
142 'thumbnail': r're:^https?://.*[\.-]jpg$',
151 # geo restricted to Sweden
152 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
153 'only_matching': True,
155 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg',
156 'only_matching': True,
159 def _real_extract(self, url):
160 video_id = self._match_id(url)
162 webpage = self._download_webpage(url, video_id)
164 data = self._parse_json(
166 r'root\["__svtplay"\]\s*=\s*([^;]+);',
167 webpage, 'embedded data', default='{}'),
168 video_id, fatal=False)
170 thumbnail = self._og_search_thumbnail(webpage)
173 video_info = try_get(
174 data, lambda x: x['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'],
177 info_dict = self._extract_video(video_info, video_id)
179 'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
180 'thumbnail': thumbnail,
184 video_id = self._search_regex(
185 r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
186 webpage, 'video id', default=None)
189 data = self._download_json(
190 'https://api.svt.se/videoplayer-api/video/%s' % video_id,
191 video_id, headers=self.geo_verification_headers())
192 info_dict = self._extract_video(data, video_id)
193 if not info_dict.get('title'):
194 info_dict['title'] = re.sub(
196 info_dict.get('episode') or self._og_search_title(webpage))
200 class SVTSeriesIE(InfoExtractor):
201 _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)'
203 'url': 'https://www.svtplay.se/rederiet',
207 'description': 'md5:505d491a58f4fcf6eb418ecab947e69e',
209 'playlist_mincount': 318,
211 'url': 'https://www.svtplay.se/rederiet?tab=sasong2',
213 'id': 'rederiet-sasong2',
214 'title': 'Rederiet - Säsong 2',
215 'description': 'md5:505d491a58f4fcf6eb418ecab947e69e',
217 'playlist_count': 12,
221 def suitable(cls, url):
222 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url)
224 def _real_extract(self, url):
225 series_id = self._match_id(url)
227 qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
228 season_slug = qs.get('tab', [None])[0]
231 series_id += '-%s' % season_slug
233 webpage = self._download_webpage(
234 url, series_id, 'Downloading series page')
236 root = self._parse_json(
238 r'root\[\s*(["\'])_*svtplay\1\s*\]\s*=\s*(?P<json>{.+?})\s*;\s*\n',
239 webpage, 'content', group='json'),
245 for season in root['relatedVideoContent']['relatedVideosAccordion']:
246 if not isinstance(season, dict):
249 if season.get('slug') != season_slug:
251 season_name = season.get('name')
252 videos = season.get('videos')
253 if not isinstance(videos, list):
256 content_url = video.get('contentUrl')
257 if not content_url or not isinstance(content_url, compat_str):
261 urljoin(url, content_url),
262 ie=SVTPlayIE.ie_key(),
263 video_title=video.get('title')
266 metadata = root.get('metaData')
267 if not isinstance(metadata, dict):
270 title = metadata.get('title')
271 season_name = season_name or season_slug
273 if title and season_name:
274 title = '%s - %s' % (title, season_name)
278 return self.playlist_result(
279 entries, series_id, title, metadata.get('description'))