]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/ndr.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / ndr.py
CommitLineData
6d1b3489 1import re
2
e9ea0bf1 3from .common import InfoExtractor
6d1b3489 4from ..compat import compat_urllib_parse_urlparse
c1ed1f70 5from ..utils import (
6d1b3489 6 ExtractorError,
e897bd82 7 determine_ext,
c1ed1f70 8 int_or_none,
6d1b3489 9 merge_dicts,
10 parse_iso8601,
376e1ad0 11 qualities,
3fc56635
S
12 try_get,
13 urljoin,
c1ed1f70 14)
e9ea0bf1
S
15
16
1934f3a0 17class NDRBaseIE(InfoExtractor):
64997815 18 def _real_extract(self, url):
5ad28e7f 19 mobj = self._match_valid_url(url)
7e0dc613 20 display_id = next(group for group in mobj.groups() if group)
2ffe3bc1 21 webpage = self._download_webpage(url, display_id)
6d1b3489 22 return self._extract_embed(webpage, display_id, url)
64997815 23
1934f3a0
YCH
24
25class NDRIE(NDRBaseIE):
26 IE_NAME = 'ndr'
2ffe3bc1 27 IE_DESC = 'NDR.de - Norddeutscher Rundfunk'
6d1b3489 28 _VALID_URL = r'https?://(?:\w+\.)*ndr\.de/(?:[^/]+/)*(?P<id>[^/?#]+),[\da-z]+\.html'
2ffe3bc1 29 _TESTS = [{
6d1b3489 30 # httpVideo, same content id
2ffe3bc1 31 'url': 'http://www.ndr.de/fernsehen/Party-Poette-und-Parade,hafengeburtstag988.html',
6d1b3489 32 'md5': '6515bc255dc5c5f8c85bbc38e035a659',
2ffe3bc1
S
33 'info_dict': {
34 'id': 'hafengeburtstag988',
6d1b3489 35 'display_id': 'Party-Poette-und-Parade',
2ffe3bc1
S
36 'ext': 'mp4',
37 'title': 'Party, Pötte und Parade',
38 'description': 'md5:ad14f9d2f91d3040b6930c697e5f6b4c',
6d1b3489 39 'uploader': 'ndrtv',
40 'timestamp': 1431255671,
41 'upload_date': '20150510',
2ffe3bc1
S
42 'duration': 3498,
43 },
6d1b3489 44 'params': {
45 'skip_download': True,
2ffe3bc1 46 },
6d1b3489 47 'expected_warnings': ['Unable to download f4m manifest'],
2ffe3bc1 48 }, {
6d1b3489 49 # httpVideo, different content id
50 'url': 'http://www.ndr.de/sport/fussball/40-Osnabrueck-spielt-sich-in-einen-Rausch,osna270.html',
51 'md5': '1043ff203eab307f0c51702ec49e9a71',
23dd2d9a 52 'info_dict': {
6d1b3489 53 'id': 'osna272',
54 'display_id': '40-Osnabrueck-spielt-sich-in-einen-Rausch',
23dd2d9a 55 'ext': 'mp4',
6d1b3489 56 'title': 'Osnabrück - Wehen Wiesbaden: Die Highlights',
57 'description': 'md5:32e9b800b3d2d4008103752682d5dc01',
58 'uploader': 'ndrtv',
59 'timestamp': 1442059200,
60 'upload_date': '20150912',
61 'duration': 510,
62 },
63 'params': {
64 'skip_download': True,
65 },
66 'skip': 'No longer available',
23dd2d9a 67 }, {
6d1b3489 68 # httpAudio, same content id
2ffe3bc1 69 'url': 'http://www.ndr.de/info/La-Valette-entgeht-der-Hinrichtung,audio51535.html',
6d1b3489 70 'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
2ffe3bc1
S
71 'info_dict': {
72 'id': 'audio51535',
6d1b3489 73 'display_id': 'La-Valette-entgeht-der-Hinrichtung',
2ffe3bc1
S
74 'ext': 'mp3',
75 'title': 'La Valette entgeht der Hinrichtung',
76 'description': 'md5:22f9541913a40fe50091d5cdd7c9f536',
6d1b3489 77 'uploader': 'ndrinfo',
78 'timestamp': 1631711863,
79 'upload_date': '20210915',
80 'duration': 884,
2ffe3bc1 81 },
6d1b3489 82 'params': {
83 'skip_download': True,
84 },
85 }, {
86 # with subtitles
87 'url': 'https://www.ndr.de/fernsehen/sendungen/extra_3/extra-3-Satiremagazin-mit-Christian-Ehring,sendung1091858.html',
88 'info_dict': {
89 'id': 'extra18674',
90 'display_id': 'extra-3-Satiremagazin-mit-Christian-Ehring',
91 'ext': 'mp4',
92 'title': 'Extra 3 vom 11.11.2020 mit Christian Ehring',
93 'description': 'md5:700f6de264010585012a72f97b0ac0c9',
94 'uploader': 'ndrtv',
95 'upload_date': '20201207',
96 'timestamp': 1614349457,
97 'duration': 1749,
98 'subtitles': {
99 'de': [{
100 'ext': 'ttml',
101 'url': r're:^https://www\.ndr\.de.+',
102 }],
103 },
104 },
105 'params': {
106 'skip_download': True,
107 },
108 'expected_warnings': ['Unable to download f4m manifest'],
109 }, {
110 'url': 'https://www.ndr.de/Fettes-Brot-Ferris-MC-und-Thees-Uhlmann-live-on-stage,festivalsommer116.html',
111 'only_matching': True,
2ffe3bc1
S
112 }]
113
6d1b3489 114 def _extract_embed(self, webpage, display_id, url):
115 embed_url = (
116 self._html_search_meta(
117 'embedURL', webpage, 'embed URL',
118 default=None)
119 or self._search_regex(
120 r'\bembedUrl["\']\s*:\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
121 'embed URL', group='url', default=None)
122 or self._search_regex(
123 r'\bvar\s*sophoraID\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
124 'embed URL', group='url', default=''))
125 # some more work needed if we only found sophoraID
126 if re.match(r'^[a-z]+\d+$', embed_url):
127 # get the initial part of the url path,. eg /panorama/archiv/2022/
128 parsed_url = compat_urllib_parse_urlparse(url)
129 path = self._search_regex(r'(.+/)%s' % display_id, parsed_url.path or '', 'embed URL', default='')
130 # find tell-tale image with the actual ID
131 ndr_id = self._search_regex(r'%s([a-z]+\d+)(?!\.)\b' % (path, ), webpage, 'embed URL', default=None)
132 # or try to use special knowledge!
133 NDR_INFO_URL_TPL = 'https://www.ndr.de/info/%s-player.html'
134 embed_url = 'ndr:%s' % (ndr_id, ) if ndr_id else NDR_INFO_URL_TPL % (embed_url, )
135 if not embed_url:
136 raise ExtractorError('Unable to extract embedUrl')
137
138 description = self._search_regex(
139 r'<p[^>]+itemprop="description">([^<]+)</p>',
140 webpage, 'description', default=None) or self._og_search_description(webpage)
141 timestamp = parse_iso8601(
142 self._search_regex(
143 (r'<span[^>]+itemprop="(?:datePublished|uploadDate)"[^>]+content="(?P<cont>[^"]+)"',
144 r'\bvar\s*pdt\s*=\s*(?P<q>["\'])(?P<cont>(?:(?!(?P=q)).)+)(?P=q)', ),
145 webpage, 'upload date', group='cont', default=None))
146 info = self._search_json_ld(webpage, display_id, default={})
147 return merge_dicts({
148 '_type': 'url_transparent',
149 'url': embed_url,
150 'display_id': display_id,
151 'description': description,
152 'timestamp': timestamp,
153 }, info)
1934f3a0
YCH
154
155
156class NJoyIE(NDRBaseIE):
2ffe3bc1
S
157 IE_NAME = 'njoy'
158 IE_DESC = 'N-JOY'
92519402 159 _VALID_URL = r'https?://(?:www\.)?n-joy\.de/(?:[^/]+/)*(?:(?P<display_id>[^/?#]+),)?(?P<id>[\da-z]+)\.html'
2ffe3bc1
S
160 _TESTS = [{
161 # httpVideo, same content id
1934f3a0
YCH
162 'url': 'http://www.n-joy.de/entertainment/comedy/comedy_contest/Benaissa-beim-NDR-Comedy-Contest,comedycontest2480.html',
163 'md5': 'cb63be60cd6f9dd75218803146d8dc67',
164 'info_dict': {
64997815 165 'id': 'comedycontest2480',
2ffe3bc1 166 'display_id': 'Benaissa-beim-NDR-Comedy-Contest',
1934f3a0
YCH
167 'ext': 'mp4',
168 'title': 'Benaissa beim NDR Comedy Contest',
2ffe3bc1
S
169 'description': 'md5:f057a6c4e1c728b10d33b5ffd36ddc39',
170 'uploader': 'ndrtv',
171 'upload_date': '20141129',
1934f3a0 172 'duration': 654,
2ffe3bc1
S
173 },
174 'params': {
175 'skip_download': True,
176 },
6d1b3489 177 'skip': 'No longer available',
2ffe3bc1
S
178 }, {
179 # httpVideo, different content id
180 'url': 'http://www.n-joy.de/musik/Das-frueheste-DJ-Set-des-Nordens-live-mit-Felix-Jaehn-,felixjaehn168.html',
181 'md5': '417660fffa90e6df2fda19f1b40a64d8',
182 'info_dict': {
6d1b3489 183 'id': 'livestream283',
2ffe3bc1 184 'display_id': 'Das-frueheste-DJ-Set-des-Nordens-live-mit-Felix-Jaehn-',
6d1b3489 185 'ext': 'mp3',
186 'title': 'Das frueheste DJ Set des Nordens live mit Felix Jaehn',
187 'description': 'md5:681698f527b8601e511e7b79edde7d2c',
2ffe3bc1 188 'uploader': 'njoy',
6d1b3489 189 'upload_date': '20210830',
2ffe3bc1
S
190 },
191 'params': {
192 'skip_download': True,
193 },
7e0dc613
S
194 }, {
195 'url': 'http://www.n-joy.de/radio/webradio/morningshow209.html',
196 'only_matching': True,
2ffe3bc1
S
197 }]
198
6d1b3489 199 def _extract_embed(self, webpage, display_id, url=None):
200 # find tell-tale URL with the actual ID, or ...
2ffe3bc1 201 video_id = self._search_regex(
6d1b3489 202 (r'''\bsrc\s*=\s*["']?(?:/\w+)+/([a-z]+\d+)(?!\.)\b''',
203 r'<iframe[^>]+id="pp_([\da-z]+)"', ),
204 webpage, 'NDR id', default=None)
205
206 description = (
207 self._html_search_meta('description', webpage)
208 or self._search_regex(
209 r'<div[^>]+class="subline"[^>]*>[^<]+</div>\s*<p>([^<]+)</p>',
210 webpage, 'description', fatal=False))
2ffe3bc1
S
211 return {
212 '_type': 'url_transparent',
213 'ie_key': 'NDREmbedBase',
214 'url': 'ndr:%s' % video_id,
215 'display_id': display_id,
216 'description': description,
6d1b3489 217 'title': display_id.replace('-', ' ').strip(),
1934f3a0 218 }
64997815 219
220
6368e2e6 221class NDREmbedBaseIE(InfoExtractor): # XXX: Conventionally, Concrete class names do not end in BaseIE
2ffe3bc1
S
222 IE_NAME = 'ndr:embed:base'
223 _VALID_URL = r'(?:ndr:(?P<id_s>[\da-z]+)|https?://www\.ndr\.de/(?P<id>[\da-z]+)-ppjson\.json)'
224 _TESTS = [{
225 'url': 'ndr:soundcheck3366',
226 'only_matching': True,
227 }, {
228 'url': 'http://www.ndr.de/soundcheck3366-ppjson.json',
229 'only_matching': True,
230 }]
64997815 231
232 def _real_extract(self, url):
5ad28e7f 233 mobj = self._match_valid_url(url)
2ffe3bc1
S
234 video_id = mobj.group('id') or mobj.group('id_s')
235
236 ppjson = self._download_json(
237 'http://www.ndr.de/%s-ppjson.json' % video_id, video_id)
238
239 playlist = ppjson['playlist']
240
241 formats = []
242 quality_key = qualities(('xs', 's', 'm', 'l', 'xl'))
243
244 for format_id, f in playlist.items():
245 src = f.get('src')
246 if not src:
247 continue
248 ext = determine_ext(src, None)
249 if ext == 'f4m':
250 formats.extend(self._extract_f4m_formats(
310ea466
S
251 src + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id,
252 f4m_id='hds', fatal=False))
2ffe3bc1
S
253 elif ext == 'm3u8':
254 formats.extend(self._extract_m3u8_formats(
310ea466
S
255 src, video_id, 'mp4', m3u8_id='hls',
256 entry_protocol='m3u8_native', fatal=False))
2ffe3bc1
S
257 else:
258 quality = f.get('quality')
259 ff = {
260 'url': src,
261 'format_id': quality or format_id,
262 'quality': quality_key(quality),
263 }
264 type_ = f.get('type')
265 if type_ and type_.split('/')[0] == 'audio':
266 ff['vcodec'] = 'none'
267 ff['ext'] = ext or 'mp3'
268 formats.append(ff)
2ffe3bc1
S
269
270 config = playlist['config']
271
272 live = playlist.get('config', {}).get('streamType') in ['httpVideoLive', 'httpAudioLive']
273 title = config['title']
2ffe3bc1
S
274 uploader = ppjson.get('config', {}).get('branding')
275 upload_date = ppjson.get('config', {}).get('publicationDate')
276 duration = int_or_none(config.get('duration'))
277
3fc56635
S
278 thumbnails = []
279 poster = try_get(config, lambda x: x['poster'], dict) or {}
280 for thumbnail_id, thumbnail in poster.items():
281 thumbnail_url = urljoin(url, thumbnail.get('src'))
282 if not thumbnail_url:
283 continue
284 thumbnails.append({
285 'id': thumbnail.get('quality') or thumbnail_id,
286 'url': thumbnail_url,
287 'preference': quality_key(thumbnail.get('quality')),
288 })
2ffe3bc1 289
8bdd16b4 290 subtitles = {}
291 tracks = config.get('tracks')
292 if tracks and isinstance(tracks, list):
293 for track in tracks:
294 if not isinstance(track, dict):
295 continue
296 track_url = urljoin(url, track.get('src'))
297 if not track_url:
298 continue
299 subtitles.setdefault(track.get('srclang') or 'de', []).append({
300 'url': track_url,
301 'ext': 'ttml',
302 })
303
2ffe3bc1
S
304 return {
305 'id': video_id,
306 'title': title,
307 'is_live': live,
308 'uploader': uploader if uploader != '-' else None,
309 'upload_date': upload_date[0:8] if upload_date else None,
310 'duration': duration,
311 'thumbnails': thumbnails,
312 'formats': formats,
8bdd16b4 313 'subtitles': subtitles,
2ffe3bc1 314 }
64997815 315
316
6368e2e6 317class NDREmbedIE(NDREmbedBaseIE): # XXX: Do not subclass from concrete IE
64997815 318 IE_NAME = 'ndr:embed'
6d1b3489 319 _VALID_URL = r'https?://(?:\w+\.)*ndr\.de/(?:[^/]+/)*(?P<id>[\da-z]+)-(?:(?:ard)?player|externalPlayer)\.html'
2ffe3bc1 320 _TESTS = [{
64997815 321 'url': 'http://www.ndr.de/fernsehen/sendungen/ndr_aktuell/ndraktuell28488-player.html',
2ffe3bc1 322 'md5': '8b9306142fe65bbdefb5ce24edb6b0a9',
64997815 323 'info_dict': {
324 'id': 'ndraktuell28488',
325 'ext': 'mp4',
326 'title': 'Norddeutschland begrüßt Flüchtlinge',
2ffe3bc1
S
327 'is_live': False,
328 'uploader': 'ndrtv',
329 'upload_date': '20150907',
64997815 330 'duration': 132,
2ffe3bc1 331 },
6d1b3489 332 'skip': 'No longer available',
2ffe3bc1
S
333 }, {
334 'url': 'http://www.ndr.de/ndr2/events/soundcheck/soundcheck3366-player.html',
335 'md5': '002085c44bae38802d94ae5802a36e78',
336 'info_dict': {
337 'id': 'soundcheck3366',
338 'ext': 'mp4',
339 'title': 'Ella Henderson braucht Vergleiche nicht zu scheuen',
340 'is_live': False,
341 'uploader': 'ndr2',
342 'upload_date': '20150912',
343 'duration': 3554,
344 },
345 'params': {
346 'skip_download': True,
347 },
6d1b3489 348 'skip': 'No longer available',
2ffe3bc1
S
349 }, {
350 'url': 'http://www.ndr.de/info/audio51535-player.html',
351 'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
352 'info_dict': {
353 'id': 'audio51535',
354 'ext': 'mp3',
355 'title': 'La Valette entgeht der Hinrichtung',
356 'is_live': False,
357 'uploader': 'ndrinfo',
6d1b3489 358 'upload_date': '20210915',
2ffe3bc1
S
359 'duration': 884,
360 },
361 'params': {
362 'skip_download': True,
363 },
364 }, {
365 'url': 'http://www.ndr.de/fernsehen/sendungen/visite/visite11010-externalPlayer.html',
366 'md5': 'ae57f80511c1e1f2fd0d0d3d31aeae7c',
367 'info_dict': {
368 'id': 'visite11010',
369 'ext': 'mp4',
370 'title': 'Visite - die ganze Sendung',
371 'is_live': False,
372 'uploader': 'ndrtv',
373 'upload_date': '20150902',
374 'duration': 3525,
375 },
376 'params': {
377 'skip_download': True,
378 },
6d1b3489 379 'skip': 'No longer available',
2ffe3bc1
S
380 }, {
381 # httpVideoLive
382 'url': 'http://www.ndr.de/fernsehen/livestream/livestream217-externalPlayer.html',
383 'info_dict': {
384 'id': 'livestream217',
6d1b3489 385 'ext': 'mp4',
ec85ded8 386 'title': r're:^NDR Fernsehen Niedersachsen \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
2ffe3bc1 387 'is_live': True,
6d1b3489 388 'upload_date': '20210409',
389 'uploader': 'ndrtv',
2ffe3bc1
S
390 },
391 'params': {
392 'skip_download': True,
393 },
394 }, {
395 'url': 'http://www.ndr.de/ndrkultur/audio255020-player.html',
396 'only_matching': True,
397 }, {
398 'url': 'http://www.ndr.de/fernsehen/sendungen/nordtour/nordtour7124-player.html',
399 'only_matching': True,
400 }, {
401 'url': 'http://www.ndr.de/kultur/film/videos/videoimport10424-player.html',
402 'only_matching': True,
403 }, {
404 'url': 'http://www.ndr.de/fernsehen/sendungen/hamburg_journal/hamj43006-player.html',
405 'only_matching': True,
406 }, {
407 'url': 'http://www.ndr.de/fernsehen/sendungen/weltbilder/weltbilder4518-player.html',
408 'only_matching': True,
409 }, {
410 'url': 'http://www.ndr.de/fernsehen/doku952-player.html',
411 'only_matching': True,
412 }]
64997815 413
414
6368e2e6 415class NJoyEmbedIE(NDREmbedBaseIE): # XXX: Do not subclass from concrete IE
2ffe3bc1 416 IE_NAME = 'njoy:embed'
92519402 417 _VALID_URL = r'https?://(?:www\.)?n-joy\.de/(?:[^/]+/)*(?P<id>[\da-z]+)-(?:player|externalPlayer)_[^/]+\.html'
2ffe3bc1
S
418 _TESTS = [{
419 # httpVideo
420 'url': 'http://www.n-joy.de/events/reeperbahnfestival/doku948-player_image-bc168e87-5263-4d6d-bd27-bb643005a6de_theme-n-joy.html',
421 'md5': '8483cbfe2320bd4d28a349d62d88bd74',
64997815 422 'info_dict': {
2ffe3bc1 423 'id': 'doku948',
64997815 424 'ext': 'mp4',
2ffe3bc1
S
425 'title': 'Zehn Jahre Reeperbahn Festival - die Doku',
426 'is_live': False,
6d1b3489 427 'upload_date': '20200826',
2ffe3bc1
S
428 'duration': 1011,
429 },
6d1b3489 430 'expected_warnings': ['Unable to download f4m manifest'],
2ffe3bc1
S
431 }, {
432 # httpAudio
433 'url': 'http://www.n-joy.de/news_wissen/stefanrichter100-player_image-d5e938b1-f21a-4b9a-86b8-aaba8bca3a13_theme-n-joy.html',
434 'md5': 'd989f80f28ac954430f7b8a48197188a',
435 'info_dict': {
436 'id': 'stefanrichter100',
437 'ext': 'mp3',
438 'title': 'Interview mit einem Augenzeugen',
439 'is_live': False,
440 'uploader': 'njoy',
441 'upload_date': '20150909',
442 'duration': 140,
443 },
444 'params': {
445 'skip_download': True,
446 },
6d1b3489 447 'skip': 'No longer available',
2ffe3bc1
S
448 }, {
449 # httpAudioLive, no explicit ext
450 'url': 'http://www.n-joy.de/news_wissen/webradioweltweit100-player_image-3fec0484-2244-4565-8fb8-ed25fd28b173_theme-n-joy.html',
451 'info_dict': {
452 'id': 'webradioweltweit100',
453 'ext': 'mp3',
ec85ded8 454 'title': r're:^N-JOY Weltweit \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
2ffe3bc1
S
455 'is_live': True,
456 'uploader': 'njoy',
6d1b3489 457 'upload_date': '20210830',
2ffe3bc1
S
458 },
459 'params': {
460 'skip_download': True,
461 },
462 }, {
463 'url': 'http://www.n-joy.de/musik/dockville882-player_image-3905259e-0803-4764-ac72-8b7de077d80a_theme-n-joy.html',
464 'only_matching': True,
465 }, {
466 'url': 'http://www.n-joy.de/radio/sendungen/morningshow/urlaubsfotos190-player_image-066a5df1-5c95-49ec-a323-941d848718db_theme-n-joy.html',
467 'only_matching': True,
468 }, {
469 'url': 'http://www.n-joy.de/entertainment/comedy/krudetv290-player_image-ab261bfe-51bf-4bf3-87ba-c5122ee35b3d_theme-n-joy.html',
470 'only_matching': True,
471 }]