[ie/dropbox] Fix password-protected video extraction (#11636)
[yt-dlp3.git] / yt_dlp / extractor / yle_areena.py
blobc2daddfa6c37cf3d5ca08c371c613448589cdd67
1 from .common import InfoExtractor
2 from .kaltura import KalturaIE
3 from ..utils import (
4 ExtractorError,
5 int_or_none,
6 parse_iso8601,
7 smuggle_url,
8 url_or_none,
10 from ..utils.traversal import traverse_obj
13 class YleAreenaIE(InfoExtractor):
14 _VALID_URL = r'https?://areena\.yle\.fi/(?P<podcast>podcastit/)?(?P<id>[\d-]+)'
15 _GEO_COUNTRIES = ['FI']
16 _TESTS = [
18 'url': 'https://areena.yle.fi/1-4371942',
19 'md5': 'd87e9a1e74e67e009990ddd413e426b4',
20 'info_dict': {
21 'id': '1-4371942',
22 'ext': 'mp4',
23 'title': 'Pouchit',
24 'description': 'md5:01071d7056ceec375f63960f90c35366',
25 'series': 'Modernit miehet',
26 'season': 'Season 1',
27 'season_number': 1,
28 'episode': 'Episode 2',
29 'episode_number': 2,
30 'thumbnail': r're:https://images\.cdn\.yle\.fi/image/upload/.+\.jpg',
31 'age_limit': 7,
32 'release_date': '20190105',
33 'release_timestamp': 1546725660,
34 'duration': 1435,
38 'url': 'https://areena.yle.fi/1-2158940',
39 'md5': '6369ddc5e07b5fdaeda27a495184143c',
40 'info_dict': {
41 'id': '1-2158940',
42 'ext': 'mp4',
43 'title': 'Albi haluaa vessan',
44 'description': 'Albi haluaa vessan.',
45 'series': 'Albi Lumiukko',
46 'thumbnail': r're:https://images\.cdn\.yle\.fi/image/upload/.+\.jpg',
47 'age_limit': 0,
48 'release_date': '20211215',
49 'release_timestamp': 1639555200,
50 'duration': 319,
54 'url': 'https://areena.yle.fi/1-64829589',
55 'info_dict': {
56 'id': '1-64829589',
57 'ext': 'mp4',
58 'title': 'HKO & Mälkki & Tanner',
59 'description': 'md5:b4f1b1af2c6569b33f75179a86eea156',
60 'series': 'Helsingin kaupunginorkesterin konsertteja',
61 'thumbnail': r're:https://images\.cdn\.yle\.fi/image/upload/.+\.jpg',
62 'release_date': '20230120',
63 'release_timestamp': 1674242079,
64 'duration': 8004,
66 'params': {
67 'skip_download': 'm3u8',
71 'url': 'https://areena.yle.fi/1-72251830',
72 'info_dict': {
73 'id': '1-72251830',
74 'ext': 'mp4',
75 'title': r're:Pentulive 2024 | Pentulive \d{4}-\d{2}-\d{2} \d{2}:\d{2}',
76 'description': 'md5:1f118707d9093bf894a34fbbc865397b',
77 'series': 'Pentulive',
78 'thumbnail': r're:https://images\.cdn\.yle\.fi/image/upload/.+\.jpg',
79 'live_status': 'is_live',
80 'release_date': '20241025',
81 'release_timestamp': 1729875600,
83 'params': {
84 'skip_download': 'livestream',
88 'url': 'https://areena.yle.fi/podcastit/1-71022852',
89 'info_dict': {
90 'id': '1-71022852',
91 'ext': 'mp3',
92 'title': 'Värityspäivä',
93 'description': 'md5:c3a02b0455ec71d32cbe09d32ec161e2',
94 'series': 'Murun ja Paukun ikioma kaupunki',
95 'episode': 'Episode 1',
96 'episode_number': 1,
97 'release_date': '20240607',
98 'release_timestamp': 1717736400,
99 'duration': 442,
104 def _real_extract(self, url):
105 video_id, is_podcast = self._match_valid_url(url).group('id', 'podcast')
106 json_ld = self._search_json_ld(self._download_webpage(url, video_id), video_id, default={})
107 video_data = self._download_json(
108 f'https://player.api.yle.fi/v1/preview/{video_id}.json?app_id=player_static_prod&app_key=8930d72170e48303cf5f3867780d549b',
109 video_id, headers={
110 'origin': 'https://areena.yle.fi',
111 'referer': 'https://areena.yle.fi/',
112 'content-type': 'application/json',
113 })['data']
115 # Example title: 'K1, J2: Pouchit | Modernit miehet'
116 season_number, episode_number, episode, series = self._search_regex(
117 r'K(?P<season_no>\d+),\s*J(?P<episode_no>\d+):?\s*\b(?P<episode>[^|]+)\s*|\s*(?P<series>.+)',
118 json_ld.get('title') or '', 'episode metadata', group=('season_no', 'episode_no', 'episode', 'series'),
119 default=(None, None, None, None))
120 description = traverse_obj(video_data, ('ongoing_ondemand', 'description', 'fin', {str}))
122 subtitles = {}
123 for sub in traverse_obj(video_data, ('ongoing_ondemand', 'subtitles', lambda _, v: url_or_none(v['uri']))):
124 subtitles.setdefault(sub.get('language') or 'und', []).append({
125 'url': sub['uri'],
126 'ext': 'srt',
127 'name': sub.get('kind'),
130 info_dict, metadata = {}, {}
131 if is_podcast and traverse_obj(video_data, ('ongoing_ondemand', 'media_url', {url_or_none})):
132 metadata = video_data['ongoing_ondemand']
133 info_dict['url'] = metadata['media_url']
134 elif traverse_obj(video_data, ('ongoing_event', 'manifest_url', {url_or_none})):
135 metadata = video_data['ongoing_event']
136 metadata.pop('duration', None) # Duration is not accurate for livestreams
137 info_dict['live_status'] = 'is_live'
138 elif traverse_obj(video_data, ('ongoing_ondemand', 'manifest_url', {url_or_none})):
139 metadata = video_data['ongoing_ondemand']
140 # XXX: Has all externally-hosted Kaltura content been moved to native hosting?
141 elif kaltura_id := traverse_obj(video_data, ('ongoing_ondemand', 'kaltura', 'id', {str})):
142 metadata = video_data['ongoing_ondemand']
143 info_dict.update({
144 '_type': 'url_transparent',
145 'url': smuggle_url(f'kaltura:1955031:{kaltura_id}', {'source_url': url}),
146 'ie_key': KalturaIE.ie_key(),
148 elif traverse_obj(video_data, ('gone', {dict})):
149 self.raise_no_formats('The content is no longer available', expected=True, video_id=video_id)
150 metadata = video_data['gone']
151 else:
152 raise ExtractorError('Unable to extract content')
154 if not info_dict.get('url') and metadata.get('manifest_url'):
155 info_dict['formats'], subs = self._extract_m3u8_formats_and_subtitles(
156 metadata['manifest_url'], video_id, 'mp4', m3u8_id='hls')
157 self._merge_subtitles(subs, target=subtitles)
159 return {
160 **traverse_obj(json_ld, {
161 'title': 'title',
162 'thumbnails': ('thumbnails', ..., {'url': 'url'}),
164 'id': video_id,
165 'title': episode,
166 'description': description,
167 'series': series,
168 'season_number': (int_or_none(self._search_regex(r'Kausi (\d+)', description, 'season number', default=None))
169 or int_or_none(season_number)),
170 'episode_number': int_or_none(episode_number),
171 'subtitles': subtitles or None,
172 **traverse_obj(metadata, {
173 'title': ('title', 'fin', {str}),
174 'description': ('description', 'fin', {str}),
175 'series': ('series', 'title', 'fin', {str}),
176 'episode_number': ('episode_number', {int_or_none}),
177 'age_limit': ('content_rating', 'age_restriction', {int_or_none}),
178 'release_timestamp': ('start_time', {parse_iso8601}),
179 'duration': ('duration', 'duration_in_seconds', {int_or_none}),
181 **info_dict,