]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/svt.py
[peertube] Add support for generic embeds
[yt-dlp.git] / youtube_dl / extractor / svt.py
CommitLineData
28f12728 1# coding: utf-8
1309b396
PH
2from __future__ import unicode_literals
3
df5ae3eb
S
4import re
5
1309b396 6from .common import InfoExtractor
df146eb2
S
7from ..compat import (
8 compat_parse_qs,
9 compat_urllib_parse_urlparse,
10)
1309b396
PH
11from ..utils import (
12 determine_ext,
e4f90ea0 13 dict_get,
23bdae09
S
14 int_or_none,
15 try_get,
fd97fa7b
MW
16 urljoin,
17 compat_str,
1309b396
PH
18)
19
20
79998cd5 21class SVTBaseIE(InfoExtractor):
4248dad9 22 _GEO_COUNTRIES = ['SE']
6d4c2597 23
23bdae09 24 def _extract_video(self, video_info, video_id):
488ff2dd 25 is_live = dict_get(video_info, ('live', 'simulcast'), default=False)
26 m3u8_protocol = 'm3u8' if is_live else 'm3u8_native'
1309b396
PH
27 formats = []
28 for vr in video_info['videoReferences']:
21d21b0c 29 player_type = vr.get('playerType') or vr.get('format')
1309b396 30 vurl = vr['url']
df5ae3eb
S
31 ext = determine_ext(vurl)
32 if ext == 'm3u8':
1309b396
PH
33 formats.extend(self._extract_m3u8_formats(
34 vurl, video_id,
488ff2dd 35 ext='mp4', entry_protocol=m3u8_protocol,
edfd9351 36 m3u8_id=player_type, fatal=False))
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':
42 if player_type == 'dashhbbtv':
43 formats.extend(self._extract_mpd_formats(
44 vurl, video_id, mpd_id=player_type, fatal=False))
1309b396
PH
45 else:
46 formats.append({
edfd9351 47 'format_id': player_type,
1309b396
PH
48 'url': vurl,
49 })
23bdae09 50 if not formats and video_info.get('rights', {}).get('geoBlockedSweden'):
04d906ea 51 self.raise_geo_restricted(
4248dad9
S
52 'This video is only available in Sweden',
53 countries=self._GEO_COUNTRIES)
1309b396
PH
54 self._sort_formats(formats)
55
1f16b958 56 subtitles = {}
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:
e4f90ea0
YCH
63 if determine_ext(subtitle_url) == 'm3u8':
64 # TODO(yan12125): handle WebVTT in m3u8 manifests
65 continue
66
67 subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url})
1f16b958 68
23bdae09
S
69 title = video_info.get('title')
70
71 series = video_info.get('programTitle')
72 season_number = int_or_none(video_info.get('season'))
73 episode = video_info.get('episodeTitle')
74 episode_number = int_or_none(video_info.get('episodeNumber'))
75
76 duration = int_or_none(dict_get(video_info, ('materialLength', 'contentDuration')))
77 age_limit = None
78 adult = dict_get(
79 video_info, ('inappropriateForChildren', 'blockedForChildren'),
80 skip_false_values=False)
81 if adult is not None:
82 age_limit = 18 if adult else 0
1309b396
PH
83
84 return {
85 'id': video_id,
23bdae09 86 'title': title,
1309b396 87 'formats': formats,
1f16b958 88 'subtitles': subtitles,
1309b396 89 'duration': duration,
df5ae3eb 90 'age_limit': age_limit,
23bdae09
S
91 'series': series,
92 'season_number': season_number,
93 'episode': episode,
94 'episode_number': episode_number,
488ff2dd 95 'is_live': is_live,
1309b396 96 }
79998cd5
S
97
98
99class SVTIE(SVTBaseIE):
100 _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
101 _TEST = {
102 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
e4f90ea0 103 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
79998cd5
S
104 'info_dict': {
105 'id': '2900353',
e4f90ea0
YCH
106 'ext': 'mp4',
107 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
79998cd5
S
108 'duration': 27,
109 'age_limit': 0,
110 },
111 }
112
bab19a8e
S
113 @staticmethod
114 def _extract_url(webpage):
115 mobj = re.search(
116 r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
117 if mobj:
118 return mobj.group('url')
119
79998cd5
S
120 def _real_extract(self, url):
121 mobj = re.match(self._VALID_URL, url)
122 widget_id = mobj.group('widget_id')
123 article_id = mobj.group('id')
e4f90ea0
YCH
124
125 info = self._download_json(
79998cd5
S
126 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
127 article_id)
128
23bdae09 129 info_dict = self._extract_video(info['video'], article_id)
e4f90ea0
YCH
130 info_dict['title'] = info['context']['title']
131 return info_dict
132
79998cd5 133
1236ac6b
S
134class SVTPlayBaseIE(SVTBaseIE):
135 _SVTPLAY_RE = r'root\s*\[\s*(["\'])_*svtplay\1\s*\]\s*=\s*(?P<json>{.+?})\s*;\s*\n'
136
137
138class SVTPlayIE(SVTPlayBaseIE):
79998cd5 139 IE_DESC = 'SVT Play and Öppet arkiv'
6cdaaf70 140 _VALID_URL = r'https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp|kanaler)/(?P<id>[^/?#&]+)'
23bdae09 141 _TESTS = [{
594c4d79
S
142 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
143 'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
79998cd5 144 'info_dict': {
594c4d79
S
145 'id': '5996901',
146 'ext': 'mp4',
147 'title': 'Flygplan till Haile Selassie',
148 'duration': 3527,
ec85ded8 149 'thumbnail': r're:^https?://.*[\.-]jpg$',
79998cd5 150 'age_limit': 0,
594c4d79
S
151 'subtitles': {
152 'sv': [{
153 'ext': 'wsrt',
154 }]
155 },
79998cd5 156 },
23bdae09
S
157 }, {
158 # geo restricted to Sweden
159 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
160 'only_matching': True,
3b34ab53
S
161 }, {
162 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg',
163 'only_matching': True,
488ff2dd 164 }, {
165 'url': 'https://www.svtplay.se/kanaler/svt1',
166 'only_matching': True,
23bdae09 167 }]
e4f90ea0 168
79998cd5 169 def _real_extract(self, url):
e4f90ea0
YCH
170 video_id = self._match_id(url)
171
172 webpage = self._download_webpage(url, video_id)
173
23bdae09
S
174 data = self._parse_json(
175 self._search_regex(
1236ac6b
S
176 self._SVTPLAY_RE, webpage, 'embedded data', default='{}',
177 group='json'),
23bdae09 178 video_id, fatal=False)
e4f90ea0
YCH
179
180 thumbnail = self._og_search_thumbnail(webpage)
181
6cdaaf70
S
182 def adjust_title(info):
183 if info['is_live']:
184 info['title'] = self._live_title(info['title'])
185
23bdae09
S
186 if data:
187 video_info = try_get(
188 data, lambda x: x['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'],
189 dict)
190 if video_info:
191 info_dict = self._extract_video(video_info, video_id)
192 info_dict.update({
193 'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
194 'thumbnail': thumbnail,
195 })
6cdaaf70 196 adjust_title(info_dict)
23bdae09
S
197 return info_dict
198
199 video_id = self._search_regex(
200 r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
201 webpage, 'video id', default=None)
202
203 if video_id:
204 data = self._download_json(
c0401751
S
205 'https://api.svt.se/videoplayer-api/video/%s' % video_id,
206 video_id, headers=self.geo_verification_headers())
23bdae09
S
207 info_dict = self._extract_video(data, video_id)
208 if not info_dict.get('title'):
209 info_dict['title'] = re.sub(
210 r'\s*\|\s*.+?$', '',
211 info_dict.get('episode') or self._og_search_title(webpage))
6cdaaf70 212 adjust_title(info_dict)
23bdae09 213 return info_dict
fd97fa7b
MW
214
215
1236ac6b 216class SVTSeriesIE(SVTPlayBaseIE):
fd97fa7b 217 _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)'
fd97fa7b
MW
218 _TESTS = [{
219 'url': 'https://www.svtplay.se/rederiet',
220 'info_dict': {
221 'id': 'rederiet',
222 'title': 'Rederiet',
223 'description': 'md5:505d491a58f4fcf6eb418ecab947e69e',
224 },
225 'playlist_mincount': 318,
df146eb2
S
226 }, {
227 'url': 'https://www.svtplay.se/rederiet?tab=sasong2',
228 'info_dict': {
229 'id': 'rederiet-sasong2',
230 'title': 'Rederiet - Säsong 2',
231 'description': 'md5:505d491a58f4fcf6eb418ecab947e69e',
232 },
233 'playlist_count': 12,
fd97fa7b
MW
234 }]
235
236 @classmethod
237 def suitable(cls, url):
b71bb3ba 238 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url)
fd97fa7b
MW
239
240 def _real_extract(self, url):
df146eb2
S
241 series_id = self._match_id(url)
242
243 qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
244 season_slug = qs.get('tab', [None])[0]
245
246 if season_slug:
247 series_id += '-%s' % season_slug
fd97fa7b 248
b71bb3ba 249 webpage = self._download_webpage(
df146eb2 250 url, series_id, 'Downloading series page')
fd97fa7b 251
b71bb3ba
S
252 root = self._parse_json(
253 self._search_regex(
1236ac6b 254 self._SVTPLAY_RE, webpage, 'content', group='json'),
df146eb2
S
255 series_id)
256
257 season_name = None
fd97fa7b
MW
258
259 entries = []
b71bb3ba 260 for season in root['relatedVideoContent']['relatedVideosAccordion']:
df146eb2
S
261 if not isinstance(season, dict):
262 continue
263 if season_slug:
264 if season.get('slug') != season_slug:
265 continue
266 season_name = season.get('name')
fd97fa7b
MW
267 videos = season.get('videos')
268 if not isinstance(videos, list):
269 continue
fd97fa7b
MW
270 for video in videos:
271 content_url = video.get('contentUrl')
b71bb3ba 272 if not content_url or not isinstance(content_url, compat_str):
fd97fa7b
MW
273 continue
274 entries.append(
275 self.url_result(
276 urljoin(url, content_url),
277 ie=SVTPlayIE.ie_key(),
278 video_title=video.get('title')
279 ))
280
b71bb3ba
S
281 metadata = root.get('metaData')
282 if not isinstance(metadata, dict):
283 metadata = {}
284
df146eb2
S
285 title = metadata.get('title')
286 season_name = season_name or season_slug
287
288 if title and season_name:
289 title = '%s - %s' % (title, season_name)
290 elif season_slug:
291 title = season_slug
292
fd97fa7b 293 return self.playlist_result(
df146eb2 294 entries, series_id, title, metadata.get('description'))