2 from __future__
import unicode_literals
7 from .common
import InfoExtractor
8 from ..compat
import compat_urllib_parse_unquote
17 class NRKBaseIE(InfoExtractor
):
20 def _download_webpage_handle(self
, *args
, **kwargs
):
21 # NRK checks X-Forwarded-For HTTP header in order to figure out the
22 # origin of the client behind proxy. This allows to bypass geo
23 # restriction by faking this header's value to some Norway IP.
24 # We will do so once we encounter any geo restriction error.
26 # NB: str is intentional
27 kwargs
.setdefault(str('headers'), {})['X-Forwarded-For'] = self
._faked
_ip
28 return super(NRKBaseIE
, self
)._download
_webpage
_handle
(*args
, **kwargs
)
31 # Use fake IP from 37.191.128.0/17 in order to workaround geo
33 def octet(lb
=0, ub
=255):
34 return random
.randint(lb
, ub
)
35 self
._faked
_ip
= '37.191.%d.%d' % (octet(128), octet())
37 def _real_extract(self
, url
):
38 video_id
= self
._match
_id
(url
)
40 data
= self
._download
_json
(
41 'http://%s/mediaelement/%s' % (self
._API
_HOST
, video_id
),
42 video_id
, 'Downloading mediaelement JSON')
44 title
= data
.get('fullTitle') or data
.get('mainTitle') or data
['title']
45 video_id
= data
.get('id') or video_id
47 http_headers
= {'X-Forwarded-For': self._faked_ip}
if self
._faked
_ip
else {}
51 conviva
= data
.get('convivaStatistics') or {}
52 live
= (data
.get('mediaElementType') == 'Live' or
53 data
.get('isLive') is True or conviva
.get('isLive'))
56 return self
._live
_title
(t
) if live
else t
58 media_assets
= data
.get('mediaAssets')
59 if media_assets
and isinstance(media_assets
, list):
60 def video_id_and_title(idx
):
61 return ((video_id
, title
) if len(media_assets
) == 1
62 else ('%s-%d' % (video_id
, idx
), '%s (Part %d)' % (title
, idx
)))
63 for num
, asset
in enumerate(media_assets
, 1):
64 asset_url
= asset
.get('url')
67 formats
= self
._extract
_akamai
_formats
(asset_url
, video_id
)
70 self
._sort
_formats
(formats
)
72 # Some f4m streams may not work with hdcore in fragments' URLs
74 extra_param
= f
.get('extra_param_to_segment_url')
75 if extra_param
and 'hdcore' in extra_param
:
76 del f
['extra_param_to_segment_url']
78 entry_id
, entry_title
= video_id_and_title(num
)
79 duration
= parse_duration(asset
.get('duration'))
81 for subtitle
in ('webVtt', 'timedText'):
82 subtitle_url
= asset
.get('%sSubtitlesUrl' % subtitle
)
84 subtitles
.setdefault('no', []).append({
85 'url': compat_urllib_parse_unquote(subtitle_url
)
88 'id': asset
.get('carrierId') or entry_id
,
89 'title': make_title(entry_title
),
91 'subtitles': subtitles
,
93 'http_headers': http_headers
,
97 media_url
= data
.get('mediaUrl')
99 formats
= self
._extract
_akamai
_formats
(media_url
, video_id
)
100 self
._sort
_formats
(formats
)
101 duration
= parse_duration(data
.get('duration'))
104 'title': make_title(title
),
105 'duration': duration
,
110 message_type
= data
.get('messageType', '')
111 # Can be ProgramIsGeoBlocked or ChannelIsGeoBlocked*
112 if 'IsGeoBlocked' in message_type
and not self
._faked
_ip
:
114 'Video is geo restricted, trying to fake IP')
116 return self
._real
_extract
(url
)
119 'ProgramRightsAreNotReady': 'Du kan dessverre ikke se eller høre programmet',
120 'ProgramRightsHasExpired': 'Programmet har gått ut',
121 'ProgramIsGeoBlocked': 'NRK har ikke rettigheter til å vise dette programmet utenfor Norge',
123 raise ExtractorError(
124 '%s said: %s' % (self
.IE_NAME
, MESSAGES
.get(
125 message_type
, message_type
)),
128 series
= conviva
.get('seriesName') or data
.get('seriesTitle')
129 episode
= conviva
.get('episodeName') or data
.get('episodeNumberOrDate')
132 images
= data
.get('images')
133 if images
and isinstance(images
, dict):
134 web_images
= images
.get('webImages')
135 if isinstance(web_images
, list):
137 'url': image
['imageUrl'],
138 'width': int_or_none(image
.get('width')),
139 'height': int_or_none(image
.get('height')),
140 } for image
in web_images
if image
.get('imageUrl')]
142 description
= data
.get('description')
145 'description': description
,
148 'age_limit': parse_age_limit(data
.get('legalAge')),
149 'thumbnails': thumbnails
,
152 vcodec
= 'none' if data
.get('mediaType') == 'Audio' else None
154 # TODO: extract chapters when https://github.com/rg3/youtube-dl/pull/9409 is merged
156 for entry
in entries
:
157 entry
.update(common_info
)
158 for f
in entry
['formats']:
161 return self
.playlist_result(entries
, video_id
, title
, description
)
164 class NRKIE(NRKBaseIE
):
165 _VALID_URL
= r
'''(?x)
170 (?:www\.)?nrk\.no/video/PS\*|
171 v8-psapi\.nrk\.no/mediaelement/
176 _API_HOST
= 'v8.psapi.nrk.no'
179 'url': 'http://www.nrk.no/video/PS*150533',
180 'md5': '2f7f6eeb2aacdd99885f355428715cfa',
184 'title': 'Dompap og andre fugler i Piip-Show',
185 'description': 'md5:d9261ba34c43b61c812cb6b0269a5c8f',
190 'url': 'http://www.nrk.no/video/PS*154915',
195 'title': 'Slik høres internett ut når du er blind',
196 'description': 'md5:a621f5cc1bd75c8d5104cb048c6b8568',
200 'url': 'nrk:ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
201 'only_matching': True,
203 'url': 'https://v8-psapi.nrk.no/mediaelement/ecc1b952-96dc-4a98-81b9-5296dc7a98d9',
204 'only_matching': True,
208 class NRKTVIE(NRKBaseIE
):
209 IE_DESC
= 'NRK TV and NRK Radio'
210 _EPISODE_RE
= r
'(?P<id>[a-zA-Z]{4}\d{8})'
211 _VALID_URL
= r
'''(?x)
213 (?:tv|radio)\.nrk(?:super)?\.no/
214 (?:serie/[^/]+|program)/
216 (?:/\d{2}-\d{2}-\d{4})?
217 (?:\#del=(?P<part_id>\d+))?
219 _API_HOST
= 'psapi-we.nrk.no'
222 'url': 'https://tv.nrk.no/serie/20-spoersmaal-tv/MUHH48000314/23-05-2014',
223 'md5': '4e9ca6629f09e588ed240fb11619922a',
225 'id': 'MUHH48000314AA',
227 'title': '20 spørsmål 23.05.2014',
228 'description': 'md5:bdea103bc35494c143c6a9acdd84887a',
232 'url': 'https://tv.nrk.no/program/mdfp15000514',
233 'md5': '43d0be26663d380603a9cf0c24366531',
235 'id': 'MDFP15000514CA',
237 'title': 'Grunnlovsjubiléet - Stor ståhei for ingenting 24.05.2014',
238 'description': 'md5:89290c5ccde1b3a24bb8050ab67fe1db',
242 # single playlist video
243 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015#del=2',
244 'md5': 'adbd1dbd813edaf532b0a253780719c2',
246 'id': 'MSPO40010515-part2',
248 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
249 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
251 'skip': 'Only works from Norway',
253 'url': 'https://tv.nrk.no/serie/tour-de-ski/MSPO40010515/06-01-2015',
255 'md5': '9480285eff92d64f06e02a5367970a7a',
257 'id': 'MSPO40010515-part1',
259 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 1:2)',
260 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
263 'md5': 'adbd1dbd813edaf532b0a253780719c2',
265 'id': 'MSPO40010515-part2',
267 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn 06.01.2015 (del 2:2)',
268 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
272 'id': 'MSPO40010515',
273 'title': 'Tour de Ski: Sprint fri teknikk, kvinner og menn',
274 'description': 'md5:238b67b97a4ac7d7b4bf0edf8cc57d26',
277 'skip': 'Only works from Norway',
279 'url': 'https://radio.nrk.no/serie/dagsnytt/NPUB21019315/12-07-2015#',
280 'only_matching': True,
284 class NRKTVDirekteIE(NRKTVIE
):
285 IE_DESC
= 'NRK TV Direkte and NRK Radio Direkte'
286 _VALID_URL
= r
'https?://(?:tv|radio)\.nrk\.no/direkte/(?P<id>[^/?#&]+)'
289 'url': 'https://tv.nrk.no/direkte/nrk1',
290 'only_matching': True,
292 'url': 'https://radio.nrk.no/direkte/p1_oslo_akershus',
293 'only_matching': True,
297 class NRKPlaylistBaseIE(InfoExtractor
):
298 def _extract_description(self
, webpage
):
301 def _real_extract(self
, url
):
302 playlist_id
= self
._match
_id
(url
)
304 webpage
= self
._download
_webpage
(url
, playlist_id
)
307 self
.url_result('nrk:%s' % video_id
, NRKIE
.ie_key())
308 for video_id
in re
.findall(self
._ITEM
_RE
, webpage
)
311 playlist_title
= self
. _extract_title(webpage
)
312 playlist_description
= self
._extract
_description
(webpage
)
314 return self
.playlist_result(
315 entries
, playlist_id
, playlist_title
, playlist_description
)
318 class NRKPlaylistIE(NRKPlaylistBaseIE
):
319 _VALID_URL
= r
'https?://(?:www\.)?nrk\.no/(?!video|skole)(?:[^/]+/)+(?P<id>[^/]+)'
320 _ITEM_RE
= r
'class="[^"]*\brich\b[^"]*"[^>]+data-video-id="([^"]+)"'
322 'url': 'http://www.nrk.no/troms/gjenopplev-den-historiske-solformorkelsen-1.12270763',
324 'id': 'gjenopplev-den-historiske-solformorkelsen-1.12270763',
325 'title': 'Gjenopplev den historiske solformørkelsen',
326 'description': 'md5:c2df8ea3bac5654a26fc2834a542feed',
330 'url': 'http://www.nrk.no/kultur/bok/rivertonprisen-til-karin-fossum-1.12266449',
332 'id': 'rivertonprisen-til-karin-fossum-1.12266449',
333 'title': 'Rivertonprisen til Karin Fossum',
334 'description': 'Første kvinne på 15 år til å vinne krimlitteraturprisen.',
339 def _extract_title(self
, webpage
):
340 return self
._og
_search
_title
(webpage
, fatal
=False)
342 def _extract_description(self
, webpage
):
343 return self
._og
_search
_description
(webpage
)
346 class NRKTVEpisodesIE(NRKPlaylistBaseIE
):
347 _VALID_URL
= r
'https?://tv\.nrk\.no/program/[Ee]pisodes/[^/]+/(?P<id>\d+)'
348 _ITEM_RE
= r
'data-episode=["\']%s' % NRKTVIE._EPISODE_RE
350 'url
': 'https
://tv
.nrk
.no
/program
/episodes
/nytt
-paa
-nytt
/69031',
353 'title
': 'Nytt på nytt
, sesong
: 201210',
358 def _extract_title(self, webpage):
359 return self._html_search_regex(
360 r'<h1
>([^
<]+)</h1
>', webpage, 'title
', fatal=False)
363 class NRKSkoleIE(InfoExtractor):
364 IE_DESC = 'NRK Skole
'
365 _VALID_URL = r'https?
://(?
:www\
.)?nrk\
.no
/skole
/?
\?.*\bmediaId
=(?P
<id>\d
+)'
368 'url
': 'https
://www
.nrk
.no
/skole
/?page
=search
&q
=&mediaId
=14099',
369 'md5
': '6bc936b01f9dd8ed45bc58b252b2d9b6
',
373 'title
': 'Genetikk og eneggede tvillinger
',
374 'description
': 'md5
:3aca25dcf38ec30f0363428d2b265f8d
',
378 'url
': 'https
://www
.nrk
.no
/skole
/?page
=objectives
&subject
=naturfag
&objective
=K15114
&mediaId
=19355',
379 'only_matching
': True,
382 def _real_extract(self, url):
383 video_id = self._match_id(url)
385 webpage = self._download_webpage(
386 'https
://mimir
.nrk
.no
/plugin
/1.0/static?mediaId
=%s' % video_id,
389 nrk_id = self._parse_json(
391 r'<script
[^
>]+type=["\']application/json["\'][^
>]*>({.+?}
)</script
>',
392 webpage, 'application json
'),
393 video_id)['activeMedia
']['psId
']
395 return self.url_result('nrk
:%s' % nrk_id)