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
26 class DRTVIE(InfoExtractor):
27 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv/se|nyheder|radio/ondemand)/(?:[^/]+/)*(?P<id>[\da-z-]+)(?:[/#?]|$)'
29 _GEO_COUNTRIES = ['DK']
32 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
33 'md5': '25e659cccc9a2ed956110a299fdf5983',
35 'id': 'klassen-darlig-taber-10',
37 'title': 'Klassen - Dårlig taber (10)',
38 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
39 'timestamp': 1539085800,
40 'upload_date': '20181009',
43 'season': 'Klassen I',
45 'season_id': 'urn:dr:mu:bundle:57d7e8216187a4031cfd6f6b',
46 'episode': 'Episode 10',
50 'expected_warnings': ['Unable to download f4m manifest'],
53 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
55 'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6',
57 'title': 'christiania pusher street ryddes drdkrjpo',
58 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
59 'timestamp': 1472800279,
60 'upload_date': '20160902',
64 'skip_download': True,
66 'expected_warnings': ['Unable to download f4m manifest'],
68 # with SignLanguage formats
69 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
71 'id': 'historien-om-danmark-stenalder',
73 'title': 'Historien om Danmark: Stenalder',
74 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
75 'timestamp': 1546628400,
76 'upload_date': '20190104',
78 'formats': 'mincount:20',
81 'skip_download': True,
85 def _real_extract(self, url):
86 video_id = self._match_id(url)
88 webpage = self._download_webpage(url, video_id)
90 if '>Programmet er ikke længere tilgængeligt' in webpage:
92 'Video %s is not available' % video_id, expected=True)
94 video_id = self._search_regex(
95 (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
96 r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
97 webpage, 'video id', default=None)
100 video_id = compat_urllib_parse_unquote(self._search_regex(
101 r'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
104 data = self._download_json(
105 'https://www.dr.dk/mu-online/api/1.4/programcard/%s' % video_id,
106 video_id, 'Downloading video JSON', query={'expanded': 'true'})
108 title = str_or_none(data.get('Title')) or re.sub(
109 r'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '',
110 self._og_search_title(webpage))
111 description = self._og_search_description(
112 webpage, default=None) or data.get('Description')
114 timestamp = unified_timestamp(
115 data.get('PrimaryBroadcastStartTime') or data.get('SortDateTime'))
120 restricted_to_denmark = False
126 primary_asset = data.get('PrimaryAsset')
127 if isinstance(primary_asset, dict):
128 assets.append(primary_asset)
129 secondary_assets = data.get('SecondaryAssets')
130 if isinstance(secondary_assets, list):
131 for secondary_asset in secondary_assets:
132 if isinstance(secondary_asset, dict):
133 assets.append(secondary_asset)
135 def hex_to_bytes(hex):
136 return binascii.a2b_hex(hex.encode('ascii'))
141 data = bytes_to_intlist(hex_to_bytes(e[10:10 + n]))
142 key = bytes_to_intlist(hashlib.sha256(
143 ('%s:sRBzYNXBzkKgnjj8pGtkACch' % a).encode('utf-8')).digest())
144 iv = bytes_to_intlist(hex_to_bytes(a))
145 decrypted = aes_cbc_decrypt(data, key, iv)
146 return intlist_to_bytes(
147 decrypted[:-decrypted[-1]]).decode('utf-8').split('?')[0]
150 kind = asset.get('Kind')
152 thumbnail = url_or_none(asset.get('Uri'))
153 elif kind in ('VideoResource', 'AudioResource'):
154 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
155 restricted_to_denmark = asset.get('RestrictedToDenmark')
156 asset_target = asset.get('Target')
157 for link in asset.get('Links', []):
158 uri = link.get('Uri')
160 encrypted_uri = link.get('EncryptedUri')
161 if not encrypted_uri:
164 uri = decrypt_uri(encrypted_uri)
167 'Unable to decrypt EncryptedUri', video_id)
169 uri = url_or_none(uri)
172 target = link.get('Target')
173 format_id = target or ''
175 if asset_target in ('SpokenSubtitles', 'SignLanguage'):
177 format_id += '-%s' % asset_target
179 f4m_formats = self._extract_f4m_formats(
180 uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
181 video_id, preference, f4m_id=format_id, fatal=False)
182 if kind == 'AudioResource':
183 for f in f4m_formats:
185 formats.extend(f4m_formats)
186 elif target == 'HLS':
187 formats.extend(self._extract_m3u8_formats(
188 uri, video_id, 'mp4', entry_protocol='m3u8_native',
189 preference=preference, m3u8_id=format_id,
192 bitrate = link.get('Bitrate')
194 format_id += '-%s' % bitrate
197 'format_id': format_id,
198 'tbr': int_or_none(bitrate),
199 'ext': link.get('FileFormat'),
200 'vcodec': 'none' if kind == 'AudioResource' else None,
201 'preference': preference,
203 subtitles_list = asset.get('SubtitlesList') or asset.get('Subtitleslist')
204 if isinstance(subtitles_list, list):
208 for subs in subtitles_list:
209 if not isinstance(subs, dict):
211 sub_uri = url_or_none(subs.get('Uri'))
214 lang = subs.get('Language') or 'da'
215 subtitles.setdefault(LANGS.get(lang, lang), []).append({
217 'ext': mimetype2ext(subs.get('MimeType')) or 'vtt'
220 if not formats and restricted_to_denmark:
221 self.raise_geo_restricted(
222 'Unfortunately, DR is not allowed to show this program outside Denmark.',
223 countries=self._GEO_COUNTRIES)
225 self._sort_formats(formats)
230 'description': description,
231 'thumbnail': thumbnail,
232 'timestamp': timestamp,
233 'duration': duration,
235 'subtitles': subtitles,
236 'series': str_or_none(data.get('SeriesTitle')),
237 'season': str_or_none(data.get('SeasonTitle')),
238 'season_number': int_or_none(data.get('SeasonNumber')),
239 'season_id': str_or_none(data.get('SeasonUrn')),
240 'episode': str_or_none(data.get('EpisodeTitle')),
241 'episode_number': int_or_none(data.get('EpisodeNumber')),
242 'release_year': int_or_none(data.get('ProductionYear')),
246 class DRTVLiveIE(InfoExtractor):
247 IE_NAME = 'drtv:live'
248 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
249 _GEO_COUNTRIES = ['DK']
251 'url': 'https://www.dr.dk/tv/live/dr1',
255 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
259 'skip_download': True,
263 def _real_extract(self, url):
264 channel_id = self._match_id(url)
265 channel_data = self._download_json(
266 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id,
268 title = self._live_title(channel_data['Title'])
271 for streaming_server in channel_data.get('StreamingServers', []):
272 server = streaming_server.get('Server')
275 link_type = streaming_server.get('LinkType')
276 for quality in streaming_server.get('Qualities', []):
277 for stream in quality.get('Streams', []):
278 stream_path = stream.get('Stream')
281 stream_url = update_url_query(
282 '%s/%s' % (server, stream_path), {'b': ''})
283 if link_type == 'HLS':
284 formats.extend(self._extract_m3u8_formats(
285 stream_url, channel_id, 'mp4',
286 m3u8_id=link_type, fatal=False, live=True))
287 elif link_type == 'HDS':
288 formats.extend(self._extract_f4m_formats(update_url_query(
289 '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}),
290 channel_id, f4m_id=link_type, fatal=False))
291 self._sort_formats(formats)
296 'thumbnail': channel_data.get('PrimaryImageUri'),