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