[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / ndr.py
blob5181c7f20cd71e578ae6492b66ab20bbcf82e741
1 import re
2 import urllib.parse
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 determine_ext,
8 int_or_none,
9 merge_dicts,
10 parse_iso8601,
11 qualities,
12 try_get,
13 urljoin,
17 class NDRBaseIE(InfoExtractor):
18 def _real_extract(self, url):
19 mobj = self._match_valid_url(url)
20 display_id = next(group for group in mobj.groups() if group)
21 webpage = self._download_webpage(url, display_id)
22 return self._extract_embed(webpage, display_id, url)
25 class NDRIE(NDRBaseIE):
26 IE_NAME = 'ndr'
27 IE_DESC = 'NDR.de - Norddeutscher Rundfunk'
28 _VALID_URL = r'https?://(?:\w+\.)*ndr\.de/(?:[^/]+/)*(?P<id>[^/?#]+),[\da-z]+\.html'
29 _TESTS = [{
30 # httpVideo, same content id
31 'url': 'http://www.ndr.de/fernsehen/Party-Poette-und-Parade,hafengeburtstag988.html',
32 'md5': '6515bc255dc5c5f8c85bbc38e035a659',
33 'info_dict': {
34 'id': 'hafengeburtstag988',
35 'display_id': 'Party-Poette-und-Parade',
36 'ext': 'mp4',
37 'title': 'Party, Pötte und Parade',
38 'description': 'md5:ad14f9d2f91d3040b6930c697e5f6b4c',
39 'uploader': 'ndrtv',
40 'timestamp': 1431255671,
41 'upload_date': '20150510',
42 'duration': 3498,
44 'params': {
45 'skip_download': True,
47 'expected_warnings': ['Unable to download f4m manifest'],
48 }, {
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',
52 'info_dict': {
53 'id': 'osna272',
54 'display_id': '40-Osnabrueck-spielt-sich-in-einen-Rausch',
55 'ext': 'mp4',
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,
63 'params': {
64 'skip_download': True,
66 'skip': 'No longer available',
67 }, {
68 # httpAudio, same content id
69 'url': 'http://www.ndr.de/info/La-Valette-entgeht-der-Hinrichtung,audio51535.html',
70 'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
71 'info_dict': {
72 'id': 'audio51535',
73 'display_id': 'La-Valette-entgeht-der-Hinrichtung',
74 'ext': 'mp3',
75 'title': 'La Valette entgeht der Hinrichtung',
76 'description': 'md5:22f9541913a40fe50091d5cdd7c9f536',
77 'uploader': 'ndrinfo',
78 'timestamp': 1631711863,
79 'upload_date': '20210915',
80 'duration': 884,
82 'params': {
83 'skip_download': True,
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.+',
105 'params': {
106 'skip_download': True,
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,
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 = urllib.parse.urlparse(url)
129 path = self._search_regex(rf'(.+/){display_id}', parsed_url.path or '', 'embed URL', default='')
130 # find tell-tale image with the actual ID
131 ndr_id = self._search_regex(rf'{path}([a-z]+\d+)(?!\.)\b', 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 = f'ndr:{ndr_id}' if ndr_id else NDR_INFO_URL_TPL % (embed_url, )
135 if not embed_url:
136 raise ExtractorError('Unable to extract embedUrl')
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)
156 class NJoyIE(NDRBaseIE):
157 IE_NAME = 'njoy'
158 IE_DESC = 'N-JOY'
159 _VALID_URL = r'https?://(?:www\.)?n-joy\.de/(?:[^/]+/)*(?:(?P<display_id>[^/?#]+),)?(?P<id>[\da-z]+)\.html'
160 _TESTS = [{
161 # httpVideo, same content id
162 'url': 'http://www.n-joy.de/entertainment/comedy/comedy_contest/Benaissa-beim-NDR-Comedy-Contest,comedycontest2480.html',
163 'md5': 'cb63be60cd6f9dd75218803146d8dc67',
164 'info_dict': {
165 'id': 'comedycontest2480',
166 'display_id': 'Benaissa-beim-NDR-Comedy-Contest',
167 'ext': 'mp4',
168 'title': 'Benaissa beim NDR Comedy Contest',
169 'description': 'md5:f057a6c4e1c728b10d33b5ffd36ddc39',
170 'uploader': 'ndrtv',
171 'upload_date': '20141129',
172 'duration': 654,
174 'params': {
175 'skip_download': True,
177 'skip': 'No longer available',
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': {
183 'id': 'livestream283',
184 'display_id': 'Das-frueheste-DJ-Set-des-Nordens-live-mit-Felix-Jaehn-',
185 'ext': 'mp3',
186 'title': 'Das frueheste DJ Set des Nordens live mit Felix Jaehn',
187 'description': 'md5:681698f527b8601e511e7b79edde7d2c',
188 'uploader': 'njoy',
189 'upload_date': '20210830',
191 'params': {
192 'skip_download': True,
194 }, {
195 'url': 'http://www.n-joy.de/radio/webradio/morningshow209.html',
196 'only_matching': True,
199 def _extract_embed(self, webpage, display_id, url=None):
200 # find tell-tale URL with the actual ID, or ...
201 video_id = self._search_regex(
202 (r'''\bsrc\s*=\s*["']?(?:/\w+)+/([a-z]+\d+)(?!\.)\b''',
203 r'<iframe[^>]+id="pp_([\da-z]+)"'),
204 webpage, 'NDR id', default=None)
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))
211 return {
212 '_type': 'url_transparent',
213 'ie_key': 'NDREmbedBase',
214 'url': f'ndr:{video_id}',
215 'display_id': display_id,
216 'description': description,
217 'title': display_id.replace('-', ' ').strip(),
221 class NDREmbedBaseIE(InfoExtractor): # XXX: Conventionally, Concrete class names do not end in BaseIE
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,
232 def _real_extract(self, url):
233 mobj = self._match_valid_url(url)
234 video_id = mobj.group('id') or mobj.group('id_s')
236 ppjson = self._download_json(
237 f'http://www.ndr.de/{video_id}-ppjson.json', video_id)
239 playlist = ppjson['playlist']
241 formats = []
242 quality_key = qualities(('xs', 's', 'm', 'l', 'xl'))
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(
251 src + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id,
252 f4m_id='hds', fatal=False))
253 elif ext == 'm3u8':
254 formats.extend(self._extract_m3u8_formats(
255 src, video_id, 'mp4', m3u8_id='hls',
256 entry_protocol='m3u8_native', fatal=False))
257 else:
258 quality = f.get('quality')
259 ff = {
260 'url': src,
261 'format_id': quality or format_id,
262 'quality': quality_key(quality),
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)
270 config = playlist['config']
272 live = playlist.get('config', {}).get('streamType') in ['httpVideoLive', 'httpAudioLive']
273 title = config['title']
274 uploader = ppjson.get('config', {}).get('branding')
275 upload_date = ppjson.get('config', {}).get('publicationDate')
276 duration = int_or_none(config.get('duration'))
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')),
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',
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,
313 'subtitles': subtitles,
317 class NDREmbedIE(NDREmbedBaseIE): # XXX: Do not subclass from concrete IE
318 IE_NAME = 'ndr:embed'
319 _VALID_URL = r'https?://(?:\w+\.)*ndr\.de/(?:[^/]+/)*(?P<id>[\da-z]+)-(?:(?:ard)?player|externalPlayer)\.html'
320 _TESTS = [{
321 'url': 'http://www.ndr.de/fernsehen/sendungen/ndr_aktuell/ndraktuell28488-player.html',
322 'md5': '8b9306142fe65bbdefb5ce24edb6b0a9',
323 'info_dict': {
324 'id': 'ndraktuell28488',
325 'ext': 'mp4',
326 'title': 'Norddeutschland begrüßt Flüchtlinge',
327 'is_live': False,
328 'uploader': 'ndrtv',
329 'upload_date': '20150907',
330 'duration': 132,
332 'skip': 'No longer available',
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,
345 'params': {
346 'skip_download': True,
348 'skip': 'No longer available',
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',
358 'upload_date': '20210915',
359 'duration': 884,
361 'params': {
362 'skip_download': True,
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,
376 'params': {
377 'skip_download': True,
379 'skip': 'No longer available',
380 }, {
381 # httpVideoLive
382 'url': 'http://www.ndr.de/fernsehen/livestream/livestream217-externalPlayer.html',
383 'info_dict': {
384 'id': 'livestream217',
385 'ext': 'mp4',
386 'title': r're:^NDR Fernsehen Niedersachsen \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
387 'is_live': True,
388 'upload_date': '20210409',
389 'uploader': 'ndrtv',
391 'params': {
392 'skip_download': True,
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,
415 class NJoyEmbedIE(NDREmbedBaseIE): # XXX: Do not subclass from concrete IE
416 IE_NAME = 'njoy:embed'
417 _VALID_URL = r'https?://(?:www\.)?n-joy\.de/(?:[^/]+/)*(?P<id>[\da-z]+)-(?:player|externalPlayer)_[^/]+\.html'
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',
422 'info_dict': {
423 'id': 'doku948',
424 'ext': 'mp4',
425 'title': 'Zehn Jahre Reeperbahn Festival - die Doku',
426 'is_live': False,
427 'upload_date': '20200826',
428 'duration': 1011,
430 'expected_warnings': ['Unable to download f4m manifest'],
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,
444 'params': {
445 'skip_download': True,
447 'skip': 'No longer available',
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',
454 'title': r're:^N-JOY Weltweit \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
455 'is_live': True,
456 'uploader': 'njoy',
457 'upload_date': '20210830',
459 'params': {
460 'skip_download': True,
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,