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