5 from .common
import InfoExtractor
6 from ..aes
import aes_cbc_decrypt_bytes
, unpad_pkcs7
7 from ..compat
import compat_urllib_parse_unquote
21 SERIES_API
= 'https://production-cdn.dr-massive.com/api/page?device=web_browser&item_detail_expand=all&lang=da&max_list_prefetch=3&path=%s'
24 class DRTVIE(InfoExtractor
):
28 (?:www\.)?dr\.dk/(?:tv/se|nyheder|(?:radio|lyd)(?:/ondemand)?)/(?:[^/]+/)*|
29 (?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/(?:se|episode|program)/
34 _GEO_COUNTRIES
= ['DK']
37 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
38 'md5': '25e659cccc9a2ed956110a299fdf5983',
40 'id': 'klassen-darlig-taber-10',
42 'title': 'Klassen - Dårlig taber (10)',
43 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
44 'timestamp': 1539085800,
45 'upload_date': '20181009',
48 'season': 'Klassen I',
50 'season_id': 'urn:dr:mu:bundle:57d7e8216187a4031cfd6f6b',
51 'episode': 'Episode 10',
55 'expected_warnings': ['Unable to download f4m manifest'],
56 'skip': 'this video has been removed',
59 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
61 'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6',
63 'title': 'christiania pusher street ryddes drdkrjpo',
64 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
65 'timestamp': 1472800279,
66 'upload_date': '20160902',
70 'skip_download': True,
72 'expected_warnings': ['Unable to download f4m manifest'],
74 # with SignLanguage formats
75 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
79 'title': 'Historien om Danmark: Stenalder',
80 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
81 'timestamp': 1546628400,
82 'upload_date': '20190104',
84 'formats': 'mincount:20',
86 'season_id': 'urn:dr:mu:bundle:5afc03ad6187a4065ca5fd35',
88 'season': 'Historien om Danmark',
89 'series': 'Historien om Danmark',
92 'skip_download': True,
95 'url': 'https://www.dr.dk/lyd/p4kbh/regionale-nyheder-kh4/p4-nyheder-2019-06-26-17-30-9',
96 'only_matching': True,
98 'url': 'https://www.dr.dk/drtv/se/bonderoeven_71769',
102 'title': 'Bonderøven 2019 (1:8)',
103 'description': 'md5:b6dcfe9b6f0bea6703e9a0092739a5bd',
104 'timestamp': 1603188600,
105 'upload_date': '20201020',
107 'season': 'Bonderøven 2019',
108 'season_id': 'urn:dr:mu:bundle:5c201667a11fa01ca4528ce5',
109 'release_year': 2019,
110 'season_number': 2019,
111 'series': 'Frank & Kastaniegaarden'
114 'skip_download': True,
117 'url': 'https://www.dr.dk/drtv/episode/bonderoeven_71769',
118 'only_matching': True,
120 'url': 'https://dr-massive.com/drtv/se/bonderoeven_71769',
121 'only_matching': True,
123 'url': 'https://www.dr.dk/drtv/program/jagten_220924',
124 'only_matching': True,
126 'url': 'https://www.dr.dk/lyd/p4aarhus/regionale-nyheder-ar4/regionale-nyheder-2022-05-05-12-30-3',
128 'id': 'urn:dr:mu:programcard:6265cb2571401424d0360113',
129 'title': "Regionale nyheder",
132 'series': 'P4 Østjylland regionale nyheder',
133 'timestamp': 1651746600,
134 'season': 'Regionale nyheder',
136 'season_id': 'urn:dr:mu:bundle:61c26889539f0201586b73c5',
138 'upload_date': '20220505',
141 'skip_download': True,
145 def _real_extract(self
, url
):
146 raw_video_id
= self
._match
_id
(url
)
148 webpage
= self
._download
_webpage
(url
, raw_video_id
)
150 if '>Programmet er ikke længere tilgængeligt' in webpage
:
151 raise ExtractorError(
152 'Video %s is not available' % raw_video_id
, expected
=True)
154 video_id
= self
._search
_regex
(
155 (r
'data-(?:material-identifier|episode-slug)="([^"]+)"',
156 r
'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
157 webpage
, 'video id', default
=None)
160 video_id
= self
._search
_regex
(
161 r
'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
162 webpage
, 'urn', default
=None)
164 video_id
= compat_urllib_parse_unquote(video_id
)
166 _PROGRAMCARD_BASE
= 'https://www.dr.dk/mu-online/api/1.4/programcard'
167 query
= {'expanded': 'true'}
170 programcard_url
= '%s/%s' % (_PROGRAMCARD_BASE
, video_id
)
172 programcard_url
= _PROGRAMCARD_BASE
173 page
= self
._parse
_json
(
175 r
'data\s*=\s*({.+?})\s*(?:;|</script)', webpage
,
176 'data'), '1')['cache']['page']
177 page
= page
[list(page
.keys())[0]]
179 page
, (lambda x
: x
['item'], lambda x
: x
['entries'][0]['item']),
181 video_id
= item
['customId'].split(':')[-1]
182 query
['productionnumber'] = video_id
184 data
= self
._download
_json
(
185 programcard_url
, video_id
, 'Downloading video JSON', query
=query
)
187 supplementary_data
= {}
188 if re
.search(r
'_\d+$', raw_video_id
):
189 supplementary_data
= self
._download
_json
(
190 SERIES_API
% f
'/episode/{raw_video_id}', raw_video_id
, fatal
=False) or {}
192 title
= str_or_none(data
.get('Title')) or re
.sub(
193 r
'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '',
194 self
._og
_search
_title
(webpage
))
195 description
= self
._og
_search
_description
(
196 webpage
, default
=None) or data
.get('Description')
198 timestamp
= unified_timestamp(
199 data
.get('PrimaryBroadcastStartTime') or data
.get('SortDateTime'))
204 restricted_to_denmark
= False
210 primary_asset
= data
.get('PrimaryAsset')
211 if isinstance(primary_asset
, dict):
212 assets
.append(primary_asset
)
213 secondary_assets
= data
.get('SecondaryAssets')
214 if isinstance(secondary_assets
, list):
215 for secondary_asset
in secondary_assets
:
216 if isinstance(secondary_asset
, dict):
217 assets
.append(secondary_asset
)
219 def hex_to_bytes(hex):
220 return binascii
.a2b_hex(hex.encode('ascii'))
225 data
= hex_to_bytes(e
[10:10 + n
])
226 key
= hashlib
.sha256(('%s:sRBzYNXBzkKgnjj8pGtkACch' % a
).encode('utf-8')).digest()
228 decrypted
= unpad_pkcs7(aes_cbc_decrypt_bytes(data
, key
, iv
))
229 return decrypted
.decode('utf-8').split('?')[0]
232 kind
= asset
.get('Kind')
234 thumbnail
= url_or_none(asset
.get('Uri'))
235 elif kind
in ('VideoResource', 'AudioResource'):
236 duration
= float_or_none(asset
.get('DurationInMilliseconds'), 1000)
237 restricted_to_denmark
= asset
.get('RestrictedToDenmark')
238 asset_target
= asset
.get('Target')
239 for link
in asset
.get('Links', []):
240 uri
= link
.get('Uri')
242 encrypted_uri
= link
.get('EncryptedUri')
243 if not encrypted_uri
:
246 uri
= decrypt_uri(encrypted_uri
)
249 'Unable to decrypt EncryptedUri', video_id
)
251 uri
= url_or_none(uri
)
254 target
= link
.get('Target')
255 format_id
= target
or ''
256 if asset_target
in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
258 format_id
+= '-%s' % asset_target
259 elif asset_target
== 'Default':
264 f4m_formats
= self
._extract
_f
4m
_formats
(
265 uri
+ '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
266 video_id
, preference
, f4m_id
=format_id
, fatal
=False)
267 if kind
== 'AudioResource':
268 for f
in f4m_formats
:
270 formats
.extend(f4m_formats
)
271 elif target
== 'HLS':
272 formats
.extend(self
._extract
_m
3u8_formats
(
273 uri
, video_id
, 'mp4', entry_protocol
='m3u8_native',
274 quality
=preference
, m3u8_id
=format_id
,
277 bitrate
= link
.get('Bitrate')
279 format_id
+= '-%s' % bitrate
282 'format_id': format_id
,
283 'tbr': int_or_none(bitrate
),
284 'ext': link
.get('FileFormat'),
285 'vcodec': 'none' if kind
== 'AudioResource' else None,
286 'quality': preference
,
288 subtitles_list
= asset
.get('SubtitlesList') or asset
.get('Subtitleslist')
289 if isinstance(subtitles_list
, list):
293 for subs
in subtitles_list
:
294 if not isinstance(subs
, dict):
296 sub_uri
= url_or_none(subs
.get('Uri'))
299 lang
= subs
.get('Language') or 'da'
300 subtitles
.setdefault(LANGS
.get(lang
, lang
), []).append({
302 'ext': mimetype2ext(subs
.get('MimeType')) or 'vtt'
305 if not formats
and restricted_to_denmark
:
306 self
.raise_geo_restricted(
307 'Unfortunately, DR is not allowed to show this program outside Denmark.',
308 countries
=self
._GEO
_COUNTRIES
)
313 'description': description
,
314 'thumbnail': thumbnail
,
315 'timestamp': timestamp
,
316 'duration': duration
,
318 'subtitles': subtitles
,
319 'series': str_or_none(data
.get('SeriesTitle')),
320 'season': str_or_none(data
.get('SeasonTitle')),
321 'season_number': int_or_none(data
.get('SeasonNumber')),
322 'season_id': str_or_none(data
.get('SeasonUrn')),
323 'episode': traverse_obj(supplementary_data
, ('entries', 0, 'item', 'contextualTitle')) or str_or_none(data
.get('EpisodeTitle')),
324 'episode_number': traverse_obj(supplementary_data
, ('entries', 0, 'item', 'episodeNumber')) or int_or_none(data
.get('EpisodeNumber')),
325 'release_year': int_or_none(data
.get('ProductionYear')),
329 class DRTVLiveIE(InfoExtractor
):
330 IE_NAME
= 'drtv:live'
331 _VALID_URL
= r
'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
332 _GEO_COUNTRIES
= ['DK']
334 'url': 'https://www.dr.dk/tv/live/dr1',
338 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
342 'skip_download': True,
346 def _real_extract(self
, url
):
347 channel_id
= self
._match
_id
(url
)
348 channel_data
= self
._download
_json
(
349 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id
,
351 title
= channel_data
['Title']
354 for streaming_server
in channel_data
.get('StreamingServers', []):
355 server
= streaming_server
.get('Server')
358 link_type
= streaming_server
.get('LinkType')
359 for quality
in streaming_server
.get('Qualities', []):
360 for stream
in quality
.get('Streams', []):
361 stream_path
= stream
.get('Stream')
364 stream_url
= update_url_query(
365 '%s/%s' % (server
, stream_path
), {'b': ''}
)
366 if link_type
== 'HLS':
367 formats
.extend(self
._extract
_m
3u8_formats
(
368 stream_url
, channel_id
, 'mp4',
369 m3u8_id
=link_type
, fatal
=False, live
=True))
370 elif link_type
== 'HDS':
371 formats
.extend(self
._extract
_f
4m
_formats
(update_url_query(
372 '%s/%s' % (server
, stream_path
), {'hdcore': '3.7.0'}
),
373 channel_id
, f4m_id
=link_type
, fatal
=False))
378 'thumbnail': channel_data
.get('PrimaryImageUri'),
384 class DRTVSeasonIE(InfoExtractor
):
385 IE_NAME
= 'drtv:season'
386 _VALID_URL
= r
'https?://(?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/saeson/(?P<display_id>[\w-]+)_(?P<id>\d+)'
387 _GEO_COUNTRIES
= ['DK']
389 'url': 'https://www.dr.dk/drtv/saeson/frank-and-kastaniegaarden_9008',
392 'display_id': 'frank-and-kastaniegaarden',
393 'title': 'Frank & Kastaniegaarden',
394 'series': 'Frank & Kastaniegaarden',
396 'playlist_mincount': 8
398 'url': 'https://www.dr.dk/drtv/saeson/frank-and-kastaniegaarden_8761',
401 'display_id': 'frank-and-kastaniegaarden',
402 'title': 'Frank & Kastaniegaarden',
403 'series': 'Frank & Kastaniegaarden',
405 'playlist_mincount': 19
408 def _real_extract(self
, url
):
409 display_id
, season_id
= self
._match
_valid
_url
(url
).group('display_id', 'id')
410 data
= self
._download
_json
(SERIES_API
% f
'/saeson/{display_id}_{season_id}', display_id
)
414 'url': f
'https://www.dr.dk/drtv{episode["path"]}',
415 'ie_key': DRTVIE
.ie_key(),
416 'title': episode
.get('title'),
417 'episode': episode
.get('episodeName'),
418 'description': episode
.get('shortDescription'),
419 'series': traverse_obj(data
, ('entries', 0, 'item', 'title')),
420 'season_number': traverse_obj(data
, ('entries', 0, 'item', 'seasonNumber')),
421 'episode_number': episode
.get('episodeNumber'),
422 } for episode
in traverse_obj(data
, ('entries', 0, 'item', 'episodes', 'items'))]
427 'display_id': display_id
,
428 'title': traverse_obj(data
, ('entries', 0, 'item', 'title')),
429 'series': traverse_obj(data
, ('entries', 0, 'item', 'title')),
431 'season_number': traverse_obj(data
, ('entries', 0, 'item', 'seasonNumber'))
435 class DRTVSeriesIE(InfoExtractor
):
436 IE_NAME
= 'drtv:series'
437 _VALID_URL
= r
'https?://(?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/serie/(?P<display_id>[\w-]+)_(?P<id>\d+)'
438 _GEO_COUNTRIES
= ['DK']
440 'url': 'https://www.dr.dk/drtv/serie/frank-and-kastaniegaarden_6954',
443 'display_id': 'frank-and-kastaniegaarden',
444 'title': 'Frank & Kastaniegaarden',
445 'series': 'Frank & Kastaniegaarden',
447 'playlist_mincount': 15
450 def _real_extract(self
, url
):
451 display_id
, series_id
= self
._match
_valid
_url
(url
).group('display_id', 'id')
452 data
= self
._download
_json
(SERIES_API
% f
'/serie/{display_id}_{series_id}', display_id
)
456 'url': f
'https://www.dr.dk/drtv{season.get("path")}',
457 'ie_key': DRTVSeasonIE
.ie_key(),
458 'title': season
.get('title'),
459 'series': traverse_obj(data
, ('entries', 0, 'item', 'title')),
460 'season_number': traverse_obj(data
, ('entries', 0, 'item', 'seasonNumber'))
461 } for season
in traverse_obj(data
, ('entries', 0, 'item', 'show', 'seasons', 'items'))]
466 'display_id': display_id
,
467 'title': traverse_obj(data
, ('entries', 0, 'item', 'title')),
468 'series': traverse_obj(data
, ('entries', 0, 'item', 'title')),