[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / sbs.py
blob8d61e22fce18a366b381631a48ec6944906c767b
1 from .common import InfoExtractor
2 from ..networking import HEADRequest
3 from ..utils import (
4 float_or_none,
5 int_or_none,
6 parse_duration,
7 parse_iso8601,
8 traverse_obj,
9 update_url_query,
10 url_or_none,
14 class SBSIE(InfoExtractor):
15 IE_DESC = 'sbs.com.au'
16 _VALID_URL = r'''(?x)
17 https?://(?:www\.)?sbs\.com\.au/(?:
18 ondemand(?:
19 /video/(?:single/)?|
20 /(?:movie|tv-program)/[^/]+/|
21 /(?:tv|news)-series/(?:[^/]+/){3}|
22 .*?\bplay=|/watch/
23 )|news/(?:embeds/)?video/
24 )(?P<id>[0-9]+)'''
25 _EMBED_REGEX = [r'''(?x)]
26 (?:
27 <meta\s+property="og:video"\s+content=|
28 <iframe[^>]+?src=
30 (["\'])(?P<url>https?://(?:www\.)?sbs\.com\.au/ondemand/video/.+?)\1''']
32 _TESTS = [{
33 # Original URL is handled by the generic IE which finds the iframe:
34 # http://www.sbs.com.au/thefeed/blog/2014/08/21/dingo-conservation
35 'url': 'http://www.sbs.com.au/ondemand/video/single/320403011771/?source=drupal&vertical=thefeed',
36 'md5': '31f84a7a19b53635db63c73f8ab0c4a7',
37 'info_dict': {
38 'id': '320403011771', # '_rFBPRPO4pMR',
39 'ext': 'mp4',
40 'title': 'Dingo Conservation (The Feed)',
41 'description': 'md5:f250a9856fca50d22dec0b5b8015f8a5',
42 'thumbnail': r're:https?://.*\.jpg',
43 'duration': 308,
44 'timestamp': 1408613220,
45 'upload_date': '20140821',
46 'uploader': 'SBSC',
48 'expected_warnings': ['Unable to download JSON metadata'],
49 }, {
50 'url': 'http://www.sbs.com.au/ondemand/video/320403011771/Dingo-Conservation-The-Feed',
51 'only_matching': True,
52 }, {
53 'url': 'http://www.sbs.com.au/news/video/471395907773/The-Feed-July-9',
54 'only_matching': True,
55 }, {
56 'url': 'https://www.sbs.com.au/ondemand/?play=1836638787723',
57 'only_matching': True,
58 }, {
59 'url': 'https://www.sbs.com.au/ondemand/program/inside-windsor-castle?play=1283505731842',
60 'only_matching': True,
61 }, {
62 'url': 'https://www.sbs.com.au/news/embeds/video/1840778819866',
63 'only_matching': True,
64 }, {
65 'url': 'https://www.sbs.com.au/ondemand/watch/1698704451971',
66 'only_matching': True,
67 }, {
68 'url': 'https://www.sbs.com.au/ondemand/movie/coherence/1469404227931',
69 'only_matching': True,
70 }, {
71 'note': 'Live stream',
72 'url': 'https://www.sbs.com.au/ondemand/video/1726824003663/sbs-24x7-live-stream-nsw',
73 'only_matching': True,
74 }, {
75 'url': 'https://www.sbs.com.au/ondemand/news-series/dateline/dateline-2022/dateline-s2022-ep26/2072245827515',
76 'only_matching': True,
77 }, {
78 'url': 'https://www.sbs.com.au/ondemand/tv-series/the-handmaids-tale/season-5/the-handmaids-tale-s5-ep1/2065631811776',
79 'only_matching': True,
80 }, {
81 'url': 'https://www.sbs.com.au/ondemand/tv-program/autun-romes-forgotten-sister/2116212803602',
82 'only_matching': True,
85 _GEO_COUNTRIES = ['AU']
86 _AUS_TV_PARENTAL_GUIDELINES = {
87 'P': 0,
88 'C': 7,
89 'G': 0,
90 'PG': 0,
91 'M': 14,
92 'MA15+': 15,
93 'MAV15+': 15,
94 'R18+': 18,
96 _PLAYER_API = 'https://www.sbs.com.au/api/v3'
98 def _real_extract(self, url):
99 video_id = self._match_id(url)
100 formats, subtitles = self._extract_smil_formats_and_subtitles(
101 update_url_query(f'{self._PLAYER_API}/video_smil', {'id': video_id}), video_id)
103 if not formats:
104 urlh = self._request_webpage(
105 HEADRequest('https://sbs-vod-prod-01.akamaized.net/'), video_id,
106 note='Checking geo-restriction', fatal=False, expected_status=403)
107 if urlh:
108 error_reasons = urlh.headers.get_all('x-error-reason') or []
109 if 'geo-blocked' in error_reasons:
110 self.raise_geo_restricted(countries=['AU'])
111 self.raise_no_formats('No formats are available', video_id=video_id)
113 media = traverse_obj(self._download_json(
114 f'{self._PLAYER_API}/video_stream', video_id, fatal=False,
115 query={'id': video_id, 'context': 'tv'}), ('video_object', {dict})) or {}
117 media.update(self._download_json(
118 f'https://catalogue.pr.sbsod.com/mpx-media/{video_id}',
119 video_id, fatal=not media) or {})
121 # For named episodes, use the catalogue's title to set episode, rather than generic 'Episode N'.
122 if traverse_obj(media, ('partOfSeries', {dict})):
123 media['epName'] = traverse_obj(media, ('title', {str}))
125 return {
126 'id': video_id,
127 **traverse_obj(media, {
128 'title': ('name', {str}),
129 'description': ('description', {str}),
130 'channel': ('taxonomy', 'channel', 'name', {str}),
131 'series': ((('partOfSeries', 'name'), 'seriesTitle'), {str}),
132 'series_id': ((('partOfSeries', 'uuid'), 'seriesID'), {str}),
133 'season_number': ('seasonNumber', {int_or_none}),
134 'episode': ('epName', {str}),
135 'episode_number': ('episodeNumber', {int_or_none}),
136 'timestamp': (('datePublished', ('publication', 'startDate')), {parse_iso8601}),
137 'release_year': ('releaseYear', {int_or_none}),
138 'duration': ('duration', ({float_or_none}, {parse_duration})),
139 'is_live': ('liveStream', {bool}),
140 'age_limit': (('classificationID', 'contentRating'), {str.upper}, {
141 lambda x: self._AUS_TV_PARENTAL_GUIDELINES.get(x)}), # dict.get is unhashable in py3.7
142 }, get_all=False),
143 **traverse_obj(media, {
144 'categories': (('genres', ...), ('taxonomy', ('genre', 'subgenre'), 'name'), {str}),
145 'tags': (('consumerAdviceTexts', ('sbsSubCertification', 'consumerAdvice')), ..., {str}),
146 'thumbnails': ('thumbnails', lambda _, v: url_or_none(v['contentUrl']), {
147 'id': ('name', {str}),
148 'url': 'contentUrl',
149 'width': ('width', {int_or_none}),
150 'height': ('height', {int_or_none}),
153 'formats': formats,
154 'subtitles': subtitles,
155 'uploader': 'SBSC',