6 from .common
import InfoExtractor
7 from ..aes
import aes_cbc_decrypt_bytes
, unpad_pkcs7
8 from ..compat
import compat_urllib_parse_unquote
22 class DRTVIE(InfoExtractor
):
26 (?:www\.)?dr\.dk/(?:tv/se|nyheder|radio(?:/ondemand)?)/(?:[^/]+/)*|
27 (?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/(?:se|episode|program)/
32 _GEO_COUNTRIES
= ['DK']
35 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
36 'md5': '25e659cccc9a2ed956110a299fdf5983',
38 'id': 'klassen-darlig-taber-10',
40 'title': 'Klassen - Dårlig taber (10)',
41 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
42 'timestamp': 1539085800,
43 'upload_date': '20181009',
46 'season': 'Klassen I',
48 'season_id': 'urn:dr:mu:bundle:57d7e8216187a4031cfd6f6b',
49 'episode': 'Episode 10',
53 'expected_warnings': ['Unable to download f4m manifest'],
56 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
58 'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6',
60 'title': 'christiania pusher street ryddes drdkrjpo',
61 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
62 'timestamp': 1472800279,
63 'upload_date': '20160902',
67 'skip_download': True,
69 'expected_warnings': ['Unable to download f4m manifest'],
71 # with SignLanguage formats
72 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
74 'id': 'historien-om-danmark-stenalder',
76 'title': 'Historien om Danmark: Stenalder',
77 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
78 'timestamp': 1546628400,
79 'upload_date': '20190104',
81 'formats': 'mincount:20',
84 'skip_download': True,
87 'url': 'https://www.dr.dk/radio/p4kbh/regionale-nyheder-kh4/p4-nyheder-2019-06-26-17-30-9',
88 'only_matching': True,
90 'url': 'https://www.dr.dk/drtv/se/bonderoeven_71769',
94 'title': 'Bonderøven (1:8)',
95 'description': 'md5:3cf18fc0d3b205745d4505f896af8121',
96 'timestamp': 1546542000,
97 'upload_date': '20190103',
101 'skip_download': True,
104 'url': 'https://www.dr.dk/drtv/episode/bonderoeven_71769',
105 'only_matching': True,
107 'url': 'https://dr-massive.com/drtv/se/bonderoeven_71769',
108 'only_matching': True,
110 'url': 'https://www.dr.dk/drtv/program/jagten_220924',
111 'only_matching': True,
114 def _real_extract(self
, url
):
115 video_id
= self
._match
_id
(url
)
117 webpage
= self
._download
_webpage
(url
, video_id
)
119 if '>Programmet er ikke længere tilgængeligt' in webpage
:
120 raise ExtractorError(
121 'Video %s is not available' % video_id
, expected
=True)
123 video_id
= self
._search
_regex
(
124 (r
'data-(?:material-identifier|episode-slug)="([^"]+)"',
125 r
'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
126 webpage
, 'video id', default
=None)
129 video_id
= self
._search
_regex
(
130 r
'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
131 webpage
, 'urn', default
=None)
133 video_id
= compat_urllib_parse_unquote(video_id
)
135 _PROGRAMCARD_BASE
= 'https://www.dr.dk/mu-online/api/1.4/programcard'
136 query
= {'expanded': 'true'}
139 programcard_url
= '%s/%s' % (_PROGRAMCARD_BASE
, video_id
)
141 programcard_url
= _PROGRAMCARD_BASE
142 page
= self
._parse
_json
(
144 r
'data\s*=\s*({.+?})\s*(?:;|</script)', webpage
,
145 'data'), '1')['cache']['page']
146 page
= page
[list(page
.keys())[0]]
148 page
, (lambda x
: x
['item'], lambda x
: x
['entries'][0]['item']),
150 video_id
= item
['customId'].split(':')[-1]
151 query
['productionnumber'] = video_id
153 data
= self
._download
_json
(
154 programcard_url
, video_id
, 'Downloading video JSON', query
=query
)
156 title
= str_or_none(data
.get('Title')) or re
.sub(
157 r
'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '',
158 self
._og
_search
_title
(webpage
))
159 description
= self
._og
_search
_description
(
160 webpage
, default
=None) or data
.get('Description')
162 timestamp
= unified_timestamp(
163 data
.get('PrimaryBroadcastStartTime') or data
.get('SortDateTime'))
168 restricted_to_denmark
= False
174 primary_asset
= data
.get('PrimaryAsset')
175 if isinstance(primary_asset
, dict):
176 assets
.append(primary_asset
)
177 secondary_assets
= data
.get('SecondaryAssets')
178 if isinstance(secondary_assets
, list):
179 for secondary_asset
in secondary_assets
:
180 if isinstance(secondary_asset
, dict):
181 assets
.append(secondary_asset
)
183 def hex_to_bytes(hex):
184 return binascii
.a2b_hex(hex.encode('ascii'))
189 data
= hex_to_bytes(e
[10:10 + n
])
190 key
= hashlib
.sha256(('%s:sRBzYNXBzkKgnjj8pGtkACch' % a
).encode('utf-8')).digest()
192 decrypted
= unpad_pkcs7(aes_cbc_decrypt_bytes(data
, key
, iv
))
193 return decrypted
.decode('utf-8').split('?')[0]
196 kind
= asset
.get('Kind')
198 thumbnail
= url_or_none(asset
.get('Uri'))
199 elif kind
in ('VideoResource', 'AudioResource'):
200 duration
= float_or_none(asset
.get('DurationInMilliseconds'), 1000)
201 restricted_to_denmark
= asset
.get('RestrictedToDenmark')
202 asset_target
= asset
.get('Target')
203 for link
in asset
.get('Links', []):
204 uri
= link
.get('Uri')
206 encrypted_uri
= link
.get('EncryptedUri')
207 if not encrypted_uri
:
210 uri
= decrypt_uri(encrypted_uri
)
213 'Unable to decrypt EncryptedUri', video_id
)
215 uri
= url_or_none(uri
)
218 target
= link
.get('Target')
219 format_id
= target
or ''
220 if asset_target
in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
222 format_id
+= '-%s' % asset_target
223 elif asset_target
== 'Default':
228 f4m_formats
= self
._extract
_f
4m
_formats
(
229 uri
+ '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
230 video_id
, preference
, f4m_id
=format_id
, fatal
=False)
231 if kind
== 'AudioResource':
232 for f
in f4m_formats
:
234 formats
.extend(f4m_formats
)
235 elif target
== 'HLS':
236 formats
.extend(self
._extract
_m
3u8_formats
(
237 uri
, video_id
, 'mp4', entry_protocol
='m3u8_native',
238 quality
=preference
, m3u8_id
=format_id
,
241 bitrate
= link
.get('Bitrate')
243 format_id
+= '-%s' % bitrate
246 'format_id': format_id
,
247 'tbr': int_or_none(bitrate
),
248 'ext': link
.get('FileFormat'),
249 'vcodec': 'none' if kind
== 'AudioResource' else None,
250 'quality': preference
,
252 subtitles_list
= asset
.get('SubtitlesList') or asset
.get('Subtitleslist')
253 if isinstance(subtitles_list
, list):
257 for subs
in subtitles_list
:
258 if not isinstance(subs
, dict):
260 sub_uri
= url_or_none(subs
.get('Uri'))
263 lang
= subs
.get('Language') or 'da'
264 subtitles
.setdefault(LANGS
.get(lang
, lang
), []).append({
266 'ext': mimetype2ext(subs
.get('MimeType')) or 'vtt'
269 if not formats
and restricted_to_denmark
:
270 self
.raise_geo_restricted(
271 'Unfortunately, DR is not allowed to show this program outside Denmark.',
272 countries
=self
._GEO
_COUNTRIES
)
274 self
._sort
_formats
(formats
)
279 'description': description
,
280 'thumbnail': thumbnail
,
281 'timestamp': timestamp
,
282 'duration': duration
,
284 'subtitles': subtitles
,
285 'series': str_or_none(data
.get('SeriesTitle')),
286 'season': str_or_none(data
.get('SeasonTitle')),
287 'season_number': int_or_none(data
.get('SeasonNumber')),
288 'season_id': str_or_none(data
.get('SeasonUrn')),
289 'episode': str_or_none(data
.get('EpisodeTitle')),
290 'episode_number': int_or_none(data
.get('EpisodeNumber')),
291 'release_year': int_or_none(data
.get('ProductionYear')),
295 class DRTVLiveIE(InfoExtractor
):
296 IE_NAME
= 'drtv:live'
297 _VALID_URL
= r
'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
298 _GEO_COUNTRIES
= ['DK']
300 'url': 'https://www.dr.dk/tv/live/dr1',
304 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
308 'skip_download': True,
312 def _real_extract(self
, url
):
313 channel_id
= self
._match
_id
(url
)
314 channel_data
= self
._download
_json
(
315 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id
,
317 title
= channel_data
['Title']
320 for streaming_server
in channel_data
.get('StreamingServers', []):
321 server
= streaming_server
.get('Server')
324 link_type
= streaming_server
.get('LinkType')
325 for quality
in streaming_server
.get('Qualities', []):
326 for stream
in quality
.get('Streams', []):
327 stream_path
= stream
.get('Stream')
330 stream_url
= update_url_query(
331 '%s/%s' % (server
, stream_path
), {'b': ''}
)
332 if link_type
== 'HLS':
333 formats
.extend(self
._extract
_m
3u8_formats
(
334 stream_url
, channel_id
, 'mp4',
335 m3u8_id
=link_type
, fatal
=False, live
=True))
336 elif link_type
== 'HDS':
337 formats
.extend(self
._extract
_f
4m
_formats
(update_url_query(
338 '%s/%s' % (server
, stream_path
), {'hdcore': '3.7.0'}
),
339 channel_id
, f4m_id
=link_type
, fatal
=False))
340 self
._sort
_formats
(formats
)
345 'thumbnail': channel_data
.get('PrimaryImageUri'),