]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/ruutu.py
[cleanup, tmz] Update tests (#3654)
[yt-dlp.git] / yt_dlp / extractor / ruutu.py
CommitLineData
0a8a7e68
TI
1import json
2import re
3
22354455
HL
4from .common import InfoExtractor
5from ..compat import compat_urllib_parse_urlparse
9414338a
S
6from ..utils import (
7 determine_ext,
db75f14d 8 ExtractorError,
29f7c58a 9 find_xpath_attr,
9414338a 10 int_or_none,
0a8a7e68
TI
11 traverse_obj,
12 try_call,
29f7c58a 13 unified_strdate,
14 url_or_none,
22889ab1 15 xpath_attr,
9414338a
S
16 xpath_text,
17)
22354455
HL
18
19
20class RuutuIE(InfoExtractor):
29f7c58a 21 _VALID_URL = r'''(?x)
22 https?://
23 (?:
24 (?:www\.)?(?:ruutu|supla)\.fi/(?:video|supla|audio)/|
25 static\.nelonenmedia\.fi/player/misc/embed_player\.html\?.*?\bnid=
26 )
27 (?P<id>\d+)
28 '''
22354455
HL
29 _TESTS = [
30 {
22889ab1 31 'url': 'http://www.ruutu.fi/video/2058907',
22354455
HL
32 'md5': 'ab2093f39be1ca8581963451b3c0234f',
33 'info_dict': {
9414338a 34 'id': '2058907',
22354455
HL
35 'ext': 'mp4',
36 'title': 'Oletko aina halunnut tietää mitä tapahtuu vain hetki ennen lähetystä? - Nyt se selvisi!',
9414338a 37 'description': 'md5:cfc6ccf0e57a814360df464a91ff67d6',
ec85ded8 38 'thumbnail': r're:^https?://.*\.jpg$',
9414338a
S
39 'duration': 114,
40 'age_limit': 0,
22354455 41 },
22354455
HL
42 },
43 {
c482b3c6 44 'url': 'http://www.ruutu.fi/video/2057306',
22354455
HL
45 'md5': '065a10ae4d5b8cfd9d0c3d332465e3d9',
46 'info_dict': {
9414338a 47 'id': '2057306',
22354455
HL
48 'ext': 'mp4',
49 'title': 'Superpesis: katso koko kausi Ruudussa',
73498a89 50 'description': 'md5:bfb7336df2a12dc21d18fa696c9f8f23',
ec85ded8 51 'thumbnail': r're:^https?://.*\.jpg$',
9414338a
S
52 'duration': 40,
53 'age_limit': 0,
22354455 54 },
22354455 55 },
73498a89 56 {
57 'url': 'http://www.supla.fi/supla/2231370',
58 'md5': 'df14e782d49a2c0df03d3be2a54ef949',
59 'info_dict': {
60 'id': '2231370',
61 'ext': 'mp4',
62 'title': 'Osa 1: Mikael Jungner',
63 'description': 'md5:7d90f358c47542e3072ff65d7b1bcffe',
ec85ded8 64 'thumbnail': r're:^https?://.*\.jpg$',
73498a89 65 'age_limit': 0,
66 },
67 },
2e25f80d
TV
68 # Episode where <SourceFile> is "NOT-USED", but has other
69 # downloadable sources available.
70 {
71 'url': 'http://www.ruutu.fi/video/3193728',
72 'only_matching': True,
73 },
8410653f
S
74 {
75 # audio podcast
76 'url': 'https://www.supla.fi/supla/3382410',
77 'md5': 'b9d7155fed37b2ebf6021d74c4b8e908',
78 'info_dict': {
79 'id': '3382410',
80 'ext': 'mp3',
81 'title': 'Mikä ihmeen poltergeist?',
82 'description': 'md5:bbb6963df17dfd0ecd9eb9a61bf14b52',
83 'thumbnail': r're:^https?://.*\.jpg$',
84 'age_limit': 0,
85 },
29f7c58a 86 'expected_warnings': [
87 'HTTP Error 502: Bad Gateway',
88 'Failed to download m3u8 information',
89 ],
90 },
91 {
92 'url': 'http://www.supla.fi/audio/2231370',
93 'only_matching': True,
94 },
95 {
96 'url': 'https://static.nelonenmedia.fi/player/misc/embed_player.html?nid=3618790',
97 'only_matching': True,
98 },
99 {
100 # episode
101 'url': 'https://www.ruutu.fi/video/3401964',
102 'info_dict': {
103 'id': '3401964',
104 'ext': 'mp4',
105 'title': 'Temptation Island Suomi - Kausi 5 - Jakso 17',
106 'description': 'md5:87cf01d5e1e88adf0c8a2937d2bd42ba',
107 'thumbnail': r're:^https?://.*\.jpg$',
108 'duration': 2582,
109 'age_limit': 12,
110 'upload_date': '20190508',
111 'series': 'Temptation Island Suomi',
112 'season_number': 5,
113 'episode_number': 17,
114 'categories': ['Reality ja tositapahtumat', 'Kotimaiset suosikit', 'Romantiikka ja parisuhde'],
115 },
116 'params': {
117 'skip_download': True,
118 },
119 },
120 {
121 # premium
122 'url': 'https://www.ruutu.fi/video/3618715',
123 'only_matching': True,
124 },
22354455 125 ]
29f7c58a 126 _API_BASE = 'https://gatling.nelonenmedia.fi'
22354455 127
0a8a7e68
TI
128 @classmethod
129 def _extract_url(cls, webpage):
130 settings = try_call(
131 lambda: json.loads(re.search(
132 r'jQuery\.extend\(Drupal\.settings, ({.+?})\);', webpage).group(1), strict=False))
133 video_id = traverse_obj(settings, (
134 'mediaCrossbowSettings', 'file', 'field_crossbow_video_id', 'und', 0, 'value'))
135 if video_id:
136 return f'http://www.ruutu.fi/video/{video_id}'
137
22354455 138 def _real_extract(self, url):
22889ab1 139 video_id = self._match_id(url)
22354455 140
22889ab1 141 video_xml = self._download_xml(
29f7c58a 142 '%s/media-xml-cache' % self._API_BASE, video_id,
cab26223 143 query={'id': video_id})
22354455
HL
144
145 formats = []
9414338a
S
146 processed_urls = []
147
148 def extract_formats(node):
149 for child in node:
150 if child.tag.endswith('Files'):
151 extract_formats(child)
152 elif child.tag.endswith('File'):
153 video_url = child.text
3089bc74
S
154 if (not video_url or video_url in processed_urls
155 or any(p in video_url for p in ('NOT_USED', 'NOT-USED'))):
2e25f80d 156 continue
9414338a
S
157 processed_urls.append(video_url)
158 ext = determine_ext(video_url)
29f7c58a 159 auth_video_url = url_or_none(self._download_webpage(
160 '%s/auth/access/v2' % self._API_BASE, video_id,
161 note='Downloading authenticated %s stream URL' % ext,
162 fatal=False, query={'stream': video_url}))
163 if auth_video_url:
164 processed_urls.append(auth_video_url)
165 video_url = auth_video_url
9414338a 166 if ext == 'm3u8':
7e5edcfd 167 formats.extend(self._extract_m3u8_formats(
29f7c58a 168 video_url, video_id, 'mp4',
169 entry_protocol='m3u8_native', m3u8_id='hls',
170 fatal=False))
9414338a 171 elif ext == 'f4m':
7e5edcfd
S
172 formats.extend(self._extract_f4m_formats(
173 video_url, video_id, f4m_id='hds', fatal=False))
dadb8361 174 elif ext == 'mpd':
4d345bf1
S
175 # video-only and audio-only streams are of different
176 # duration resulting in out of sync issue
177 continue
dadb8361
RA
178 formats.extend(self._extract_mpd_formats(
179 video_url, video_id, mpd_id='dash', fatal=False))
8410653f
S
180 elif ext == 'mp3' or child.tag == 'AudioMediaFile':
181 formats.append({
182 'format_id': 'audio',
183 'url': video_url,
184 'vcodec': 'none',
185 })
9414338a
S
186 else:
187 proto = compat_urllib_parse_urlparse(video_url).scheme
188 if not child.tag.startswith('HTTP') and proto != 'rtmp':
189 continue
190 preference = -1 if proto == 'rtmp' else 1
191 label = child.get('label')
192 tbr = int_or_none(child.get('bitrate'))
76a353c9
S
193 format_id = '%s-%s' % (proto, label if label else tbr) if label or tbr else proto
194 if not self._is_valid_url(video_url, video_id, format_id):
195 continue
59a9efe8 196 width, height = [int_or_none(x) for x in child.get('resolution', 'x').split('x')[:2]]
9414338a 197 formats.append({
76a353c9 198 'format_id': format_id,
9414338a
S
199 'url': video_url,
200 'width': width,
201 'height': height,
202 'tbr': tbr,
203 'preference': preference,
204 })
22354455 205
9414338a 206 extract_formats(video_xml.find('./Clip'))
db75f14d 207
29f7c58a 208 def pv(name):
209 node = find_xpath_attr(
210 video_xml, './Clip/PassthroughVariables/variable', 'name', name)
211 if node is not None:
212 return node.get('value')
213
214 if not formats:
a06916d9 215 if (not self.get_param('allow_unplayable_formats')
06869367 216 and xpath_text(video_xml, './Clip/DRM', default=None)):
88acdbc2 217 self.report_drm(video_id)
29f7c58a 218 ns_st_cds = pv('ns_st_cds')
219 if ns_st_cds != 'free':
220 raise ExtractorError('This video is %s.' % ns_st_cds, expected=True)
db75f14d 221
22354455
HL
222 self._sort_formats(formats)
223
29f7c58a 224 themes = pv('themes')
225
22354455
HL
226 return {
227 'id': video_id,
22889ab1
S
228 'title': xpath_attr(video_xml, './/Behavior/Program', 'program_name', 'title', fatal=True),
229 'description': xpath_attr(video_xml, './/Behavior/Program', 'description', 'description'),
230 'thumbnail': xpath_attr(video_xml, './/Behavior/Startpicture', 'href', 'thumbnail'),
29f7c58a 231 'duration': int_or_none(xpath_text(video_xml, './/Runtime', 'duration')) or int_or_none(pv('runtime')),
9414338a 232 'age_limit': int_or_none(xpath_text(video_xml, './/AgeLimit', 'age limit')),
29f7c58a 233 'upload_date': unified_strdate(pv('date_start')),
234 'series': pv('series_name'),
235 'season_number': int_or_none(pv('season_number')),
236 'episode_number': int_or_none(pv('episode_number')),
237 'categories': themes.split(',') if themes else [],
9414338a 238 'formats': formats,
22354455 239 }