2 from __future__
import unicode_literals
9 from .common
import InfoExtractor
10 from ..aes
import aes_cbc_decrypt
11 from ..compat
import compat_urllib_parse_unquote
27 class DRTVIE(InfoExtractor
):
31 (?:www\.)?dr\.dk/(?:tv/se|nyheder|radio(?:/ondemand)?)/(?:[^/]+/)*|
32 (?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/(?:se|episode)/
37 _GEO_COUNTRIES
= ['DK']
40 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
41 'md5': '25e659cccc9a2ed956110a299fdf5983',
43 'id': 'klassen-darlig-taber-10',
45 'title': 'Klassen - Dårlig taber (10)',
46 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
47 'timestamp': 1539085800,
48 'upload_date': '20181009',
51 'season': 'Klassen I',
53 'season_id': 'urn:dr:mu:bundle:57d7e8216187a4031cfd6f6b',
54 'episode': 'Episode 10',
58 'expected_warnings': ['Unable to download f4m manifest'],
61 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
63 'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6',
65 'title': 'christiania pusher street ryddes drdkrjpo',
66 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
67 'timestamp': 1472800279,
68 'upload_date': '20160902',
72 'skip_download': True,
74 'expected_warnings': ['Unable to download f4m manifest'],
76 # with SignLanguage formats
77 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
79 'id': 'historien-om-danmark-stenalder',
81 'title': 'Historien om Danmark: Stenalder',
82 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
83 'timestamp': 1546628400,
84 'upload_date': '20190104',
86 'formats': 'mincount:20',
89 'skip_download': True,
92 'url': 'https://www.dr.dk/radio/p4kbh/regionale-nyheder-kh4/p4-nyheder-2019-06-26-17-30-9',
93 'only_matching': True,
95 'url': 'https://www.dr.dk/drtv/se/bonderoeven_71769',
99 'title': 'Bonderøven (1:8)',
100 'description': 'md5:3cf18fc0d3b205745d4505f896af8121',
101 'timestamp': 1546542000,
102 'upload_date': '20190103',
106 'skip_download': True,
109 'url': 'https://www.dr.dk/drtv/episode/bonderoeven_71769',
110 'only_matching': True,
112 'url': 'https://dr-massive.com/drtv/se/bonderoeven_71769',
113 'only_matching': True,
116 def _real_extract(self
, url
):
117 video_id
= self
._match
_id
(url
)
119 webpage
= self
._download
_webpage
(url
, video_id
)
121 if '>Programmet er ikke længere tilgængeligt' in webpage
:
122 raise ExtractorError(
123 'Video %s is not available' % video_id
, expected
=True)
125 video_id
= self
._search
_regex
(
126 (r
'data-(?:material-identifier|episode-slug)="([^"]+)"',
127 r
'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
128 webpage
, 'video id', default
=None)
131 video_id
= self
._search
_regex
(
132 r
'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
133 webpage
, 'urn', default
=None)
135 video_id
= compat_urllib_parse_unquote(video_id
)
137 _PROGRAMCARD_BASE
= 'https://www.dr.dk/mu-online/api/1.4/programcard'
138 query
= {'expanded': 'true'}
141 programcard_url
= '%s/%s' % (_PROGRAMCARD_BASE
, video_id
)
143 programcard_url
= _PROGRAMCARD_BASE
144 page
= self
._parse
_json
(
146 r
'data\s*=\s*({.+?})\s*(?:;|</script)', webpage
,
147 'data'), '1')['cache']['page']
148 page
= page
[list(page
.keys())[0]]
150 page
, (lambda x
: x
['item'], lambda x
: x
['entries'][0]['item']),
152 video_id
= item
['customId'].split(':')[-1]
153 query
['productionnumber'] = video_id
155 data
= self
._download
_json
(
156 programcard_url
, video_id
, 'Downloading video JSON', query
=query
)
158 title
= str_or_none(data
.get('Title')) or re
.sub(
159 r
'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '',
160 self
._og
_search
_title
(webpage
))
161 description
= self
._og
_search
_description
(
162 webpage
, default
=None) or data
.get('Description')
164 timestamp
= unified_timestamp(
165 data
.get('PrimaryBroadcastStartTime') or data
.get('SortDateTime'))
170 restricted_to_denmark
= False
176 primary_asset
= data
.get('PrimaryAsset')
177 if isinstance(primary_asset
, dict):
178 assets
.append(primary_asset
)
179 secondary_assets
= data
.get('SecondaryAssets')
180 if isinstance(secondary_assets
, list):
181 for secondary_asset
in secondary_assets
:
182 if isinstance(secondary_asset
, dict):
183 assets
.append(secondary_asset
)
185 def hex_to_bytes(hex):
186 return binascii
.a2b_hex(hex.encode('ascii'))
191 data
= bytes_to_intlist(hex_to_bytes(e
[10:10 + n
]))
192 key
= bytes_to_intlist(hashlib
.sha256(
193 ('%s:sRBzYNXBzkKgnjj8pGtkACch' % a
).encode('utf-8')).digest())
194 iv
= bytes_to_intlist(hex_to_bytes(a
))
195 decrypted
= aes_cbc_decrypt(data
, key
, iv
)
196 return intlist_to_bytes(
197 decrypted
[:-decrypted
[-1]]).decode('utf-8').split('?')[0]
200 kind
= asset
.get('Kind')
202 thumbnail
= url_or_none(asset
.get('Uri'))
203 elif kind
in ('VideoResource', 'AudioResource'):
204 duration
= float_or_none(asset
.get('DurationInMilliseconds'), 1000)
205 restricted_to_denmark
= asset
.get('RestrictedToDenmark')
206 asset_target
= asset
.get('Target')
207 for link
in asset
.get('Links', []):
208 uri
= link
.get('Uri')
210 encrypted_uri
= link
.get('EncryptedUri')
211 if not encrypted_uri
:
214 uri
= decrypt_uri(encrypted_uri
)
217 'Unable to decrypt EncryptedUri', video_id
)
219 uri
= url_or_none(uri
)
222 target
= link
.get('Target')
223 format_id
= target
or ''
224 if asset_target
in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
226 format_id
+= '-%s' % asset_target
227 elif asset_target
== 'Default':
232 f4m_formats
= self
._extract
_f
4m
_formats
(
233 uri
+ '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
234 video_id
, preference
, f4m_id
=format_id
, fatal
=False)
235 if kind
== 'AudioResource':
236 for f
in f4m_formats
:
238 formats
.extend(f4m_formats
)
239 elif target
== 'HLS':
240 formats
.extend(self
._extract
_m
3u8_formats
(
241 uri
, video_id
, 'mp4', entry_protocol
='m3u8_native',
242 preference
=preference
, m3u8_id
=format_id
,
245 bitrate
= link
.get('Bitrate')
247 format_id
+= '-%s' % bitrate
250 'format_id': format_id
,
251 'tbr': int_or_none(bitrate
),
252 'ext': link
.get('FileFormat'),
253 'vcodec': 'none' if kind
== 'AudioResource' else None,
254 'preference': preference
,
256 subtitles_list
= asset
.get('SubtitlesList') or asset
.get('Subtitleslist')
257 if isinstance(subtitles_list
, list):
261 for subs
in subtitles_list
:
262 if not isinstance(subs
, dict):
264 sub_uri
= url_or_none(subs
.get('Uri'))
267 lang
= subs
.get('Language') or 'da'
268 subtitles
.setdefault(LANGS
.get(lang
, lang
), []).append({
270 'ext': mimetype2ext(subs
.get('MimeType')) or 'vtt'
273 if not formats
and restricted_to_denmark
:
274 self
.raise_geo_restricted(
275 'Unfortunately, DR is not allowed to show this program outside Denmark.',
276 countries
=self
._GEO
_COUNTRIES
)
278 self
._sort
_formats
(formats
)
283 'description': description
,
284 'thumbnail': thumbnail
,
285 'timestamp': timestamp
,
286 'duration': duration
,
288 'subtitles': subtitles
,
289 'series': str_or_none(data
.get('SeriesTitle')),
290 'season': str_or_none(data
.get('SeasonTitle')),
291 'season_number': int_or_none(data
.get('SeasonNumber')),
292 'season_id': str_or_none(data
.get('SeasonUrn')),
293 'episode': str_or_none(data
.get('EpisodeTitle')),
294 'episode_number': int_or_none(data
.get('EpisodeNumber')),
295 'release_year': int_or_none(data
.get('ProductionYear')),
299 class DRTVLiveIE(InfoExtractor
):
300 IE_NAME
= 'drtv:live'
301 _VALID_URL
= r
'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
302 _GEO_COUNTRIES
= ['DK']
304 'url': 'https://www.dr.dk/tv/live/dr1',
308 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
312 'skip_download': True,
316 def _real_extract(self
, url
):
317 channel_id
= self
._match
_id
(url
)
318 channel_data
= self
._download
_json
(
319 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id
,
321 title
= self
._live
_title
(channel_data
['Title'])
324 for streaming_server
in channel_data
.get('StreamingServers', []):
325 server
= streaming_server
.get('Server')
328 link_type
= streaming_server
.get('LinkType')
329 for quality
in streaming_server
.get('Qualities', []):
330 for stream
in quality
.get('Streams', []):
331 stream_path
= stream
.get('Stream')
334 stream_url
= update_url_query(
335 '%s/%s' % (server
, stream_path
), {'b': ''})
336 if link_type
== 'HLS':
337 formats
.extend(self
._extract
_m
3u8_formats
(
338 stream_url
, channel_id
, 'mp4',
339 m3u8_id
=link_type
, fatal
=False, live
=True))
340 elif link_type
== 'HDS':
341 formats
.extend(self
._extract
_f
4m
_formats
(update_url_query(
342 '%s/%s' % (server
, stream_path
), {'hdcore': '3.7.0'}),
343 channel_id
, f4m_id
=link_type
, fatal
=False))
344 self
._sort
_formats
(formats
)
349 'thumbnail': channel_data
.get('PrimaryImageUri'),