]>
Commit | Line | Data |
---|---|---|
1 | import re | |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..compat import compat_str | |
5 | from ..utils import ( | |
6 | determine_ext, | |
7 | dict_get, | |
8 | int_or_none, | |
9 | unified_timestamp, | |
10 | str_or_none, | |
11 | strip_or_none, | |
12 | try_get, | |
13 | ) | |
14 | ||
15 | ||
16 | class SVTBaseIE(InfoExtractor): | |
17 | _GEO_COUNTRIES = ['SE'] | |
18 | ||
19 | def _extract_video(self, video_info, video_id): | |
20 | is_live = dict_get(video_info, ('live', 'simulcast'), default=False) | |
21 | m3u8_protocol = 'm3u8' if is_live else 'm3u8_native' | |
22 | formats = [] | |
23 | subtitles = {} | |
24 | for vr in video_info['videoReferences']: | |
25 | player_type = vr.get('playerType') or vr.get('format') | |
26 | vurl = vr['url'] | |
27 | ext = determine_ext(vurl) | |
28 | if ext == 'm3u8': | |
29 | fmts, subs = self._extract_m3u8_formats_and_subtitles( | |
30 | vurl, video_id, | |
31 | ext='mp4', entry_protocol=m3u8_protocol, | |
32 | m3u8_id=player_type, fatal=False) | |
33 | formats.extend(fmts) | |
34 | self._merge_subtitles(subs, target=subtitles) | |
35 | elif ext == 'f4m': | |
36 | formats.extend(self._extract_f4m_formats( | |
37 | vurl + '?hdcore=3.3.0', video_id, | |
38 | f4m_id=player_type, fatal=False)) | |
39 | elif ext == 'mpd': | |
40 | fmts, subs = self._extract_mpd_formats_and_subtitles( | |
41 | vurl, video_id, mpd_id=player_type, fatal=False) | |
42 | formats.extend(fmts) | |
43 | self._merge_subtitles(subs, target=subtitles) | |
44 | else: | |
45 | formats.append({ | |
46 | 'format_id': player_type, | |
47 | 'url': vurl, | |
48 | }) | |
49 | rights = try_get(video_info, lambda x: x['rights'], dict) or {} | |
50 | if not formats and rights.get('geoBlockedSweden'): | |
51 | self.raise_geo_restricted( | |
52 | 'This video is only available in Sweden', | |
53 | countries=self._GEO_COUNTRIES, metadata_available=True) | |
54 | self._sort_formats(formats) | |
55 | ||
56 | subtitle_references = dict_get(video_info, ('subtitles', 'subtitleReferences')) | |
57 | if isinstance(subtitle_references, list): | |
58 | for sr in subtitle_references: | |
59 | subtitle_url = sr.get('url') | |
60 | subtitle_lang = sr.get('language', 'sv') | |
61 | if subtitle_url: | |
62 | sub = { | |
63 | 'url': subtitle_url, | |
64 | } | |
65 | if determine_ext(subtitle_url) == 'm3u8': | |
66 | # XXX: no way of testing, is it ever hit? | |
67 | sub['ext'] = 'vtt' | |
68 | subtitles.setdefault(subtitle_lang, []).append(sub) | |
69 | ||
70 | title = video_info.get('title') | |
71 | ||
72 | series = video_info.get('programTitle') | |
73 | season_number = int_or_none(video_info.get('season')) | |
74 | episode = video_info.get('episodeTitle') | |
75 | episode_number = int_or_none(video_info.get('episodeNumber')) | |
76 | ||
77 | timestamp = unified_timestamp(rights.get('validFrom')) | |
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 | 'timestamp': timestamp, | |
93 | 'age_limit': age_limit, | |
94 | 'series': series, | |
95 | 'season_number': season_number, | |
96 | 'episode': episode, | |
97 | 'episode_number': episode_number, | |
98 | 'is_live': is_live, | |
99 | } | |
100 | ||
101 | ||
102 | class SVTIE(SVTBaseIE): | |
103 | _VALID_URL = r'https?://(?:www\.)?svt\.se/wd\?(?:.*?&)?widgetId=(?P<widget_id>\d+)&.*?\barticleId=(?P<id>\d+)' | |
104 | _TEST = { | |
105 | 'url': 'http://www.svt.se/wd?widgetId=23991§ionId=541&articleId=2900353&type=embed&contextSectionId=123&autostart=false', | |
106 | 'md5': '33e9a5d8f646523ce0868ecfb0eed77d', | |
107 | 'info_dict': { | |
108 | 'id': '2900353', | |
109 | 'ext': 'mp4', | |
110 | 'title': 'Stjärnorna skojar till det - under SVT-intervjun', | |
111 | 'duration': 27, | |
112 | 'age_limit': 0, | |
113 | }, | |
114 | } | |
115 | ||
116 | @staticmethod | |
117 | def _extract_url(webpage): | |
118 | mobj = re.search( | |
119 | r'(?:<iframe src|href)="(?P<url>%s[^"]*)"' % SVTIE._VALID_URL, webpage) | |
120 | if mobj: | |
121 | return mobj.group('url') | |
122 | ||
123 | def _real_extract(self, url): | |
124 | mobj = self._match_valid_url(url) | |
125 | widget_id = mobj.group('widget_id') | |
126 | article_id = mobj.group('id') | |
127 | ||
128 | info = self._download_json( | |
129 | 'http://www.svt.se/wd?widgetId=%s&articleId=%s&format=json&type=embed&output=json' % (widget_id, article_id), | |
130 | article_id) | |
131 | ||
132 | info_dict = self._extract_video(info['video'], article_id) | |
133 | info_dict['title'] = info['context']['title'] | |
134 | return info_dict | |
135 | ||
136 | ||
137 | class SVTPlayBaseIE(SVTBaseIE): | |
138 | _SVTPLAY_RE = r'root\s*\[\s*(["\'])_*svtplay\1\s*\]\s*=\s*(?P<json>{.+?})\s*;\s*\n' | |
139 | ||
140 | ||
141 | class SVTPlayIE(SVTPlayBaseIE): | |
142 | IE_DESC = 'SVT Play and Öppet arkiv' | |
143 | _VALID_URL = r'''(?x) | |
144 | (?: | |
145 | (?: | |
146 | svt:| | |
147 | https?://(?:www\.)?svt\.se/barnkanalen/barnplay/[^/]+/ | |
148 | ) | |
149 | (?P<svt_id>[^/?#&]+)| | |
150 | https?://(?:www\.)?(?:svtplay|oppetarkiv)\.se/(?:video|klipp|kanaler)/(?P<id>[^/?#&]+) | |
151 | (?:.*?(?:modalId|id)=(?P<modal_id>[\da-zA-Z-]+))? | |
152 | ) | |
153 | ''' | |
154 | _TESTS = [{ | |
155 | 'url': 'https://www.svtplay.se/video/30479064', | |
156 | 'md5': '2382036fd6f8c994856c323fe51c426e', | |
157 | 'info_dict': { | |
158 | 'id': '8zVbDPA', | |
159 | 'ext': 'mp4', | |
160 | 'title': 'Designdrömmar i Stenungsund', | |
161 | 'timestamp': 1615770000, | |
162 | 'upload_date': '20210315', | |
163 | 'duration': 3519, | |
164 | 'thumbnail': r're:^https?://(?:.*[\.-]jpg|www.svtstatic.se/image/.*)$', | |
165 | 'age_limit': 0, | |
166 | 'subtitles': { | |
167 | 'sv': [{ | |
168 | 'ext': 'vtt', | |
169 | }] | |
170 | }, | |
171 | }, | |
172 | 'params': { | |
173 | # skip for now due to download test asserts that segment is > 10000 bytes and svt uses | |
174 | # init segments that are smaller | |
175 | # AssertionError: Expected test_SVTPlay_jNwpV9P.mp4 to be at least 9.77KiB, but it's only 864.00B | |
176 | 'skip_download': True, | |
177 | }, | |
178 | }, { | |
179 | 'url': 'https://www.svtplay.se/video/30479064/husdrommar/husdrommar-sasong-8-designdrommar-i-stenungsund?modalId=8zVbDPA', | |
180 | 'only_matching': True, | |
181 | }, { | |
182 | 'url': 'https://www.svtplay.se/video/30684086/rapport/rapport-24-apr-18-00-7?id=e72gVpa', | |
183 | 'only_matching': True, | |
184 | }, { | |
185 | # geo restricted to Sweden | |
186 | 'url': 'http://www.oppetarkiv.se/video/5219710/trollflojten', | |
187 | 'only_matching': True, | |
188 | }, { | |
189 | 'url': 'http://www.svtplay.se/klipp/9023742/stopptid-om-bjorn-borg', | |
190 | 'only_matching': True, | |
191 | }, { | |
192 | 'url': 'https://www.svtplay.se/kanaler/svt1', | |
193 | 'only_matching': True, | |
194 | }, { | |
195 | 'url': 'svt:1376446-003A', | |
196 | 'only_matching': True, | |
197 | }, { | |
198 | 'url': 'svt:14278044', | |
199 | 'only_matching': True, | |
200 | }, { | |
201 | 'url': 'https://www.svt.se/barnkanalen/barnplay/kar/eWv5MLX/', | |
202 | 'only_matching': True, | |
203 | }, { | |
204 | 'url': 'svt:eWv5MLX', | |
205 | 'only_matching': True, | |
206 | }] | |
207 | ||
208 | def _extract_by_video_id(self, video_id, webpage=None): | |
209 | data = self._download_json( | |
210 | 'https://api.svt.se/videoplayer-api/video/%s' % video_id, | |
211 | video_id, headers=self.geo_verification_headers()) | |
212 | info_dict = self._extract_video(data, video_id) | |
213 | if not info_dict.get('title'): | |
214 | title = dict_get(info_dict, ('episode', 'series')) | |
215 | if not title and webpage: | |
216 | title = re.sub( | |
217 | r'\s*\|\s*.+?$', '', self._og_search_title(webpage)) | |
218 | if not title: | |
219 | title = video_id | |
220 | info_dict['title'] = title | |
221 | return info_dict | |
222 | ||
223 | def _real_extract(self, url): | |
224 | mobj = self._match_valid_url(url) | |
225 | video_id = mobj.group('id') | |
226 | svt_id = mobj.group('svt_id') or mobj.group('modal_id') | |
227 | ||
228 | if svt_id: | |
229 | return self._extract_by_video_id(svt_id) | |
230 | ||
231 | webpage = self._download_webpage(url, video_id) | |
232 | ||
233 | data = self._parse_json( | |
234 | self._search_regex( | |
235 | self._SVTPLAY_RE, webpage, 'embedded data', default='{}', | |
236 | group='json'), | |
237 | video_id, fatal=False) | |
238 | ||
239 | thumbnail = self._og_search_thumbnail(webpage) | |
240 | ||
241 | if data: | |
242 | video_info = try_get( | |
243 | data, lambda x: x['context']['dispatcher']['stores']['VideoTitlePageStore']['data']['video'], | |
244 | dict) | |
245 | if video_info: | |
246 | info_dict = self._extract_video(video_info, video_id) | |
247 | info_dict.update({ | |
248 | 'title': data['context']['dispatcher']['stores']['MetaStore']['title'], | |
249 | 'thumbnail': thumbnail, | |
250 | }) | |
251 | return info_dict | |
252 | ||
253 | svt_id = try_get( | |
254 | data, lambda x: x['statistics']['dataLake']['content']['id'], | |
255 | compat_str) | |
256 | ||
257 | if not svt_id: | |
258 | svt_id = self._search_regex( | |
259 | (r'<video[^>]+data-video-id=["\']([\da-zA-Z-]+)', | |
260 | r'<[^>]+\bdata-rt=["\']top-area-play-button["\'][^>]+\bhref=["\'][^"\']*video/%s/[^"\']*\b(?:modalId|id)=([\da-zA-Z-]+)' % re.escape(video_id), | |
261 | r'["\']videoSvtId["\']\s*:\s*["\']([\da-zA-Z-]+)', | |
262 | r'["\']videoSvtId\\?["\']\s*:\s*\\?["\']([\da-zA-Z-]+)', | |
263 | r'"content"\s*:\s*{.*?"id"\s*:\s*"([\da-zA-Z-]+)"', | |
264 | r'["\']svtId["\']\s*:\s*["\']([\da-zA-Z-]+)', | |
265 | r'["\']svtId\\?["\']\s*:\s*\\?["\']([\da-zA-Z-]+)'), | |
266 | webpage, 'video id') | |
267 | ||
268 | info_dict = self._extract_by_video_id(svt_id, webpage) | |
269 | info_dict['thumbnail'] = thumbnail | |
270 | ||
271 | return info_dict | |
272 | ||
273 | ||
274 | class SVTSeriesIE(SVTPlayBaseIE): | |
275 | _VALID_URL = r'https?://(?:www\.)?svtplay\.se/(?P<id>[^/?&#]+)(?:.+?\btab=(?P<season_slug>[^&#]+))?' | |
276 | _TESTS = [{ | |
277 | 'url': 'https://www.svtplay.se/rederiet', | |
278 | 'info_dict': { | |
279 | 'id': '14445680', | |
280 | 'title': 'Rederiet', | |
281 | 'description': 'md5:d9fdfff17f5d8f73468176ecd2836039', | |
282 | }, | |
283 | 'playlist_mincount': 318, | |
284 | }, { | |
285 | 'url': 'https://www.svtplay.se/rederiet?tab=season-2-14445680', | |
286 | 'info_dict': { | |
287 | 'id': 'season-2-14445680', | |
288 | 'title': 'Rederiet - Säsong 2', | |
289 | 'description': 'md5:d9fdfff17f5d8f73468176ecd2836039', | |
290 | }, | |
291 | 'playlist_mincount': 12, | |
292 | }] | |
293 | ||
294 | @classmethod | |
295 | def suitable(cls, url): | |
296 | return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTSeriesIE, cls).suitable(url) | |
297 | ||
298 | def _real_extract(self, url): | |
299 | series_slug, season_id = self._match_valid_url(url).groups() | |
300 | ||
301 | series = self._download_json( | |
302 | 'https://api.svt.se/contento/graphql', series_slug, | |
303 | 'Downloading series page', query={ | |
304 | 'query': '''{ | |
305 | listablesBySlug(slugs: ["%s"]) { | |
306 | associatedContent(include: [productionPeriod, season]) { | |
307 | items { | |
308 | item { | |
309 | ... on Episode { | |
310 | videoSvtId | |
311 | } | |
312 | } | |
313 | } | |
314 | id | |
315 | name | |
316 | } | |
317 | id | |
318 | longDescription | |
319 | name | |
320 | shortDescription | |
321 | } | |
322 | }''' % series_slug, | |
323 | })['data']['listablesBySlug'][0] | |
324 | ||
325 | season_name = None | |
326 | ||
327 | entries = [] | |
328 | for season in series['associatedContent']: | |
329 | if not isinstance(season, dict): | |
330 | continue | |
331 | if season_id: | |
332 | if season.get('id') != season_id: | |
333 | continue | |
334 | season_name = season.get('name') | |
335 | items = season.get('items') | |
336 | if not isinstance(items, list): | |
337 | continue | |
338 | for item in items: | |
339 | video = item.get('item') or {} | |
340 | content_id = video.get('videoSvtId') | |
341 | if not content_id or not isinstance(content_id, compat_str): | |
342 | continue | |
343 | entries.append(self.url_result( | |
344 | 'svt:' + content_id, SVTPlayIE.ie_key(), content_id)) | |
345 | ||
346 | title = series.get('name') | |
347 | season_name = season_name or season_id | |
348 | ||
349 | if title and season_name: | |
350 | title = '%s - %s' % (title, season_name) | |
351 | elif season_id: | |
352 | title = season_id | |
353 | ||
354 | return self.playlist_result( | |
355 | entries, season_id or series.get('id'), title, | |
356 | dict_get(series, ('longDescription', 'shortDescription'))) | |
357 | ||
358 | ||
359 | class SVTPageIE(InfoExtractor): | |
360 | _VALID_URL = r'https?://(?:www\.)?svt\.se/(?P<path>(?:[^/]+/)*(?P<id>[^/?&#]+))' | |
361 | _TESTS = [{ | |
362 | 'url': 'https://www.svt.se/sport/ishockey/bakom-masken-lehners-kamp-mot-mental-ohalsa', | |
363 | 'info_dict': { | |
364 | 'id': '25298267', | |
365 | 'title': 'Bakom masken – Lehners kamp mot mental ohälsa', | |
366 | }, | |
367 | 'playlist_count': 4, | |
368 | }, { | |
369 | 'url': 'https://www.svt.se/nyheter/utrikes/svenska-andrea-ar-en-mil-fran-branderna-i-kalifornien', | |
370 | 'info_dict': { | |
371 | 'id': '24243746', | |
372 | 'title': 'Svenska Andrea redo att fly sitt hem i Kalifornien', | |
373 | }, | |
374 | 'playlist_count': 2, | |
375 | }, { | |
376 | # only programTitle | |
377 | 'url': 'http://www.svt.se/sport/ishockey/jagr-tacklar-giroux-under-intervjun', | |
378 | 'info_dict': { | |
379 | 'id': '8439V2K', | |
380 | 'ext': 'mp4', | |
381 | 'title': 'Stjärnorna skojar till det - under SVT-intervjun', | |
382 | 'duration': 27, | |
383 | 'age_limit': 0, | |
384 | }, | |
385 | }, { | |
386 | 'url': 'https://www.svt.se/nyheter/lokalt/vast/svt-testar-tar-nagon-upp-skrapet-1', | |
387 | 'only_matching': True, | |
388 | }, { | |
389 | 'url': 'https://www.svt.se/vader/manadskronikor/maj2018', | |
390 | 'only_matching': True, | |
391 | }] | |
392 | ||
393 | @classmethod | |
394 | def suitable(cls, url): | |
395 | return False if SVTIE.suitable(url) or SVTPlayIE.suitable(url) else super(SVTPageIE, cls).suitable(url) | |
396 | ||
397 | def _real_extract(self, url): | |
398 | path, display_id = self._match_valid_url(url).groups() | |
399 | ||
400 | article = self._download_json( | |
401 | 'https://api.svt.se/nss-api/page/' + path, display_id, | |
402 | query={'q': 'articles'})['articles']['content'][0] | |
403 | ||
404 | entries = [] | |
405 | ||
406 | def _process_content(content): | |
407 | if content.get('_type') in ('VIDEOCLIP', 'VIDEOEPISODE'): | |
408 | video_id = compat_str(content['image']['svtId']) | |
409 | entries.append(self.url_result( | |
410 | 'svt:' + video_id, SVTPlayIE.ie_key(), video_id)) | |
411 | ||
412 | for media in article.get('media', []): | |
413 | _process_content(media) | |
414 | ||
415 | for obj in article.get('structuredBody', []): | |
416 | _process_content(obj.get('content') or {}) | |
417 | ||
418 | return self.playlist_result( | |
419 | entries, str_or_none(article.get('id')), | |
420 | strip_or_none(article.get('title'))) |