]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
bc0f937b S |
2 | from __future__ import unicode_literals |
3 | ||
588b82bb S |
4 | import re |
5 | ||
bc0f937b | 6 | from .common import InfoExtractor |
8d3a3a99 | 7 | from ..compat import compat_HTTPError |
bc0f937b S |
8 | from ..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 | ||
21 | class TV2IE(InfoExtractor): | |
e5d731f3 | 22 | _VALID_URL = r'https?://(?:www\.)?tv2\.no/v\d*/(?P<id>\d+)' |
2181983a | 23 | _TESTS = [{ |
bc0f937b | 24 | 'url': 'http://www.tv2.no/v/916509/', |
bc0f937b S |
25 | 'info_dict': { |
26 | 'id': '916509', | |
ea81966e | 27 | 'ext': 'mp4', |
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', | |
ea81966e | 32 | 'duration': 157, |
bc0f937b S |
33 | 'view_count': int, |
34 | 'categories': list, | |
ed1a3905 | 35 | }, |
e5d731f3 | 36 | }, { |
37 | 'url': 'http://www.tv2.no/v2/916509', | |
38 | 'only_matching': True, | |
2181983a | 39 | }] |
ea81966e | 40 | _PROTOCOLS = ('HLS', 'DASH') |
8d3a3a99 | 41 | _GEO_COUNTRIES = ['NO'] |
bc0f937b S |
42 | |
43 | def _real_extract(self, url): | |
44 | video_id = self._match_id(url) | |
ea81966e A |
45 | asset = self._download_json('https://sumo.tv2.no/rest/assets/' + video_id, video_id, |
46 | 'Downloading metadata JSON') | |
47 | title = asset['title'] | |
2181983a | 48 | is_live = asset.get('live') is True |
49 | ||
bc0f937b S |
50 | formats = [] |
51 | format_urls = [] | |
8d3a3a99 RA |
52 | for protocol in self._PROTOCOLS: |
53 | try: | |
ea81966e A |
54 | data = self._download_json('https://api.sumo.tv2.no/play/%s?stream=%s' % (video_id, protocol), |
55 | video_id, 'Downloading playabck JSON', | |
56 | headers={'content-type': 'application/json'}, | |
57 | data='{"device":{"id":"1-1-1","name":"Nettleser (HTML)"}}'.encode())['playback'] | |
8d3a3a99 RA |
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 | |
ea81966e | 68 | items = data.get('streams', []) |
9a621ddc | 69 | for item in items: |
bc0f937b S |
70 | video_url = item.get('url') |
71 | if not video_url or video_url in format_urls: | |
72 | continue | |
ea81966e | 73 | format_id = '%s-%s' % (protocol.lower(), item.get('type')) |
bc0f937b S |
74 | if not self._is_valid_url(video_url, video_id, format_id): |
75 | continue | |
76 | format_urls.append(video_url) | |
77 | ext = determine_ext(video_url) | |
78 | if ext == 'f4m': | |
79 | formats.extend(self._extract_f4m_formats( | |
dbc0b39b | 80 | video_url, video_id, f4m_id=format_id, fatal=False)) |
bc0f937b | 81 | elif ext == 'm3u8': |
0b25af9b RA |
82 | if not data.get('drmProtected'): |
83 | formats.extend(self._extract_m3u8_formats( | |
2181983a | 84 | video_url, video_id, 'mp4', |
85 | 'm3u8' if is_live else 'm3u8_native', | |
0b25af9b | 86 | m3u8_id=format_id, fatal=False)) |
8d3a3a99 RA |
87 | elif ext == 'mpd': |
88 | formats.extend(self._extract_mpd_formats( | |
89 | video_url, video_id, format_id, fatal=False)) | |
bc0f937b S |
90 | elif ext == 'ism' or video_url.endswith('.ism/Manifest'): |
91 | pass | |
92 | else: | |
93 | formats.append({ | |
94 | 'url': video_url, | |
95 | 'format_id': format_id, | |
bc0f937b | 96 | }) |
0b25af9b | 97 | if not formats and data.get('drmProtected'): |
88acdbc2 | 98 | self.report_drm(video_id) |
bc0f937b S |
99 | self._sort_formats(formats) |
100 | ||
bc0f937b | 101 | thumbnails = [{ |
ea81966e A |
102 | 'id': type, |
103 | 'url': thumb_url, | |
104 | } for type, thumb_url in (asset.get('images') or {}).items()] | |
bc0f937b S |
105 | |
106 | return { | |
107 | 'id': video_id, | |
108 | 'url': video_url, | |
39ca3b5c | 109 | 'title': title, |
8d3a3a99 | 110 | 'description': strip_or_none(asset.get('description')), |
bc0f937b | 111 | 'thumbnails': thumbnails, |
ea81966e | 112 | 'timestamp': parse_iso8601(asset.get('live_broadcast_time') or asset.get('update_time')), |
8d3a3a99 RA |
113 | 'duration': float_or_none(asset.get('accurateDuration') or asset.get('duration')), |
114 | 'view_count': int_or_none(asset.get('views')), | |
ea81966e | 115 | 'categories': asset.get('tags', '').split(','), |
bc0f937b | 116 | 'formats': formats, |
2181983a | 117 | 'is_live': is_live, |
bc0f937b | 118 | } |
588b82bb S |
119 | |
120 | ||
121 | class TV2ArticleIE(InfoExtractor): | |
5886b38d | 122 | _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)' |
588b82bb S |
123 | _TESTS = [{ |
124 | 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542', | |
125 | 'info_dict': { | |
126 | 'id': '6930542', | |
481c5c51 | 127 | 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret', |
8d3a3a99 | 128 | 'description': 'De fire siktede nekter fortsatt for å ha stjålet pingvinbabyene, men innrømmer å ha åpnet luken til de små kyllingene.', |
588b82bb S |
129 | }, |
130 | 'playlist_count': 2, | |
131 | }, { | |
132 | 'url': 'http://www.tv2.no/a/6930542', | |
133 | 'only_matching': True, | |
134 | }] | |
135 | ||
136 | def _real_extract(self, url): | |
137 | playlist_id = self._match_id(url) | |
138 | ||
139 | webpage = self._download_webpage(url, playlist_id) | |
140 | ||
481c5c51 S |
141 | # Old embed pattern (looks unused nowadays) |
142 | assets = re.findall(r'data-assetid=["\'](\d+)', webpage) | |
143 | ||
144 | if not assets: | |
145 | # New embed pattern | |
8d3a3a99 | 146 | for v in re.findall(r'(?s)TV2ContentboxVideo\(({.+?})\)', webpage): |
481c5c51 S |
147 | video = self._parse_json( |
148 | v, playlist_id, transform_source=js_to_json, fatal=False) | |
149 | if not video: | |
150 | continue | |
151 | asset = video.get('assetId') | |
152 | if asset: | |
153 | assets.append(asset) | |
154 | ||
588b82bb | 155 | entries = [ |
481c5c51 S |
156 | self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2') |
157 | for asset_id in assets] | |
588b82bb S |
158 | |
159 | title = remove_end(self._og_search_title(webpage), ' - TV2.no') | |
160 | description = remove_end(self._og_search_description(webpage), ' - TV2.no') | |
161 | ||
162 | return self.playlist_result(entries, playlist_id, title, description) | |
8d3a3a99 RA |
163 | |
164 | ||
ea81966e | 165 | class KatsomoIE(InfoExtractor): |
2181983a | 166 | _VALID_URL = r'https?://(?:www\.)?(?:katsomo|mtv(uutiset)?)\.fi/(?:sarja/[0-9a-z-]+-\d+/[0-9a-z-]+-|(?:#!/)?jakso/(?:\d+/[^/]+/)?|video/prog)(?P<id>\d+)' |
167 | _TESTS = [{ | |
8d3a3a99 RA |
168 | 'url': 'https://www.mtv.fi/sarja/mtv-uutiset-live-33001002003/lahden-pelicans-teki-kovan-ratkaisun-ville-nieminen-pihalle-1181321', |
169 | 'info_dict': { | |
170 | 'id': '1181321', | |
171 | 'ext': 'mp4', | |
2181983a | 172 | 'title': 'Lahden Pelicans teki kovan ratkaisun – Ville Nieminen pihalle', |
8d3a3a99 RA |
173 | 'description': 'Päätöksen teki Pelicansin hallitus.', |
174 | 'timestamp': 1575116484, | |
175 | 'upload_date': '20191130', | |
176 | 'duration': 37.12, | |
177 | 'view_count': int, | |
178 | 'categories': list, | |
179 | }, | |
180 | 'params': { | |
181 | # m3u8 download | |
182 | 'skip_download': True, | |
183 | }, | |
2181983a | 184 | }, { |
185 | 'url': 'http://www.katsomo.fi/#!/jakso/33001005/studio55-fi/658521/jukka-kuoppamaki-tekee-yha-lauluja-vaikka-lentokoneessa', | |
186 | 'only_matching': True, | |
187 | }, { | |
188 | 'url': 'https://www.mtvuutiset.fi/video/prog1311159', | |
189 | 'only_matching': True, | |
190 | }, { | |
191 | 'url': 'https://www.katsomo.fi/#!/jakso/1311159', | |
192 | 'only_matching': True, | |
193 | }] | |
8d3a3a99 RA |
194 | _API_DOMAIN = 'api.katsomo.fi' |
195 | _PROTOCOLS = ('HLS', 'MPD') | |
196 | _GEO_COUNTRIES = ['FI'] | |
2181983a | 197 | |
ea81966e A |
198 | def _real_extract(self, url): |
199 | video_id = self._match_id(url) | |
200 | api_base = 'http://%s/api/web/asset/%s' % (self._API_DOMAIN, video_id) | |
201 | ||
202 | asset = self._download_json( | |
203 | api_base + '.json', video_id, | |
204 | 'Downloading metadata JSON')['asset'] | |
205 | title = asset.get('subtitle') or asset['title'] | |
206 | is_live = asset.get('live') is True | |
207 | ||
208 | formats = [] | |
209 | format_urls = [] | |
210 | for protocol in self._PROTOCOLS: | |
211 | try: | |
212 | data = self._download_json( | |
213 | api_base + '/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % protocol, | |
214 | video_id, 'Downloading play JSON')['playback'] | |
215 | except ExtractorError as e: | |
216 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401: | |
217 | error = self._parse_json(e.cause.read().decode(), video_id)['error'] | |
218 | error_code = error.get('code') | |
219 | if error_code == 'ASSET_PLAYBACK_INVALID_GEO_LOCATION': | |
220 | self.raise_geo_restricted(countries=self._GEO_COUNTRIES) | |
221 | elif error_code == 'SESSION_NOT_AUTHENTICATED': | |
222 | self.raise_login_required() | |
223 | raise ExtractorError(error['description']) | |
224 | raise | |
225 | items = try_get(data, lambda x: x['items']['item']) | |
226 | if not items: | |
227 | continue | |
228 | if not isinstance(items, list): | |
229 | items = [items] | |
230 | for item in items: | |
231 | if not isinstance(item, dict): | |
232 | continue | |
233 | video_url = item.get('url') | |
234 | if not video_url or video_url in format_urls: | |
235 | continue | |
236 | format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat')) | |
237 | if not self._is_valid_url(video_url, video_id, format_id): | |
238 | continue | |
239 | format_urls.append(video_url) | |
240 | ext = determine_ext(video_url) | |
241 | if ext == 'f4m': | |
242 | formats.extend(self._extract_f4m_formats( | |
243 | video_url, video_id, f4m_id=format_id, fatal=False)) | |
244 | elif ext == 'm3u8': | |
245 | if not data.get('drmProtected'): | |
246 | formats.extend(self._extract_m3u8_formats( | |
247 | video_url, video_id, 'mp4', | |
248 | 'm3u8' if is_live else 'm3u8_native', | |
249 | m3u8_id=format_id, fatal=False)) | |
250 | elif ext == 'mpd': | |
251 | formats.extend(self._extract_mpd_formats( | |
252 | video_url, video_id, format_id, fatal=False)) | |
253 | elif ext == 'ism' or video_url.endswith('.ism/Manifest'): | |
254 | pass | |
255 | else: | |
256 | formats.append({ | |
257 | 'url': video_url, | |
258 | 'format_id': format_id, | |
259 | 'tbr': int_or_none(item.get('bitrate')), | |
260 | 'filesize': int_or_none(item.get('fileSize')), | |
261 | }) | |
262 | if not formats and data.get('drmProtected'): | |
263 | self.report_drm(video_id) | |
264 | self._sort_formats(formats) | |
265 | ||
266 | thumbnails = [{ | |
267 | 'id': thumbnail.get('@type'), | |
268 | 'url': thumbnail.get('url'), | |
269 | } for _, thumbnail in (asset.get('imageVersions') or {}).items()] | |
270 | ||
271 | return { | |
272 | 'id': video_id, | |
273 | 'url': video_url, | |
39ca3b5c | 274 | 'title': title, |
ea81966e A |
275 | 'description': strip_or_none(asset.get('description')), |
276 | 'thumbnails': thumbnails, | |
277 | 'timestamp': parse_iso8601(asset.get('createTime')), | |
278 | 'duration': float_or_none(asset.get('accurateDuration') or asset.get('duration')), | |
279 | 'view_count': int_or_none(asset.get('views')), | |
280 | 'categories': asset.get('keywords', '').split(','), | |
281 | 'formats': formats, | |
282 | 'is_live': is_live, | |
283 | } | |
284 | ||
2181983a | 285 | |
286 | class MTVUutisetArticleIE(InfoExtractor): | |
287 | _VALID_URL = r'https?://(?:www\.)mtvuutiset\.fi/artikkeli/[^/]+/(?P<id>\d+)' | |
288 | _TESTS = [{ | |
289 | 'url': 'https://www.mtvuutiset.fi/artikkeli/tallaisia-vaurioita-viking-amorellassa-on-useamman-osaston-alla-vetta/7931384', | |
290 | 'info_dict': { | |
291 | 'id': '1311159', | |
292 | 'ext': 'mp4', | |
293 | 'title': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla', | |
294 | 'description': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla', | |
295 | 'timestamp': 1600608966, | |
296 | 'upload_date': '20200920', | |
297 | 'duration': 153.7886666, | |
298 | 'view_count': int, | |
299 | 'categories': list, | |
300 | }, | |
301 | 'params': { | |
302 | # m3u8 download | |
303 | 'skip_download': True, | |
304 | }, | |
305 | }, { | |
306 | # multiple Youtube embeds | |
307 | 'url': 'https://www.mtvuutiset.fi/artikkeli/50-vuotta-subarun-vastaiskua/6070962', | |
308 | 'only_matching': True, | |
309 | }] | |
310 | ||
311 | def _real_extract(self, url): | |
312 | article_id = self._match_id(url) | |
313 | article = self._download_json( | |
314 | 'http://api.mtvuutiset.fi/mtvuutiset/api/json/' + article_id, | |
315 | article_id) | |
316 | ||
317 | def entries(): | |
318 | for video in (article.get('videos') or []): | |
319 | video_type = video.get('videotype') | |
320 | video_url = video.get('url') | |
321 | if not (video_url and video_type in ('katsomo', 'youtube')): | |
322 | continue | |
323 | yield self.url_result( | |
324 | video_url, video_type.capitalize(), video.get('video_id')) | |
325 | ||
326 | return self.playlist_result( | |
327 | entries(), article_id, article.get('title'), article.get('description')) |