]> jfr.im git - yt-dlp.git/blame_incremental - yt_dlp/extractor/drtv.py
[utils] Better traceback for `ExtractorError`
[yt-dlp.git] / yt_dlp / extractor / drtv.py
... / ...
CommitLineData
1# coding: utf-8
2from __future__ import unicode_literals
3
4import binascii
5import hashlib
6import re
7
8
9from .common import InfoExtractor
10from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
11from ..compat import compat_urllib_parse_unquote
12from ..utils import (
13 ExtractorError,
14 int_or_none,
15 float_or_none,
16 mimetype2ext,
17 str_or_none,
18 try_get,
19 unified_timestamp,
20 update_url_query,
21 url_or_none,
22)
23
24
25class DRTVIE(InfoExtractor):
26 _VALID_URL = r'''(?x)
27 https?://
28 (?:
29 (?:www\.)?dr\.dk/(?:tv/se|nyheder|radio(?:/ondemand)?)/(?:[^/]+/)*|
30 (?:www\.)?(?:dr\.dk|dr-massive\.com)/drtv/(?:se|episode|program)/
31 )
32 (?P<id>[\da-z_-]+)
33 '''
34 _GEO_BYPASS = False
35 _GEO_COUNTRIES = ['DK']
36 IE_NAME = 'drtv'
37 _TESTS = [{
38 'url': 'https://www.dr.dk/tv/se/boern/ultra/klassen-ultra/klassen-darlig-taber-10',
39 'md5': '25e659cccc9a2ed956110a299fdf5983',
40 'info_dict': {
41 'id': 'klassen-darlig-taber-10',
42 'ext': 'mp4',
43 'title': 'Klassen - Dårlig taber (10)',
44 'description': 'md5:815fe1b7fa656ed80580f31e8b3c79aa',
45 'timestamp': 1539085800,
46 'upload_date': '20181009',
47 'duration': 606.84,
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,
55 },
56 'expected_warnings': ['Unable to download f4m manifest'],
57 }, {
58 # embed
59 'url': 'https://www.dr.dk/nyheder/indland/live-christianias-rydning-af-pusher-street-er-i-gang',
60 'info_dict': {
61 'id': 'urn:dr:mu:programcard:57c926176187a50a9c6e83c6',
62 'ext': 'mp4',
63 'title': 'christiania pusher street ryddes drdkrjpo',
64 'description': 'md5:2a71898b15057e9b97334f61d04e6eb5',
65 'timestamp': 1472800279,
66 'upload_date': '20160902',
67 'duration': 131.4,
68 },
69 'params': {
70 'skip_download': True,
71 },
72 'expected_warnings': ['Unable to download f4m manifest'],
73 }, {
74 # with SignLanguage formats
75 'url': 'https://www.dr.dk/tv/se/historien-om-danmark/-/historien-om-danmark-stenalder',
76 'info_dict': {
77 'id': 'historien-om-danmark-stenalder',
78 'ext': 'mp4',
79 'title': 'Historien om Danmark: Stenalder',
80 'description': 'md5:8c66dcbc1669bbc6f873879880f37f2a',
81 'timestamp': 1546628400,
82 'upload_date': '20190104',
83 'duration': 3502.56,
84 'formats': 'mincount:20',
85 },
86 'params': {
87 'skip_download': True,
88 },
89 }, {
90 'url': 'https://www.dr.dk/radio/p4kbh/regionale-nyheder-kh4/p4-nyheder-2019-06-26-17-30-9',
91 'only_matching': True,
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,
112 }, {
113 'url': 'https://www.dr.dk/drtv/program/jagten_220924',
114 'only_matching': True,
115 }]
116
117 def _real_extract(self, url):
118 video_id = self._match_id(url)
119
120 webpage = self._download_webpage(url, video_id)
121
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
126 video_id = self._search_regex(
127 (r'data-(?:material-identifier|episode-slug)="([^"]+)"',
128 r'data-resource="[^>"]+mu/programcard/expanded/([^"]+)"'),
129 webpage, 'video id', default=None)
130
131 if not video_id:
132 video_id = self._search_regex(
133 r'(urn(?:%3A|:)dr(?:%3A|:)mu(?:%3A|:)programcard(?:%3A|:)[\da-f]+)',
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
155
156 data = self._download_json(
157 programcard_url, video_id, 'Downloading video JSON', query=query)
158
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))
162 description = self._og_search_description(
163 webpage, default=None) or data.get('Description')
164
165 timestamp = unified_timestamp(
166 data.get('PrimaryBroadcastStartTime') or data.get('SortDateTime'))
167
168 thumbnail = None
169 duration = None
170
171 restricted_to_denmark = False
172
173 formats = []
174 subtitles = {}
175
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:]
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]
197
198 for asset in assets:
199 kind = asset.get('Kind')
200 if kind == 'Image':
201 thumbnail = url_or_none(asset.get('Uri'))
202 elif kind in ('VideoResource', 'AudioResource'):
203 duration = float_or_none(asset.get('DurationInMilliseconds'), 1000)
204 restricted_to_denmark = asset.get('RestrictedToDenmark')
205 asset_target = asset.get('Target')
206 for link in asset.get('Links', []):
207 uri = link.get('Uri')
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)
219 if not uri:
220 continue
221 target = link.get('Target')
222 format_id = target or ''
223 if asset_target in ('SpokenSubtitles', 'SignLanguage', 'VisuallyInterpreted'):
224 preference = -1
225 format_id += '-%s' % asset_target
226 elif asset_target == 'Default':
227 preference = 1
228 else:
229 preference = None
230 if target == 'HDS':
231 f4m_formats = self._extract_f4m_formats(
232 uri + '?hdcore=3.3.0&plugin=aasp-3.3.0.99.43',
233 video_id, preference, f4m_id=format_id, fatal=False)
234 if kind == 'AudioResource':
235 for f in f4m_formats:
236 f['vcodec'] = 'none'
237 formats.extend(f4m_formats)
238 elif target == 'HLS':
239 formats.extend(self._extract_m3u8_formats(
240 uri, video_id, 'mp4', entry_protocol='m3u8_native',
241 quality=preference, m3u8_id=format_id,
242 fatal=False))
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,
250 'tbr': int_or_none(bitrate),
251 'ext': link.get('FileFormat'),
252 'vcodec': 'none' if kind == 'AudioResource' else None,
253 'quality': preference,
254 })
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 })
271
272 if not formats and restricted_to_denmark:
273 self.raise_geo_restricted(
274 'Unfortunately, DR is not allowed to show this program outside Denmark.',
275 countries=self._GEO_COUNTRIES)
276
277 self._sort_formats(formats)
278
279 return {
280 'id': video_id,
281 'title': title,
282 'description': description,
283 'thumbnail': thumbnail,
284 'timestamp': timestamp,
285 'duration': duration,
286 'formats': formats,
287 'subtitles': subtitles,
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')),
295 }
296
297
298class DRTVLiveIE(InfoExtractor):
299 IE_NAME = 'drtv:live'
300 _VALID_URL = r'https?://(?:www\.)?dr\.dk/(?:tv|TV)/live/(?P<id>[\da-z-]+)'
301 _GEO_COUNTRIES = ['DK']
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)
320 title = channel_data['Title']
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 }