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