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 _API_DOMAIN = 'sumo.tv2.no'
38 _PROTOCOLS = ('HDS', 'HLS', 'DASH')
39 _GEO_COUNTRIES = ['NO']
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
43 api_base = 'http://%s/api/web/asset/%s' % (self._API_DOMAIN, video_id)
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
53 for protocol in self._PROTOCOLS:
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'])
68 items = try_get(data, lambda x: x['items']['item'])
71 if not isinstance(items, list):
74 if not isinstance(item, dict):
76 video_url = item.get('url')
77 if not video_url or video_url in format_urls:
79 format_id = '%s-%s' % (protocol.lower(), item.get('mediaFormat'))
80 if not self._is_valid_url(video_url, video_id, format_id):
82 format_urls.append(video_url)
83 ext = determine_ext(video_url)
85 formats.extend(self._extract_f4m_formats(
86 video_url, video_id, f4m_id=format_id, fatal=False))
88 if not data.get('drmProtected'):
89 formats.extend(self._extract_m3u8_formats(
90 video_url, video_id, 'mp4',
91 'm3u8' if is_live else 'm3u8_native',
92 m3u8_id=format_id, fatal=False))
94 formats.extend(self._extract_mpd_formats(
95 video_url, video_id, format_id, fatal=False))
96 elif ext == 'ism' or video_url.endswith('.ism/Manifest'):
101 'format_id': format_id,
102 'tbr': int_or_none(item.get('bitrate')),
103 'filesize': int_or_none(item.get('fileSize')),
105 if not formats and data.get('drmProtected'):
106 raise ExtractorError('This video is DRM protected.', expected=True)
107 self._sort_formats(formats)
110 'id': thumbnail.get('@type'),
111 'url': thumbnail.get('url'),
112 } for _, thumbnail in (asset.get('imageVersions') or {}).items()]
117 'title': self._live_title(title) if is_live else title,
118 'description': strip_or_none(asset.get('description')),
119 'thumbnails': thumbnails,
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(','),
129 class TV2ArticleIE(InfoExtractor):
130 _VALID_URL = r'https?://(?:www\.)?tv2\.no/(?:a|\d{4}/\d{2}/\d{2}(/[^/]+)+)/(?P<id>\d+)'
132 'url': 'http://www.tv2.no/2015/05/16/nyheter/alesund/krim/pingvin/6930542',
135 'title': 'Russen hetses etter pingvintyveri - innrømmer å ha åpnet luken på buret',
136 'description': 'De fire siktede nekter fortsatt for å ha stjålet pingvinbabyene, men innrømmer å ha åpnet luken til de små kyllingene.',
140 'url': 'http://www.tv2.no/a/6930542',
141 'only_matching': True,
144 def _real_extract(self, url):
145 playlist_id = self._match_id(url)
147 webpage = self._download_webpage(url, playlist_id)
149 # Old embed pattern (looks unused nowadays)
150 assets = re.findall(r'data-assetid=["\'](\d+)', webpage)
154 for v in re.findall(r'(?s)TV2ContentboxVideo\(({.+?})\)', webpage):
155 video = self._parse_json(
156 v, playlist_id, transform_source=js_to_json, fatal=False)
159 asset = video.get('assetId')
164 self.url_result('http://www.tv2.no/v/%s' % asset_id, 'TV2')
165 for asset_id in assets]
167 title = remove_end(self._og_search_title(webpage), ' - TV2.no')
168 description = remove_end(self._og_search_description(webpage), ' - TV2.no')
170 return self.playlist_result(entries, playlist_id, title, description)
173 class KatsomoIE(TV2IE):
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+)'
176 'url': 'https://www.mtv.fi/sarja/mtv-uutiset-live-33001002003/lahden-pelicans-teki-kovan-ratkaisun-ville-nieminen-pihalle-1181321',
180 'title': 'Lahden Pelicans teki kovan ratkaisun – Ville Nieminen pihalle',
181 'description': 'Päätöksen teki Pelicansin hallitus.',
182 'timestamp': 1575116484,
183 'upload_date': '20191130',
190 'skip_download': True,
193 'url': 'http://www.katsomo.fi/#!/jakso/33001005/studio55-fi/658521/jukka-kuoppamaki-tekee-yha-lauluja-vaikka-lentokoneessa',
194 'only_matching': True,
196 'url': 'https://www.mtvuutiset.fi/video/prog1311159',
197 'only_matching': True,
199 'url': 'https://www.katsomo.fi/#!/jakso/1311159',
200 'only_matching': True,
202 _API_DOMAIN = 'api.katsomo.fi'
203 _PROTOCOLS = ('HLS', 'MPD')
204 _GEO_COUNTRIES = ['FI']
207 class MTVUutisetArticleIE(InfoExtractor):
208 _VALID_URL = r'https?://(?:www\.)mtvuutiset\.fi/artikkeli/[^/]+/(?P<id>\d+)'
210 'url': 'https://www.mtvuutiset.fi/artikkeli/tallaisia-vaurioita-viking-amorellassa-on-useamman-osaston-alla-vetta/7931384',
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,
224 'skip_download': True,
227 # multiple Youtube embeds
228 'url': 'https://www.mtvuutiset.fi/artikkeli/50-vuotta-subarun-vastaiskua/6070962',
229 'only_matching': True,
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,
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')):
244 yield self.url_result(
245 video_url, video_type.capitalize(), video.get('video_id'))
247 return self.playlist_result(
248 entries(), article_id, article.get('title'), article.get('description'))