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