]>
Commit | Line | Data |
---|---|---|
7d254639 | 1 | # coding: utf-8 |
f2b8db57 S |
2 | from __future__ import unicode_literals |
3 | ||
a2d821d7 S |
4 | import binascii |
5 | import hashlib | |
6 | import re | |
7 | ||
8 | ||
1335c3ac | 9 | from .common import InfoExtractor |
1d3586d0 | 10 | from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7 |
a2d821d7 | 11 | from ..compat import compat_urllib_parse_unquote |
1335c3ac S |
12 | from ..utils import ( |
13 | ExtractorError, | |
6066d03d S |
14 | int_or_none, |
15 | float_or_none, | |
16 | mimetype2ext, | |
a2d821d7 | 17 | str_or_none, |
5709d661 | 18 | try_get, |
a2d821d7 | 19 | unified_timestamp, |
2c15db82 | 20 | update_url_query, |
a2d821d7 | 21 | url_or_none, |
1335c3ac | 22 | ) |
f2b8db57 S |
23 | |
24 | ||
18c1c424 | 25 | class DRTVIE(InfoExtractor): |
5709d661 S |
26 | _VALID_URL = r'''(?x) |
27 | https?:// | |
28 | (?: | |
29 | (?:www\.)?dr\.dk/(?:tv/se|nyheder|radio(?:/ondemand)?)/(?:[^/]+/)*| | |
29f7c58a | 30 | (?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/(?:se|episode|program)/ |
5709d661 S |
31 | ) |
32 | (?P<id>[\da-z_-]+) | |
33 | ''' | |
96182695 S |
34 | _GEO_BYPASS = False |
35 | _GEO_COUNTRIES = ['DK'] | |
2c15db82 | 36 | IE_NAME = 'drtv' |
5e9e3d0f | 37 | _TESTS = [{ |
3fcce302 | 38 | 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10', |
a2d821d7 | 39 | 'md5': '25e659cccc9a2ed956110a299fdf5983', |
f2b8db57 | 40 | 'info_dict': { |
3fcce302 | 41 | 'id': 'klassen-darlig-taber-10', |
f2b8db57 | 42 | 'ext': 'mp4', |
3fcce302 S |
43 | 'title': 'Klassen - Dårlig taber (10)', |
44 | 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa', | |
a2d821d7 S |
45 | 'timestamp': 1539085800, |
46 | 'upload_date': '20181009', | |
3fcce302 | 47 | 'duration': 606.84, |
a2d821d7 S |
48 | 'series': 'Klassen', |
49 | 'season': 'Klassen I', | |
50 | 'season_number': 1, | |
51 | 'season_id': 'urn:dr:mu:bundle:57d7e8216187a4031cfd6f6b', | |
52 | 'episode': 'Episode 10', | |
53 | 'episode_number': 10, | |
54 | 'release_year': 2016, | |
3fcce302 | 55 | }, |
a2d821d7 | 56 | 'expected_warnings': ['Unable to download f4m manifest'], |
5e9e3d0f | 57 | }, { |
8d65880e | 58 | # embed |
5e9e3d0f | 59 | 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang', |
5e9e3d0f | 60 | 'info_dict': { |
a2d821d7 | 61 | 'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6', |
5e9e3d0f | 62 | 'ext': 'mp4', |
a2d821d7 | 63 | 'title': 'christiania pusher street ryddes drdkrjpo', |
8d65880e | 64 | 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5', |
5e9e3d0f SB |
65 | 'timestamp': 1472800279, |
66 | 'upload_date': '20160902', | |
67 | 'duration': 131.4, | |
3fcce302 | 68 | }, |
8d65880e S |
69 | 'params': { |
70 | 'skip_download': True, | |
71 | }, | |
a2d821d7 | 72 | 'expected_warnings': ['Unable to download f4m manifest'], |
b972fb03 | 73 | }, { |
8d65880e | 74 | # with SignLanguage formats |
b972fb03 | 75 | 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder', |
b972fb03 RR |
76 | 'info_dict': { |
77 | 'id': 'historien-om-danmark-stenalder', | |
78 | 'ext': 'mp4', | |
a2d821d7 | 79 | 'title': 'Historien om Danmark: Stenalder', |
8d65880e | 80 | 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a', |
a2d821d7 S |
81 | 'timestamp': 1546628400, |
82 | 'upload_date': '20190104', | |
83 | 'duration': 3502.56, | |
8d65880e S |
84 | 'formats': 'mincount:20', |
85 | }, | |
86 | 'params': { | |
87 | 'skip_download': True, | |
b972fb03 | 88 | }, |
f5629946 S |
89 | }, { |
90 | 'url': 'https://www.dr.dk/radio/p4kbh/regionale-nyheder-kh4/p4-nyheder-2019-06-26-17-30-9', | |
91 | 'only_matching': True, | |
5709d661 S |
92 | }, { |
93 | 'url': 'https://www.dr.dk/drtv/se/bonderoeven_71769', | |
94 | 'info_dict': { | |
95 | 'id': '00951930010', | |
96 | 'ext': 'mp4', | |
97 | 'title': 'Bonderøven (1:8)', | |
98 | 'description': 'md5:3cf18fc0d3b205745d4505f896af8121', | |
99 | 'timestamp': 1546542000, | |
100 | 'upload_date': '20190103', | |
101 | 'duration': 2576.6, | |
102 | }, | |
103 | 'params': { | |
104 | 'skip_download': True, | |
105 | }, | |
106 | }, { | |
107 | 'url': 'https://www.dr.dk/drtv/episode/bonderoeven_71769', | |
108 | 'only_matching': True, | |
109 | }, { | |
110 | 'url': 'https://dr-massive.com/drtv/se/bonderoeven_71769', | |
111 | 'only_matching': True, | |
29f7c58a | 112 | }, { |
113 | 'url': 'https://www.dr.dk/drtv/program/jagten_220924', | |
114 | 'only_matching': True, | |
5e9e3d0f | 115 | }] |
f2b8db57 S |
116 | |
117 | def _real_extract(self, url): | |
6ad4013d | 118 | video_id = self._match_id(url) |
f2b8db57 | 119 | |
78271e33 S |
120 | webpage = self._download_webpage(url, video_id) |
121 | ||
aff84bec S |
122 | if '>Programmet er ikke længere tilgængeligt' in webpage: |
123 | raise ExtractorError( | |
124 | 'Video %s is not available' % video_id, expected=True) | |
125 | ||
78271e33 | 126 | video_id = self._search_regex( |
5e9e3d0f | 127 | (r'data-(?:material-identifier|episode-slug)="([^"]+)"', |
a2d821d7 S |
128 | r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'), |
129 | webpage, 'video id', default=None) | |
130 | ||
131 | if not video_id: | |
5709d661 | 132 | video_id = self._search_regex( |
a2d821d7 | 133 | r'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)', |
5709d661 S |
134 | webpage, 'urn', default=None) |
135 | if video_id: | |
136 | video_id = compat_urllib_parse_unquote(video_id) | |
137 | ||
138 | _PROGRAMCARD_BASE = 'https://www.dr.dk/mu-online/api/1.4/programcard' | |
139 | query = {'expanded': 'true'} | |
140 | ||
141 | if video_id: | |
142 | programcard_url = '%s/%s' % (_PROGRAMCARD_BASE, video_id) | |
143 | else: | |
144 | programcard_url = _PROGRAMCARD_BASE | |
145 | page = self._parse_json( | |
146 | self._search_regex( | |
147 | r'data\s*=\s*({.+?})\s*(?:;|</script)', webpage, | |
148 | 'data'), '1')['cache']['page'] | |
149 | page = page[list(page.keys())[0]] | |
150 | item = try_get( | |
151 | page, (lambda x: x['item'], lambda x: x['entries'][0]['item']), | |
152 | dict) | |
153 | video_id = item['customId'].split(':')[-1] | |
154 | query['productionnumber'] = video_id | |
f2b8db57 | 155 | |
1602a240 | 156 | data = self._download_json( |
5709d661 | 157 | programcard_url, video_id, 'Downloading video JSON', query=query) |
f2b8db57 | 158 | |
a2d821d7 S |
159 | title = str_or_none(data.get('Title')) or re.sub( |
160 | r'\s*\|\s*(?:TV\s*\|\s*DR|DRTV)$', '', | |
161 | self._og_search_title(webpage)) | |
6066d03d S |
162 | description = self._og_search_description( |
163 | webpage, default=None) or data.get('Description') | |
5e9e3d0f | 164 | |
a2d821d7 S |
165 | timestamp = unified_timestamp( |
166 | data.get('PrimaryBroadcastStartTime') or data.get('SortDateTime')) | |
f2b8db57 S |
167 | |
168 | thumbnail = None | |
169 | duration = None | |
170 | ||
171 | restricted_to_denmark = False | |
172 | ||
173 | formats = [] | |
174 | subtitles = {} | |
175 | ||
a2d821d7 S |
176 | assets = [] |
177 | primary_asset = data.get('PrimaryAsset') | |
178 | if isinstance(primary_asset, dict): | |
179 | assets.append(primary_asset) | |
180 | secondary_assets = data.get('SecondaryAssets') | |
181 | if isinstance(secondary_assets, list): | |
182 | for secondary_asset in secondary_assets: | |
183 | if isinstance(secondary_asset, dict): | |
184 | assets.append(secondary_asset) | |
185 | ||
186 | def hex_to_bytes(hex): | |
187 | return binascii.a2b_hex(hex.encode('ascii')) | |
188 | ||
189 | def decrypt_uri(e): | |
190 | n = int(e[2:10], 16) | |
191 | a = e[10 + n:] | |
1d3586d0 | 192 | data = hex_to_bytes(e[10:10 + n]) |
193 | key = hashlib.sha256(('%s:sRBzYNXBzkKgnjj8pGtkACch' % a).encode('utf-8')).digest() | |
194 | iv = hex_to_bytes(a) | |
195 | decrypted = unpad_pkcs7(aes_cbc_decrypt_bytes(data, key, iv)) | |
196 | return decrypted.decode('utf-8').split('?')[0] | |
a2d821d7 S |
197 | |
198 | for asset in assets: | |
2c15db82 RA |
199 | kind = asset.get('Kind') |
200 | if kind == 'Image': | |
a2d821d7 | 201 | thumbnail = url_or_none(asset.get('Uri')) |
8d65880e | 202 | elif kind in ('VideoResource', 'AudioResource'): |
6066d03d S |
203 | duration = float_or_none(asset.get('DurationInMilliseconds'), 1000) |
204 | restricted_to_denmark = asset.get('RestrictedToDenmark') | |
8d65880e | 205 | asset_target = asset.get('Target') |
6066d03d S |
206 | for link in asset.get('Links', []): |
207 | uri = link.get('Uri') | |
a2d821d7 S |
208 | if not uri: |
209 | encrypted_uri = link.get('EncryptedUri') | |
210 | if not encrypted_uri: | |
211 | continue | |
212 | try: | |
213 | uri = decrypt_uri(encrypted_uri) | |
214 | except Exception: | |
215 | self.report_warning( | |
216 | 'Unable to decrypt EncryptedUri', video_id) | |
217 | continue | |
218 | uri = url_or_none(uri) | |
6066d03d S |
219 | if not uri: |
220 | continue | |
221 | target = link.get('Target') | |
222 | format_id = target or '' | |
49fe4175 | 223 | if asset_target in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'): |
b972fb03 | 224 | preference = -1 |
8d65880e | 225 | format_id += '-%s' % asset_target |
49fe4175 S |
226 | elif asset_target == 'Default': |
227 | preference = 1 | |
228 | else: | |
229 | preference = None | |
1335c3ac | 230 | if target == 'HDS': |
2c15db82 | 231 | f4m_formats = self._extract_f4m_formats( |
1335c3ac | 232 | uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43', |
9a0942ad | 233 | video_id, preference, f4m_id=format_id, fatal=False) |
2c15db82 RA |
234 | if kind == 'AudioResource': |
235 | for f in f4m_formats: | |
236 | f['vcodec'] = 'none' | |
237 | formats.extend(f4m_formats) | |
1335c3ac S |
238 | elif target == 'HLS': |
239 | formats.extend(self._extract_m3u8_formats( | |
6066d03d | 240 | uri, video_id, 'mp4', entry_protocol='m3u8_native', |
f983b875 | 241 | quality=preference, m3u8_id=format_id, |
9a0942ad | 242 | fatal=False)) |
1335c3ac S |
243 | else: |
244 | bitrate = link.get('Bitrate') | |
245 | if bitrate: | |
246 | format_id += '-%s' % bitrate | |
247 | formats.append({ | |
248 | 'url': uri, | |
249 | 'format_id': format_id, | |
6066d03d | 250 | 'tbr': int_or_none(bitrate), |
1335c3ac | 251 | 'ext': link.get('FileFormat'), |
2c15db82 | 252 | 'vcodec': 'none' if kind == 'AudioResource' else None, |
f983b875 | 253 | 'quality': preference, |
1335c3ac | 254 | }) |
a2d821d7 S |
255 | subtitles_list = asset.get('SubtitlesList') or asset.get('Subtitleslist') |
256 | if isinstance(subtitles_list, list): | |
257 | LANGS = { | |
258 | 'Danish': 'da', | |
259 | } | |
260 | for subs in subtitles_list: | |
261 | if not isinstance(subs, dict): | |
262 | continue | |
263 | sub_uri = url_or_none(subs.get('Uri')) | |
264 | if not sub_uri: | |
265 | continue | |
266 | lang = subs.get('Language') or 'da' | |
267 | subtitles.setdefault(LANGS.get(lang, lang), []).append({ | |
268 | 'url': sub_uri, | |
269 | 'ext': mimetype2ext(subs.get('MimeType')) or 'vtt' | |
270 | }) | |
f2b8db57 S |
271 | |
272 | if not formats and restricted_to_denmark: | |
6066d03d S |
273 | self.raise_geo_restricted( |
274 | 'Unfortunately, DR is not allowed to show this program outside Denmark.', | |
96182695 | 275 | countries=self._GEO_COUNTRIES) |
f2b8db57 S |
276 | |
277 | self._sort_formats(formats) | |
278 | ||
f2b8db57 S |
279 | return { |
280 | 'id': video_id, | |
281 | 'title': title, | |
282 | 'description': description, | |
283 | 'thumbnail': thumbnail, | |
284 | 'timestamp': timestamp, | |
285 | 'duration': duration, | |
286 | 'formats': formats, | |
18c1c424 | 287 | 'subtitles': subtitles, |
a2d821d7 S |
288 | 'series': str_or_none(data.get('SeriesTitle')), |
289 | 'season': str_or_none(data.get('SeasonTitle')), | |
290 | 'season_number': int_or_none(data.get('SeasonNumber')), | |
291 | 'season_id': str_or_none(data.get('SeasonUrn')), | |
292 | 'episode': str_or_none(data.get('EpisodeTitle')), | |
293 | 'episode_number': int_or_none(data.get('EpisodeNumber')), | |
294 | 'release_year': int_or_none(data.get('ProductionYear')), | |
f2b8db57 | 295 | } |
2c15db82 RA |
296 | |
297 | ||
298 | class DRTVLiveIE(InfoExtractor): | |
299 | IE_NAME = 'drtv:live' | |
300 | _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)' | |
fc11ad38 | 301 | _GEO_COUNTRIES = ['DK'] |
2c15db82 RA |
302 | _TEST = { |
303 | 'url': 'https://www.dr.dk/tv/live/dr1', | |
304 | 'info_dict': { | |
305 | 'id': 'dr1', | |
306 | 'ext': 'mp4', | |
307 | 'title': 're:^DR1 [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
308 | }, | |
309 | 'params': { | |
310 | # m3u8 download | |
311 | 'skip_download': True, | |
312 | }, | |
313 | } | |
314 | ||
315 | def _real_extract(self, url): | |
316 | channel_id = self._match_id(url) | |
317 | channel_data = self._download_json( | |
318 | 'https://www.dr.dk/mu-online/api/1.0/channel/' + channel_id, | |
319 | channel_id) | |
39ca3b5c | 320 | title = channel_data['Title'] |
2c15db82 RA |
321 | |
322 | formats = [] | |
323 | for streaming_server in channel_data.get('StreamingServers', []): | |
324 | server = streaming_server.get('Server') | |
325 | if not server: | |
326 | continue | |
327 | link_type = streaming_server.get('LinkType') | |
328 | for quality in streaming_server.get('Qualities', []): | |
329 | for stream in quality.get('Streams', []): | |
330 | stream_path = stream.get('Stream') | |
331 | if not stream_path: | |
332 | continue | |
333 | stream_url = update_url_query( | |
334 | '%s/%s' % (server, stream_path), {'b': ''}) | |
335 | if link_type == 'HLS': | |
336 | formats.extend(self._extract_m3u8_formats( | |
337 | stream_url, channel_id, 'mp4', | |
338 | m3u8_id=link_type, fatal=False, live=True)) | |
339 | elif link_type == 'HDS': | |
340 | formats.extend(self._extract_f4m_formats(update_url_query( | |
341 | '%s/%s' % (server, stream_path), {'hdcore': '3.7.0'}), | |
342 | channel_id, f4m_id=link_type, fatal=False)) | |
343 | self._sort_formats(formats) | |
344 | ||
345 | return { | |
346 | 'id': channel_id, | |
347 | 'title': title, | |
348 | 'thumbnail': channel_data.get('PrimaryImageUri'), | |
349 | 'formats': formats, | |
350 | 'is_live': True, | |
351 | } |