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