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|program)/
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,
115 'url': 'https://www.dr.dk/drtv/program/jagten_220924',
116 'only_matching': True,
119 def _real_extract(self, url):
120 video_id = self._match_id(url)
122 webpage = self._download_webpage(url, video_id)
124 if '>Programmet er ikke længere tilgængeligt' in webpage:
125 raise ExtractorError(
126 'Video %s is not available' % video_id, expected=True)
128 video_id = self._search_regex(
129 (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
130 r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
131 webpage, 'video id', default=None)
134 video_id = self._search_regex(
135 r'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
136 webpage, 'urn', default=None)
138 video_id = compat_urllib_parse_unquote(video_id)
140 _PROGRAMCARD_BASE = 'https://www.dr.dk/mu-online/api/1.4/programcard'
141 query = {'expanded': 'true'}
144 programcard_url = '%s/%s' % (_PROGRAMCARD_BASE, video_id)
146 programcard_url = _PROGRAMCARD_BASE
147 page = self._parse_json(
149 r'data\s*=\s*({.+?})\s*(?:;|</script)', webpage,
150 'data'), '1')['cache']['page']
151 page = page[list(page.keys())[0]]
153 page, (lambda x: x['item'], lambda x: x['entries'][0]['item']),
155 video_id = item['customId'].split(':')[-1]
156 query['productionnumber'] = video_id
158 data = self._download_json(
159 programcard_url, video_id, 'Downloading video JSON', query=query)
161 title = str_or_none(data.get('Title')) or re.sub(
162 r'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '',
163 self._og_search_title(webpage))
164 description = self._og_search_description(
165 webpage, default=None) or data.get('Description')
167 timestamp = unified_timestamp(
168 data.get('PrimaryBroadcastStartTime') or data.get('SortDateTime'))
173 restricted_to_denmark = False
179 primary_asset = data.get('PrimaryAsset')
180 if isinstance(primary_asset, dict):
181 assets.append(primary_asset)
182 secondary_assets = data.get('SecondaryAssets')
183 if isinstance(secondary_assets, list):
184 for secondary_asset in secondary_assets:
185 if isinstance(secondary_asset, dict):
186 assets.append(secondary_asset)
188 def hex_to_bytes(hex):
189 return binascii.a2b_hex(hex.encode('ascii'))
194 data = bytes_to_intlist(hex_to_bytes(e[10:10 + n]))
195 key = bytes_to_intlist(hashlib.sha256(
196 ('%s:sRBzYNXBzkKgnjj8pGtkACch' % a).encode('utf-8')).digest())
197 iv = bytes_to_intlist(hex_to_bytes(a))
198 decrypted = aes_cbc_decrypt(data, key, iv)
199 return intlist_to_bytes(
200 decrypted[:-decrypted[-1]]).decode('utf-8').split('?')[0]
203 kind = asset.get('Kind')
205 thumbnail = url_or_none(asset.get('Uri'))
206 elif kind in ('VideoResource', 'AudioResource'):
207 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
208 restricted_to_denmark = asset.get('RestrictedToDenmark')
209 asset_target = asset.get('Target')
210 for link in asset.get('Links', []):
211 uri = link.get('Uri')
213 encrypted_uri = link.get('EncryptedUri')
214 if not encrypted_uri:
217 uri = decrypt_uri(encrypted_uri)
220 'Unable to decrypt EncryptedUri', video_id)
222 uri = url_or_none(uri)
225 target = link.get('Target')
226 format_id = target or ''
227 if asset_target in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
229 format_id += '-%s' % asset_target
230 elif asset_target == 'Default':
235 f4m_formats = self._extract_f4m_formats(
236 uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
237 video_id, preference, f4m_id=format_id, fatal=False)
238 if kind == 'AudioResource':
239 for f in f4m_formats:
241 formats.extend(f4m_formats)
242 elif target == 'HLS':
243 formats.extend(self._extract_m3u8_formats(
244 uri, video_id, 'mp4', entry_protocol='m3u8_native',
245 preference=preference, m3u8_id=format_id,
248 bitrate = link.get('Bitrate')
250 format_id += '-%s' % bitrate
253 'format_id': format_id,
254 'tbr': int_or_none(bitrate),
255 'ext': link.get('FileFormat'),
256 'vcodec': 'none' if kind == 'AudioResource' else None,
257 'preference': preference,
259 subtitles_list = asset.get('SubtitlesList') or asset.get('Subtitleslist')
260 if isinstance(subtitles_list, list):
264 for subs in subtitles_list:
265 if not isinstance(subs, dict):
267 sub_uri = url_or_none(subs.get('Uri'))
270 lang = subs.get('Language') or 'da'
271 subtitles.setdefault(LANGS.get(lang, lang), []).append({
273 'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
276 if not formats and restricted_to_denmark:
277 self.raise_geo_restricted(
278 'Unfortunately, DR is not allowed to show this program outside Denmark.',
279 countries=self._GEO_COUNTRIES)
281 self._sort_formats(formats)
286 'description': description,
287 'thumbnail': thumbnail,
288 'timestamp': timestamp,
289 'duration': duration,
291 'subtitles': subtitles,
292 'series': str_or_none(data.get('SeriesTitle')),
293 'season': str_or_none(data.get('SeasonTitle')),
294 'season_number': int_or_none(data.get('SeasonNumber')),
295 'season_id': str_or_none(data.get('SeasonUrn')),
296 'episode': str_or_none(data.get('EpisodeTitle')),
297 'episode_number': int_or_none(data.get('EpisodeNumber')),
298 'release_year': int_or_none(data.get('ProductionYear')),
302 class DRTVLiveIE(InfoExtractor):
303 IE_NAME = 'drtv:live'
304 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
305 _GEO_COUNTRIES = ['DK']
307 'url': 'https://www.dr.dk/tv/live/dr1',
311 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
315 'skip_download': True,
319 def _real_extract(self, url):
320 channel_id = self._match_id(url)
321 channel_data = self._download_json(
322 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
324 title = self._live_title(channel_data['Title'])
327 for streaming_server in channel_data.get('StreamingServers', []):
328 server = streaming_server.get('Server')
331 link_type = streaming_server.get('LinkType')
332 for quality in streaming_server.get('Qualities', []):
333 for stream in quality.get('Streams', []):
334 stream_path = stream.get('Stream')
337 stream_url = update_url_query(
338 '%s/%s' % (server, stream_path), {'b': ''})
339 if link_type == 'HLS':
340 formats.extend(self._extract_m3u8_formats(
341 stream_url, channel_id, 'mp4',
342 m3u8_id=link_type, fatal=False, live=True))
343 elif link_type == 'HDS':
344 formats.extend(self._extract_f4m_formats(update_url_query(
345 '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}),
346 channel_id, f4m_id=link_type, fatal=False))
347 self._sort_formats(formats)
352 'thumbnail': channel_data.get('PrimaryImageUri'),