]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/svt.py
0901c3163e6cab4723d451b30df8574e359ba899
[youtubedl] / youtube_dl / extractor / svt.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_parse_qs,
9 compat_urllib_parse_urlparse,
10 )
11 from ..utils import (
12 determine_ext,
13 dict_get,
14 int_or_none,
15 orderedSet,
16 strip_or_none,
17 try_get,
18 urljoin,
19 compat_str,
20 )
21
22
23 class SVTBaseIE(InfoExtractor):
24 _GEO_COUNTRIES = ['SE']
25
26 def _extract_video(self, video_info, video_id):
27 is_live = dict_get(video_info, ('live', 'simulcast'), default=False)
28 m3u8_protocol = 'm3u8' if is_live else 'm3u8_native'
29 formats = []
30 for vr in video_info['videoReferences']:
31 player_type = vr.get('playerType') or vr.get('format')
32 vurl = vr['url']
33 ext = determine_ext(vurl)
34 if ext == 'm3u8':
35 formats.extend(self._extract_m3u8_formats(
36 vurl, video_id,
37 ext='mp4', entry_protocol=m3u8_protocol,
38 m3u8_id=player_type, fatal=False))
39 elif ext == 'f4m':
40 formats.extend(self._extract_f4m_formats(
41 vurl + '?hdcore=3.3.0', video_id,
42 f4m_id=player_type, fatal=False))
43 elif ext == 'mpd':
44 if player_type == 'dashhbbtv':
45 formats.extend(self._extract_mpd_formats(
46 vurl, video_id, mpd_id=player_type, fatal=False))
47 else:
48 formats.append({
49 'format_id': player_type,
50 'url': vurl,
51 })
52 if not formats and video_info.get('rights', {}).get('geoBlockedSweden'):
53 self.raise_geo_restricted(
54 'This video is only available in Sweden',
55 countries=self._GEO_COUNTRIES)
56 self._sort_formats(formats)
57
58 subtitles = {}
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')
64 if subtitle_url:
65 if determine_ext(subtitle_url) == 'm3u8':
66 # TODO(yan12125): handle WebVTT in m3u8 manifests
67 continue
68
69 subtitles.setdefault(subtitle_lang, []).append({'url': subtitle_url})
70
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
78 duration = int_or_none(dict_get(video_info, ('materialLength', 'contentDuration')))
79 age_limit = None
80 adult = dict_get(
81 video_info, ('inappropriateForChildren', 'blockedForChildren'),
82 skip_false_values=False)
83 if adult is not None:
84 age_limit = 18 if adult else 0
85
86 return {
87 'id': video_id,
88 'title': title,
89 'formats': formats,
90 'subtitles': subtitles,
91 'duration': duration,
92 'age_limit': age_limit,
93 'series': series,
94 'season_number': season_number,
95 'episode': episode,
96 'episode_number': episode_number,
97 'is_live': is_live,
98 }
99
100
101 class SVTIE(SVTBaseIE):
102 _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)'
103 _TEST = {
104 'url': 'http://www.svt.se/wd?widgetId=23991&sectionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false',
105 'md5': '33e9a5d8f646523ce0868ecfb0eed77d',
106 'info_dict': {
107 'id': '2900353',
108 'ext': 'mp4',
109 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
110 'duration': 27,
111 'age_limit': 0,
112 },
113 }
114
115 @staticmethod
116 def _extract_url(webpage):
117 mobj = re.search(
118 r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage)
119 if mobj:
120 return mobj.group('url')
121
122 def _real_extract(self, url):
123 mobj = re.match(self._VALID_URL, url)
124 widget_id = mobj.group('widget_id')
125 article_id = mobj.group('id')
126
127 info = self._download_json(
128 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id),
129 article_id)
130
131 info_dict = self._extract_video(info['video'], article_id)
132 info_dict['title'] = info['context']['title']
133 return info_dict
134
135
136 class SVTPlayBaseIE(SVTBaseIE):
137 _SVTPLAY_RE = r'root\s*\[\s*(["\'])_*svtplay\1\s*\]\s*=\s*(?P<json>{.+?})\s*;\s*\n'
138
139
140 class SVTPlayIE(SVTPlayBaseIE):
141 IE_DESC = 'SVT Play and Öppet arkiv'
142 _VALID_URL = r'''(?x)
143 (?:
144 svt:(?P<svt_id>[^/?#&]+)|
145 https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp|kanaler)/(?P<id>[^/?#&]+)
146 )
147 '''
148 _TESTS = [{
149 'url': 'http://www.svtplay.se/video/5996901/flygplan-till-haile-selassie/flygplan-till-haile-selassie-2',
150 'md5': '2b6704fe4a28801e1a098bbf3c5ac611',
151 'info_dict': {
152 'id': '5996901',
153 'ext': 'mp4',
154 'title': 'Flygplan till Haile Selassie',
155 'duration': 3527,
156 'thumbnail': r're:^https?://.*[\.-]jpg$',
157 'age_limit': 0,
158 'subtitles': {
159 'sv': [{
160 'ext': 'wsrt',
161 }]
162 },
163 },
164 }, {
165 # geo restricted to Sweden
166 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten',
167 'only_matching': True,
168 }, {
169 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg',
170 'only_matching': True,
171 }, {
172 'url': 'https://www.svtplay.se/kanaler/svt1',
173 'only_matching': True,
174 }, {
175 'url': 'svt:1376446-003A',
176 'only_matching': True,
177 }, {
178 'url': 'svt:14278044',
179 'only_matching': True,
180 }]
181
182 def _adjust_title(self, info):
183 if info['is_live']:
184 info['title'] = self._live_title(info['title'])
185
186 def _extract_by_video_id(self, video_id, webpage=None):
187 data = self._download_json(
188 'https://api.svt.se/videoplayer-api/video/%s' % video_id,
189 video_id, headers=self.geo_verification_headers())
190 info_dict = self._extract_video(data, video_id)
191 if not info_dict.get('title'):
192 title = dict_get(info_dict, ('episode', 'series'))
193 if not title and webpage:
194 title = re.sub(
195 r'\s*\|\s*.+?$', '', self._og_search_title(webpage))
196 if not title:
197 title = video_id
198 info_dict['title'] = title
199 self._adjust_title(info_dict)
200 return info_dict
201
202 def _real_extract(self, url):
203 mobj = re.match(self._VALID_URL, url)
204 video_id, svt_id = mobj.group('id', 'svt_id')
205
206 if svt_id:
207 return self._extract_by_video_id(svt_id)
208
209 webpage = self._download_webpage(url, video_id)
210
211 data = self._parse_json(
212 self._search_regex(
213 self._SVTPLAY_RE, webpage, 'embedded data', default='{}',
214 group='json'),
215 video_id, fatal=False)
216
217 thumbnail = self._og_search_thumbnail(webpage)
218
219 if data:
220 video_info = try_get(
221 data, lambda x: x['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'],
222 dict)
223 if video_info:
224 info_dict = self._extract_video(video_info, video_id)
225 info_dict.update({
226 'title': data['context']['dispatcher']['stores']['MetaStore']['title'],
227 'thumbnail': thumbnail,
228 })
229 self._adjust_title(info_dict)
230 return info_dict
231
232 svt_id = self._search_regex(
233 r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)',
234 webpage, 'video id')
235
236 return self._extract_by_video_id(svt_id, webpage)
237
238
239 class SVTSeriesIE(SVTPlayBaseIE):
240 _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)'
241 _TESTS = [{
242 'url': 'https://www.svtplay.se/rederiet',
243 'info_dict': {
244 'id': 'rederiet',
245 'title': 'Rederiet',
246 'description': 'md5:505d491a58f4fcf6eb418ecab947e69e',
247 },
248 'playlist_mincount': 318,
249 }, {
250 'url': 'https://www.svtplay.se/rederiet?tab=sasong2',
251 'info_dict': {
252 'id': 'rederiet-sasong2',
253 'title': 'Rederiet - Säsong 2',
254 'description': 'md5:505d491a58f4fcf6eb418ecab947e69e',
255 },
256 'playlist_count': 12,
257 }]
258
259 @classmethod
260 def suitable(cls, url):
261 return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url)
262
263 def _real_extract(self, url):
264 series_id = self._match_id(url)
265
266 qs = compat_parse_qs(compat_urllib_parse_urlparse(url).query)
267 season_slug = qs.get('tab', [None])[0]
268
269 if season_slug:
270 series_id += '-%s' % season_slug
271
272 webpage = self._download_webpage(
273 url, series_id, 'Downloading series page')
274
275 root = self._parse_json(
276 self._search_regex(
277 self._SVTPLAY_RE, webpage, 'content', group='json'),
278 series_id)
279
280 season_name = None
281
282 entries = []
283 for season in root['relatedVideoContent']['relatedVideosAccordion']:
284 if not isinstance(season, dict):
285 continue
286 if season_slug:
287 if season.get('slug') != season_slug:
288 continue
289 season_name = season.get('name')
290 videos = season.get('videos')
291 if not isinstance(videos, list):
292 continue
293 for video in videos:
294 content_url = video.get('contentUrl')
295 if not content_url or not isinstance(content_url, compat_str):
296 continue
297 entries.append(
298 self.url_result(
299 urljoin(url, content_url),
300 ie=SVTPlayIE.ie_key(),
301 video_title=video.get('title')
302 ))
303
304 metadata = root.get('metaData')
305 if not isinstance(metadata, dict):
306 metadata = {}
307
308 title = metadata.get('title')
309 season_name = season_name or season_slug
310
311 if title and season_name:
312 title = '%s - %s' % (title, season_name)
313 elif season_slug:
314 title = season_slug
315
316 return self.playlist_result(
317 entries, series_id, title, metadata.get('description'))
318
319
320 class SVTPageIE(InfoExtractor):
321 _VALID_URL = r'https?://(?:www\.)?svt\.se/(?:[^/]+/)*(?P<id>[^/?&#]+)'
322 _TESTS = [{
323 'url': 'https://www.svt.se/sport/oseedat/guide-sommartraningen-du-kan-gora-var-och-nar-du-vill',
324 'info_dict': {
325 'id': 'guide-sommartraningen-du-kan-gora-var-och-nar-du-vill',
326 'title': 'GUIDE: Sommarträning du kan göra var och när du vill',
327 },
328 'playlist_count': 7,
329 }, {
330 'url': 'https://www.svt.se/nyheter/inrikes/ebba-busch-thor-kd-har-delvis-ratt-om-no-go-zoner',
331 'info_dict': {
332 'id': 'ebba-busch-thor-kd-har-delvis-ratt-om-no-go-zoner',
333 'title': 'Ebba Busch Thor har bara delvis rätt om ”no-go-zoner”',
334 },
335 'playlist_count': 1,
336 }, {
337 # only programTitle
338 'url': 'http://www.svt.se/sport/ishockey/jagr-tacklar-giroux-under-intervjun',
339 'info_dict': {
340 'id': '2900353',
341 'ext': 'mp4',
342 'title': 'Stjärnorna skojar till det - under SVT-intervjun',
343 'duration': 27,
344 'age_limit': 0,
345 },
346 }, {
347 'url': 'https://www.svt.se/nyheter/lokalt/vast/svt-testar-tar-nagon-upp-skrapet-1',
348 'only_matching': True,
349 }, {
350 'url': 'https://www.svt.se/vader/manadskronikor/maj2018',
351 'only_matching': True,
352 }]
353
354 @classmethod
355 def suitable(cls, url):
356 return False if SVTIE.suitable(url) else super(SVTPageIE, cls).suitable(url)
357
358 def _real_extract(self, url):
359 playlist_id = self._match_id(url)
360
361 webpage = self._download_webpage(url, playlist_id)
362
363 entries = [
364 self.url_result(
365 'svt:%s' % video_id, ie=SVTPlayIE.ie_key(), video_id=video_id)
366 for video_id in orderedSet(re.findall(
367 r'data-video-id=["\'](\d+)', webpage))]
368
369 title = strip_or_none(self._og_search_title(webpage, default=None))
370
371 return self.playlist_result(entries, playlist_id, title)