3 from .common
import InfoExtractor
4 from ..compat
import compat_HTTPError
18 class TV2IE(InfoExtractor
):
19 _VALID_URL
= r
'https?://(?:www\.)?tv2\.no/v(?:ideo)?\d*/(?:[^?#]+/)*(?P<id>\d+)'
21 'url': 'http://www.tv2.no/v/1791207/',
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',
30 'thumbnail': r
're:^https://.*$',
35 'url': 'http://www.tv2.no/v2/916509',
36 'only_matching': True,
38 'url': 'https://www.tv2.no/video/nyhetene/her-kolliderer-romsonden-med-asteroiden/1791207/',
39 'only_matching': True,
41 _PROTOCOLS
= ('HLS', 'DASH')
42 _GEO_COUNTRIES
= ['NO']
44 def _real_extract(self
, url
):
45 video_id
= self
._match
_id
(url
)
46 asset
= self
._download
_json
('https://sumo.tv2.no/rest/assets/' + video_id
, video_id
,
47 'Downloading metadata JSON')
48 title
= asset
['title']
49 is_live
= asset
.get('live') is True
53 for protocol
in self
._PROTOCOLS
:
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']
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'])
69 items
= data
.get('streams', [])
71 video_url
= item
.get('url')
72 if not video_url
or video_url
in format_urls
:
74 format_id
= '%s-%s' % (protocol
.lower(), item
.get('type'))
75 if not self
._is
_valid
_url
(video_url
, video_id
, format_id
):
77 format_urls
.append(video_url
)
78 ext
= determine_ext(video_url
)
80 formats
.extend(self
._extract
_f
4m
_formats
(
81 video_url
, video_id
, f4m_id
=format_id
, fatal
=False))
83 if not data
.get('drmProtected'):
84 formats
.extend(self
._extract
_m
3u8_formats
(
85 video_url
, video_id
, 'mp4', live
=is_live
, m3u8_id
=format_id
, fatal
=False))
87 formats
.extend(self
._extract
_mpd
_formats
(
88 video_url
, video_id
, format_id
, fatal
=False))
89 elif ext
== 'ism' or video_url
.endswith('.ism/Manifest'):
94 'format_id': format_id
,
96 if not formats
and data
.get('drmProtected'):
97 self
.report_drm(video_id
)
102 } for type, thumb_url
in (asset
.get('images') or {}).items()]
108 'description': strip_or_none(asset
.get('description')),
109 'thumbnails': thumbnails
,
110 'timestamp': parse_iso8601(asset
.get('live_broadcast_time') or asset
.get('update_time')),
111 'duration': float_or_none(asset
.get('accurateDuration') or asset
.get('duration')),
112 'view_count': int_or_none(asset
.get('views')),
113 'categories': asset
.get('tags', '').split(','),
119 class TV2ArticleIE(InfoExtractor
):
120 _VALID_URL
= r
'https?://(?:www\.)?tv2\.no/(?!v(?:ideo)?\d*/)[^?#]+/(?P<id>\d+)'
122 'url': 'https://www.tv2.no/underholdning/forraeder/katarina-flatland-angrer-etter-forraeder-exit/15095188/',
125 'title': 'Katarina Flatland angrer etter Forræder-exit',
126 'description': 'SANDEFJORD (TV 2): Katarina Flatland (33) måtte følge i sine fars fotspor, da hun ble forvist fra Forræder.',
130 'url': 'http://www.tv2.no/a/6930542',
131 'only_matching': True,
134 def _real_extract(self
, url
):
135 playlist_id
= self
._match
_id
(url
)
137 webpage
= self
._download
_webpage
(url
, playlist_id
)
139 # Old embed pattern (looks unused nowadays)
140 assets
= re
.findall(r
'data-assetid=["\'](\d
+)', webpage)
144 for v in re.findall(r'(?s
)(?
:TV2ContentboxVideo|TV2\
.TV2Video
)\
(({.+?}
)\
)', webpage):
145 video = self._parse_json(
146 v, playlist_id, transform_source=js_to_json, fatal=False)
149 asset = video.get('assetId
')
154 self.url_result('http
://www
.tv2
.no
/v
/%s' % asset_id, 'TV2
')
155 for asset_id in assets]
157 title = remove_end(self._og_search_title(webpage), ' - TV2
.no
')
158 description = remove_end(self._og_search_description(webpage), ' - TV2
.no
')
160 return self.playlist_result(entries, playlist_id, title, description)
163 class KatsomoIE(InfoExtractor):
164 _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 'url': 'https://www.mtv.fi/sarja/mtv-uutiset-live-33001002003/lahden-pelicans-teki-kovan-ratkaisun-ville-nieminen-pihalle-1181321',
170 'title': 'Lahden Pelicans teki kovan ratkaisun – Ville Nieminen pihalle',
171 'description': 'Päätöksen teki Pelicansin hallitus.',
172 'timestamp': 1575116484,
173 'upload_date': '20191130',
180 'skip_download': True,
183 'url': 'http://www.katsomo.fi/#!/jakso/33001005/studio55-fi/658521/jukka-kuoppamaki-tekee-yha-lauluja-vaikka-lentokoneessa',
184 'only_matching': True,
186 'url': 'https://www.mtvuutiset.fi/video/prog1311159',
187 'only_matching': True,
189 'url': 'https://www.katsomo.fi/#!/jakso/1311159',
190 'only_matching': True,
192 _API_DOMAIN
= 'api.katsomo.fi'
193 _PROTOCOLS
= ('HLS', 'MPD')
194 _GEO_COUNTRIES
= ['FI']
196 def _real_extract(self
, url
):
197 video_id
= self
._match
_id
(url
)
198 api_base
= 'http://%s/api/web/asset/%s' % (self
._API
_DOMAIN
, video_id
)
200 asset
= self
._download
_json
(
201 api_base
+ '.json', video_id
,
202 'Downloading metadata JSON')['asset']
203 title
= asset
.get('subtitle') or asset
['title']
204 is_live
= asset
.get('live') is True
208 for protocol
in self
._PROTOCOLS
:
210 data
= self
._download
_json
(
211 api_base
+ '/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % protocol
,
212 video_id
, 'Downloading play JSON')['playback']
213 except ExtractorError
as e
:
214 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 401:
215 error
= self
._parse
_json
(e
.cause
.read().decode(), video_id
)['error']
216 error_code
= error
.get('code')
217 if error_code
== 'ASSET_PLAYBACK_INVALID_GEO_LOCATION':
218 self
.raise_geo_restricted(countries
=self
._GEO
_COUNTRIES
)
219 elif error_code
== 'SESSION_NOT_AUTHENTICATED':
220 self
.raise_login_required()
221 raise ExtractorError(error
['description'])
223 items
= try_get(data
, lambda x
: x
['items']['item'])
226 if not isinstance(items
, list):
229 if not isinstance(item
, dict):
231 video_url
= item
.get('url')
232 if not video_url
or video_url
in format_urls
:
234 format_id
= '%s-%s' % (protocol
.lower(), item
.get('mediaFormat'))
235 if not self
._is
_valid
_url
(video_url
, video_id
, format_id
):
237 format_urls
.append(video_url
)
238 ext
= determine_ext(video_url
)
240 formats
.extend(self
._extract
_f
4m
_formats
(
241 video_url
, video_id
, f4m_id
=format_id
, fatal
=False))
243 if not data
.get('drmProtected'):
244 formats
.extend(self
._extract
_m
3u8_formats
(
245 video_url
, video_id
, 'mp4', live
=is_live
, m3u8_id
=format_id
, fatal
=False))
247 formats
.extend(self
._extract
_mpd
_formats
(
248 video_url
, video_id
, format_id
, fatal
=False))
249 elif ext
== 'ism' or video_url
.endswith('.ism/Manifest'):
254 'format_id': format_id
,
255 'tbr': int_or_none(item
.get('bitrate')),
256 'filesize': int_or_none(item
.get('fileSize')),
258 if not formats
and data
.get('drmProtected'):
259 self
.report_drm(video_id
)
262 'id': thumbnail
.get('@type'),
263 'url': thumbnail
.get('url'),
264 } for _
, thumbnail
in (asset
.get('imageVersions') or {}).items()]
270 'description': strip_or_none(asset
.get('description')),
271 'thumbnails': thumbnails
,
272 'timestamp': parse_iso8601(asset
.get('createTime')),
273 'duration': float_or_none(asset
.get('accurateDuration') or asset
.get('duration')),
274 'view_count': int_or_none(asset
.get('views')),
275 'categories': asset
.get('keywords', '').split(','),
281 class MTVUutisetArticleIE(InfoExtractor
):
282 _VALID_URL
= r
'https?://(?:www\.)mtvuutiset\.fi/artikkeli/[^/]+/(?P<id>\d+)'
284 'url': 'https://www.mtvuutiset.fi/artikkeli/tallaisia-vaurioita-viking-amorellassa-on-useamman-osaston-alla-vetta/7931384',
288 'title': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla',
289 'description': 'Viking Amorellan matkustajien evakuointi on alkanut – tältä operaatio näyttää laivalla',
290 'timestamp': 1600608966,
291 'upload_date': '20200920',
292 'duration': 153.7886666,
298 'skip_download': True,
301 # multiple Youtube embeds
302 'url': 'https://www.mtvuutiset.fi/artikkeli/50-vuotta-subarun-vastaiskua/6070962',
303 'only_matching': True,
306 def _real_extract(self
, url
):
307 article_id
= self
._match
_id
(url
)
308 article
= self
._download
_json
(
309 'http://api.mtvuutiset.fi/mtvuutiset/api/json/' + article_id
,
313 for video
in (article
.get('videos') or []):
314 video_type
= video
.get('videotype')
315 video_url
= video
.get('url')
316 if not (video_url
and video_type
in ('katsomo', 'youtube')):
318 yield self
.url_result(
319 video_url
, video_type
.capitalize(), video
.get('video_id'))
321 return self
.playlist_result(
322 entries(), article_id
, article
.get('title'), article
.get('description'))