]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tv2.py
Completely change project name to yt-dlp (#85)
[yt-dlp.git] / yt_dlp / extractor / tv2.py
CommitLineData
dcdb292f 1# coding: utf-8
bc0f937b
S
2from __future__ import unicode_literals
3
588b82bb
S
4import re
5
bc0f937b 6from .common import InfoExtractor
8d3a3a99 7from ..compat import compat_HTTPError
bc0f937b
S
8from ..utils import (
9 determine_ext,
8d3a3a99 10 ExtractorError,
bc0f937b
S
11 int_or_none,
12 float_or_none,
481c5c51 13 js_to_json,
bc0f937b 14 parse_iso8601,
588b82bb 15 remove_end,
8d3a3a99 16 strip_or_none,
9a621ddc 17 try_get,
bc0f937b
S
18)
19
20
21class TV2IE(InfoExtractor):
5886b38d 22 _VALID_URL = r'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
2181983a 23 _TESTS = [{
bc0f937b 24 'url': 'http://www.tv2.no/v/916509/',
bc0f937b
S
25 'info_dict': {
26 'id': '916509',
8d3a3a99 27 'ext': 'flv',
ed1a3905 28 'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
bc0f937b
S
29 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
30 'timestamp': 1431715610,
31 'upload_date': '20150515',
32 'duration': 156.967,
33 'view_count': int,
34 'categories': list,
ed1a3905 35 },
2181983a 36 }]
8d3a3a99
RA
37 _API_DOMAIN = 'sumo.tv2.no'
38 _PROTOCOLS = ('HDS', 'HLS', 'DASH')
39 _GEO_COUNTRIES = ['NO']
bc0f937b
S
40
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
8d3a3a99 43 api_base = 'http://%s/api/web/asset/%s' % (self._API_DOMAIN, video_id)
bc0f937b 44
2181983a 45 asset = self._download_json(
46 api_base + '.json', video_id,
47 'Downloading metadata JSON')['asset']
48 title = asset.get('subtitle') or asset['title']
49 is_live = asset.get('live') is True
50
bc0f937b
S
51 formats = []
52 format_urls = []
8d3a3a99
RA
53 for protocol in self._PROTOCOLS:
54 try:
55 data = self._download_json(
56 api_base + '/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % protocol,
57 video_id, 'Downloading play JSON')['playback']
58 except ExtractorError as e:
59 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
60 error = self._parse_json(e.cause.read().decode(), video_id)['error']
61 error_code = error.get('code')
62 if error_code == 'ASSET_PLAYBACK_INVALID_GEO_LOCATION':
63 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
64 elif error_code == 'SESSION_NOT_AUTHENTICATED':
65 self.raise_login_required()
66 raise ExtractorError(error['description'])
67 raise
9a621ddc
S
68 items = try_get(data, lambda x: x['items']['item'])
69 if not items:
70 continue
71 if not isinstance(items, list):
72 items = [items]
73 for item in items:
74 if not isinstance(item, dict):
75 continue
bc0f937b
S
76 video_url = item.get('url')
77 if not video_url or video_url in format_urls:
78 continue
79 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
80 if not self._is_valid_url(video_url, video_id, format_id):
81 continue
82 format_urls.append(video_url)
83 ext = determine_ext(video_url)
84 if ext == 'f4m':
85 formats.extend(self._extract_f4m_formats(
dbc0b39b 86 video_url, video_id, f4m_id=format_id, fatal=False))
bc0f937b 87 elif ext == 'm3u8':
0b25af9b
RA
88 if not data.get('drmProtected'):
89 formats.extend(self._extract_m3u8_formats(
2181983a 90 video_url, video_id, 'mp4',
91 'm3u8' if is_live else 'm3u8_native',
0b25af9b 92 m3u8_id=format_id, fatal=False))
8d3a3a99
RA
93 elif ext == 'mpd':
94 formats.extend(self._extract_mpd_formats(
95 video_url, video_id, format_id, fatal=False))
bc0f937b
S
96 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
97 pass
98 else:
99 formats.append({
100 'url': video_url,
101 'format_id': format_id,
102 'tbr': int_or_none(item.get('bitrate')),
103 'filesize': int_or_none(item.get('fileSize')),
104 })
0b25af9b
RA
105 if not formats and data.get('drmProtected'):
106 raise ExtractorError('This video is DRM protected.', expected=True)
bc0f937b
S
107 self._sort_formats(formats)
108
bc0f937b
S
109 thumbnails = [{
110 'id': thumbnail.get('@type'),
111 'url': thumbnail.get('url'),
0b25af9b 112 } for _, thumbnail in (asset.get('imageVersions') or {}).items()]
bc0f937b
S
113
114 return {
115 'id': video_id,
116 'url': video_url,
2181983a 117 'title': self._live_title(title) if is_live else title,
8d3a3a99 118 'description': strip_or_none(asset.get('description')),
bc0f937b 119 'thumbnails': thumbnails,
8d3a3a99
RA
120 'timestamp': parse_iso8601(asset.get('createTime')),
121 'duration': float_or_none(asset.get('accurateDuration') or asset.get('duration')),
122 'view_count': int_or_none(asset.get('views')),
123 'categories': asset.get('keywords', '').split(','),
bc0f937b 124 'formats': formats,
2181983a 125 'is_live': is_live,
bc0f937b 126 }
588b82bb
S
127
128
129class TV2ArticleIE(InfoExtractor):
5886b38d 130 _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
588b82bb
S
131 _TESTS = [{
132 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
133 'info_dict': {
134 'id': '6930542',
481c5c51 135 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
8d3a3a99 136 'description': 'De fire siktede nekter fortsatt for å ha stjålet pingvinbabyene, men innrømmer å ha åpnet luken til de små kyllingene.',
588b82bb
S
137 },
138 'playlist_count': 2,
139 }, {
140 'url': 'http://www.tv2.no/a/6930542',
141 'only_matching': True,
142 }]
143
144 def _real_extract(self, url):
145 playlist_id = self._match_id(url)
146
147 webpage = self._download_webpage(url, playlist_id)
148
481c5c51
S
149 # Old embed pattern (looks unused nowadays)
150 assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
151
152 if not assets:
153 # New embed pattern
8d3a3a99 154 for v in re.findall(r'(?s)TV2ContentboxVideo\(({.+?})\)', webpage):
481c5c51
S
155 video = self._parse_json(
156 v, playlist_id, transform_source=js_to_json, fatal=False)
157 if not video:
158 continue
159 asset = video.get('assetId')
160 if asset:
161 assets.append(asset)
162
588b82bb 163 entries = [
481c5c51
S
164 self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
165 for asset_id in assets]
588b82bb
S
166
167 title = remove_end(self._og_search_title(webpage), ' - TV2.no')
168 description = remove_end(self._og_search_description(webpage), ' - TV2.no')
169
170 return self.playlist_result(entries, playlist_id, title, description)
8d3a3a99
RA
171
172
173class KatsomoIE(TV2IE):
2181983a 174 _VALID_URL = r'https?://(?:www\.)?(?:katsomo|mtv(uutiset)?)\.fi/(?:sarja/[0-9a-z-]+-\d+/[0-9a-z-]+-|(?:#!/)?jakso/(?:\d+/[^/]+/)?|video/prog)(?P<id>\d+)'
175 _TESTS = [{
8d3a3a99
RA
176 'url': 'https://www.mtv.fi/sarja/mtv-uutiset-live-33001002003/lahden-pelicans-teki-kovan-ratkaisun-ville-nieminen-pihalle-1181321',
177 'info_dict': {
178 'id': '1181321',
179 'ext': 'mp4',
2181983a 180 'title': 'Lahden Pelicans teki kovan ratkaisun – Ville Nieminen pihalle',
8d3a3a99
RA
181 'description': 'Päätöksen teki Pelicansin hallitus.',
182 'timestamp': 1575116484,
183 'upload_date': '20191130',
184 'duration': 37.12,
185 'view_count': int,
186 'categories': list,
187 },
188 'params': {
189 # m3u8 download
190 'skip_download': True,
191 },
2181983a 192 }, {
193 'url': 'http://www.katsomo.fi/#!/jakso/33001005/studio55-fi/658521/jukka-kuoppamaki-tekee-yha-lauluja-vaikka-lentokoneessa',
194 'only_matching': True,
195 }, {
196 'url': 'https://www.mtvuutiset.fi/video/prog1311159',
197 'only_matching': True,
198 }, {
199 'url': 'https://www.katsomo.fi/#!/jakso/1311159',
200 'only_matching': True,
201 }]
8d3a3a99
RA
202 _API_DOMAIN = 'api.katsomo.fi'
203 _PROTOCOLS = ('HLS', 'MPD')
204 _GEO_COUNTRIES = ['FI']
2181983a 205
206
207class MTVUutisetArticleIE(InfoExtractor):
208 _VALID_URL = r'https?://(?:www\.)mtvuutiset\.fi/artikkeli/[^/]+/(?P<id>\d+)'
209 _TESTS = [{
210 'url': 'https://www.mtvuutiset.fi/artikkeli/tallaisia-vaurioita-viking-amorellassa-on-useamman-osaston-alla-vetta/7931384',
211 'info_dict': {
212 'id': '1311159',
213 'ext': 'mp4',
214 'title': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla',
215 'description': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla',
216 'timestamp': 1600608966,
217 'upload_date': '20200920',
218 'duration': 153.7886666,
219 'view_count': int,
220 'categories': list,
221 },
222 'params': {
223 # m3u8 download
224 'skip_download': True,
225 },
226 }, {
227 # multiple Youtube embeds
228 'url': 'https://www.mtvuutiset.fi/artikkeli/50-vuotta-subarun-vastaiskua/6070962',
229 'only_matching': True,
230 }]
231
232 def _real_extract(self, url):
233 article_id = self._match_id(url)
234 article = self._download_json(
235 'http://api.mtvuutiset.fi/mtvuutiset/api/json/' + article_id,
236 article_id)
237
238 def entries():
239 for video in (article.get('videos') or []):
240 video_type = video.get('videotype')
241 video_url = video.get('url')
242 if not (video_url and video_type in ('katsomo', 'youtube')):
243 continue
244 yield self.url_result(
245 video_url, video_type.capitalize(), video.get('video_id'))
246
247 return self.playlist_result(
248 entries(), article_id, article.get('title'), article.get('description'))