2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
7 from ..compat
import compat_HTTPError
21 class TV2IE(InfoExtractor
):
22 _VALID_URL
= r
'https?://(?:www\.)?tv2\.no/v/(?P<id>\d+)'
24 'url': 'http://www.tv2.no/v/916509/',
28 'title': 'Se Frode Gryttens hyllest av Steven Gerrard',
29 'description': 'TV 2 Sportens huspoet tar avskjed med Liverpools kaptein Steven Gerrard.',
30 'timestamp': 1431715610,
31 'upload_date': '20150515',
37 _PROTOCOLS
= ('HLS', 'DASH')
38 _GEO_COUNTRIES
= ['NO']
40 def _real_extract(self
, url
):
41 video_id
= self
._match
_id
(url
)
42 asset
= self
._download
_json
('https://sumo.tv2.no/rest/assets/' + video_id
, video_id
,
43 'Downloading metadata JSON')
44 title
= asset
['title']
45 is_live
= asset
.get('live') is True
49 for protocol
in self
._PROTOCOLS
:
51 data
= self
._download
_json
('https://api.sumo.tv2.no/play/%s?stream=%s' % (video_id
, protocol
),
52 video_id
, 'Downloading playabck JSON',
53 headers
={'content-type': 'application/json'}
,
54 data
='{"device":{"id":"1-1-1","name":"Nettleser (HTML)"}}'.encode())['playback']
55 except ExtractorError
as e
:
56 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 401:
57 error
= self
._parse
_json
(e
.cause
.read().decode(), video_id
)['error']
58 error_code
= error
.get('code')
59 if error_code
== 'ASSET_PLAYBACK_INVALID_GEO_LOCATION':
60 self
.raise_geo_restricted(countries
=self
._GEO
_COUNTRIES
)
61 elif error_code
== 'SESSION_NOT_AUTHENTICATED':
62 self
.raise_login_required()
63 raise ExtractorError(error
['description'])
65 items
= data
.get('streams', [])
67 video_url
= item
.get('url')
68 if not video_url
or video_url
in format_urls
:
70 format_id
= '%s-%s' % (protocol
.lower(), item
.get('type'))
71 if not self
._is
_valid
_url
(video_url
, video_id
, format_id
):
73 format_urls
.append(video_url
)
74 ext
= determine_ext(video_url
)
76 formats
.extend(self
._extract
_f
4m
_formats
(
77 video_url
, video_id
, f4m_id
=format_id
, fatal
=False))
79 if not data
.get('drmProtected'):
80 formats
.extend(self
._extract
_m
3u8_formats
(
81 video_url
, video_id
, 'mp4',
82 'm3u8' if is_live
else 'm3u8_native',
83 m3u8_id
=format_id
, fatal
=False))
85 formats
.extend(self
._extract
_mpd
_formats
(
86 video_url
, video_id
, format_id
, fatal
=False))
87 elif ext
== 'ism' or video_url
.endswith('.ism/Manifest'):
92 'format_id': format_id
,
94 if not formats
and data
.get('drmProtected'):
95 self
.report_drm(video_id
)
96 self
._sort
_formats
(formats
)
101 } for type, thumb_url
in (asset
.get('images') or {}).items()]
106 'title': self
._live
_title
(title
) if is_live
else title
,
107 'description': strip_or_none(asset
.get('description')),
108 'thumbnails': thumbnails
,
109 'timestamp': parse_iso8601(asset
.get('live_broadcast_time') or asset
.get('update_time')),
110 'duration': float_or_none(asset
.get('accurateDuration') or asset
.get('duration')),
111 'view_count': int_or_none(asset
.get('views')),
112 'categories': asset
.get('tags', '').split(','),
118 class TV2ArticleIE(InfoExtractor
):
119 _VALID_URL
= r
'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
121 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
124 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
125 'description': 'De fire siktede nekter fortsatt for å ha stjålet pingvinbabyene, men innrømmer å ha åpnet luken til de små kyllingene.',
129 'url': 'http://www.tv2.no/a/6930542',
130 'only_matching': True,
133 def _real_extract(self
, url
):
134 playlist_id
= self
._match
_id
(url
)
136 webpage
= self
._download
_webpage
(url
, playlist_id
)
138 # Old embed pattern (looks unused nowadays)
139 assets
= re
.findall(r
'data-assetid=["\'](\d
+)', webpage)
143 for v in re.findall(r'(?s
)TV2ContentboxVideo\
(({.+?}
)\
)', webpage):
144 video = self._parse_json(
145 v, playlist_id, transform_source=js_to_json, fatal=False)
148 asset = video.get('assetId
')
153 self.url_result('http
://www
.tv2
.no
/v
/%s' % asset_id, 'TV2
')
154 for asset_id in assets]
156 title = remove_end(self._og_search_title(webpage), ' - TV2
.no
')
157 description = remove_end(self._og_search_description(webpage), ' - TV2
.no
')
159 return self.playlist_result(entries, playlist_id, title, description)
162 class KatsomoIE(InfoExtractor):
163 _VALID_URL = r'https?
://(?
:www\
.)?
(?
:katsomo|
mtv(uutiset
)?
)\
.fi
/(?
:sarja
/[0-9a
-z
-]+-\d
+/[0-9a
-z
-]+-|
(?
:#!/)?jakso/(?:\d+/[^/]+/)?|video/prog)(?P<id>\d+)'
165 'url': 'https://www.mtv.fi/sarja/mtv-uutiset-live-33001002003/lahden-pelicans-teki-kovan-ratkaisun-ville-nieminen-pihalle-1181321',
169 'title': 'Lahden Pelicans teki kovan ratkaisun – Ville Nieminen pihalle',
170 'description': 'Päätöksen teki Pelicansin hallitus.',
171 'timestamp': 1575116484,
172 'upload_date': '20191130',
179 'skip_download': True,
182 'url': 'http://www.katsomo.fi/#!/jakso/33001005/studio55-fi/658521/jukka-kuoppamaki-tekee-yha-lauluja-vaikka-lentokoneessa',
183 'only_matching': True,
185 'url': 'https://www.mtvuutiset.fi/video/prog1311159',
186 'only_matching': True,
188 'url': 'https://www.katsomo.fi/#!/jakso/1311159',
189 'only_matching': True,
191 _API_DOMAIN
= 'api.katsomo.fi'
192 _PROTOCOLS
= ('HLS', 'MPD')
193 _GEO_COUNTRIES
= ['FI']
195 def _real_extract(self
, url
):
196 video_id
= self
._match
_id
(url
)
197 api_base
= 'http://%s/api/web/asset/%s' % (self
._API
_DOMAIN
, video_id
)
199 asset
= self
._download
_json
(
200 api_base
+ '.json', video_id
,
201 'Downloading metadata JSON')['asset']
202 title
= asset
.get('subtitle') or asset
['title']
203 is_live
= asset
.get('live') is True
207 for protocol
in self
._PROTOCOLS
:
209 data
= self
._download
_json
(
210 api_base
+ '/play.json?protocol=%s&videoFormat=SMIL+ISMUSP' % protocol
,
211 video_id
, 'Downloading play JSON')['playback']
212 except ExtractorError
as e
:
213 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 401:
214 error
= self
._parse
_json
(e
.cause
.read().decode(), video_id
)['error']
215 error_code
= error
.get('code')
216 if error_code
== 'ASSET_PLAYBACK_INVALID_GEO_LOCATION':
217 self
.raise_geo_restricted(countries
=self
._GEO
_COUNTRIES
)
218 elif error_code
== 'SESSION_NOT_AUTHENTICATED':
219 self
.raise_login_required()
220 raise ExtractorError(error
['description'])
222 items
= try_get(data
, lambda x
: x
['items']['item'])
225 if not isinstance(items
, list):
228 if not isinstance(item
, dict):
230 video_url
= item
.get('url')
231 if not video_url
or video_url
in format_urls
:
233 format_id
= '%s-%s' % (protocol
.lower(), item
.get('mediaFormat'))
234 if not self
._is
_valid
_url
(video_url
, video_id
, format_id
):
236 format_urls
.append(video_url
)
237 ext
= determine_ext(video_url
)
239 formats
.extend(self
._extract
_f
4m
_formats
(
240 video_url
, video_id
, f4m_id
=format_id
, fatal
=False))
242 if not data
.get('drmProtected'):
243 formats
.extend(self
._extract
_m
3u8_formats
(
244 video_url
, video_id
, 'mp4',
245 'm3u8' if is_live
else 'm3u8_native',
246 m3u8_id
=format_id
, fatal
=False))
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'):
255 'format_id': format_id
,
256 'tbr': int_or_none(item
.get('bitrate')),
257 'filesize': int_or_none(item
.get('fileSize')),
259 if not formats
and data
.get('drmProtected'):
260 self
.report_drm(video_id
)
261 self
._sort
_formats
(formats
)
264 'id': thumbnail
.get('@type'),
265 'url': thumbnail
.get('url'),
266 } for _
, thumbnail
in (asset
.get('imageVersions') or {}).items()]
271 'title': self
._live
_title
(title
) if is_live
else title
,
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(','),
283 class MTVUutisetArticleIE(InfoExtractor
):
284 _VALID_URL
= r
'https?://(?:www\.)mtvuutiset\.fi/artikkeli/[^/]+/(?P<id>\d+)'
286 'url': 'https://www.mtvuutiset.fi/artikkeli/tallaisia-vaurioita-viking-amorellassa-on-useamman-osaston-alla-vetta/7931384',
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,
300 'skip_download': True,
303 # multiple Youtube embeds
304 'url': 'https://www.mtvuutiset.fi/artikkeli/50-vuotta-subarun-vastaiskua/6070962',
305 'only_matching': True,
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
,
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')):
320 yield self
.url_result(
321 video_url
, video_type
.capitalize(), video
.get('video_id'))
323 return self
.playlist_result(
324 entries(), article_id
, article
.get('title'), article
.get('description'))