]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/zdf.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / zdf.py
CommitLineData
d5822b96
PH
1import re
2
3from .common import InfoExtractor
4from ..utils import (
db4678e4 5 NO_DEFAULT,
6 ExtractorError,
a5c1d955 7 determine_ext,
db4678e4 8 extract_attributes,
ec5e77c5 9 float_or_none,
fb47cb5b 10 int_or_none,
34921b43 11 join_nonempty,
ec5e77c5 12 merge_dicts,
fb47cb5b
S
13 parse_codecs,
14 qualities,
5f9aaac8 15 traverse_obj,
fb47cb5b
S
16 try_get,
17 unified_timestamp,
18 update_url_query,
3052a30d 19 url_or_none,
fb47cb5b 20 urljoin,
d5822b96 21)
0b7c2485 22
fb47cb5b
S
23
24class ZDFBaseIE(InfoExtractor):
9cf26b6e 25 _GEO_COUNTRIES = ['DE']
ee0ed033 26 _QUALITIES = ('auto', 'low', 'med', 'high', 'veryhigh', 'hd', 'fhd', 'uhd')
a2e6db36 27
ec5e77c5 28 def _call_api(self, url, video_id, item, api_token=None, referrer=None):
29 headers = {}
30 if api_token:
add96eb9 31 headers['Api-Auth'] = f'Bearer {api_token}'
ec5e77c5 32 if referrer:
33 headers['Referer'] = referrer
34 return self._download_json(
add96eb9 35 url, video_id, f'Downloading JSON {item}', headers=headers)
a5c1d955 36
fb47cb5b
S
37 @staticmethod
38 def _extract_subtitles(src):
39 subtitles = {}
40 for caption in try_get(src, lambda x: x['captions'], list) or []:
3052a30d
S
41 subtitle_url = url_or_none(caption.get('uri'))
42 if subtitle_url:
fb47cb5b
S
43 lang = caption.get('language', 'deu')
44 subtitles.setdefault(lang, []).append({
45 'url': subtitle_url,
46 })
47 return subtitles
48
49 def _extract_format(self, video_id, formats, format_urls, meta):
3052a30d 50 format_url = url_or_none(meta.get('url'))
600e9003 51 if not format_url or format_url in format_urls:
fb47cb5b
S
52 return
53 format_urls.add(format_url)
600e9003 54
55 mime_type, ext = meta.get('mimeType'), determine_ext(format_url)
fb47cb5b 56 if mime_type == 'application/x-mpegURL' or ext == 'm3u8':
600e9003 57 new_formats = self._extract_m3u8_formats(
fb47cb5b 58 format_url, video_id, 'mp4', m3u8_id='hls',
600e9003 59 entry_protocol='m3u8_native', fatal=False)
fb47cb5b 60 elif mime_type == 'application/f4m+xml' or ext == 'f4m':
600e9003 61 new_formats = self._extract_f4m_formats(
62 update_url_query(format_url, {'hdcore': '3.7.0'}), video_id, f4m_id='hds', fatal=False)
ee0ed033 63 elif ext == 'mpd':
64 new_formats = self._extract_mpd_formats(
65 format_url, video_id, mpd_id='dash', fatal=False)
fb47cb5b
S
66 else:
67 f = parse_codecs(meta.get('mimeCodec'))
600e9003 68 if not f and meta.get('type'):
69 data = meta['type'].split('_')
70 if try_get(data, lambda x: x[2]) == ext:
71 f = {'vcodec': data[0], 'acodec': data[1]}
fb47cb5b
S
72 f.update({
73 'url': format_url,
34921b43 74 'format_id': join_nonempty('http', meta.get('type'), meta.get('quality')),
add96eb9 75 'tbr': int_or_none(self._search_regex(r'_(\d+)k_', format_url, 'tbr', default=None)),
fb47cb5b 76 })
600e9003 77 new_formats = [f]
78 formats.extend(merge_dicts(f, {
34921b43 79 'format_note': join_nonempty('quality', 'class', from_dict=meta, delim=', '),
600e9003 80 'language': meta.get('language'),
81 'language_preference': 10 if meta.get('class') == 'main' else -10 if meta.get('class') == 'ad' else -1,
82 'quality': qualities(self._QUALITIES)(meta.get('quality')),
83 }) for f in new_formats)
fb47cb5b 84
ec5e77c5 85 def _extract_ptmd(self, ptmd_url, video_id, api_token, referrer):
50de3dba 86 ptmd = self._call_api(
ec5e77c5 87 ptmd_url, video_id, 'metadata', api_token, referrer)
88
89 content_id = ptmd.get('basename') or ptmd_url.split('/')[-1]
fb47cb5b 90
a5c1d955 91 formats = []
fb47cb5b
S
92 track_uris = set()
93 for p in ptmd['priorityList']:
94 formitaeten = p.get('formitaeten')
95 if not isinstance(formitaeten, list):
a5c1d955 96 continue
fb47cb5b
S
97 for f in formitaeten:
98 f_qualities = f.get('qualities')
99 if not isinstance(f_qualities, list):
100 continue
101 for quality in f_qualities:
102 tracks = try_get(quality, lambda x: x['audio']['tracks'], list)
103 if not tracks:
104 continue
105 for track in tracks:
106 self._extract_format(
ec5e77c5 107 content_id, formats, track_uris, {
fb47cb5b
S
108 'url': track.get('uri'),
109 'type': f.get('type'),
110 'mimeType': f.get('mimeType'),
111 'quality': quality.get('quality'),
600e9003 112 'class': track.get('class'),
fb47cb5b
S
113 'language': track.get('language'),
114 })
fb47cb5b 115
ec5e77c5 116 duration = float_or_none(try_get(
117 ptmd, lambda x: x['attributes']['duration']['value']), scale=1000)
118
119 return {
120 'extractor_key': ZDFIE.ie_key(),
121 'id': content_id,
122 'duration': duration,
123 'formats': formats,
124 'subtitles': self._extract_subtitles(ptmd),
9f14daf2 125 '_format_sort_fields': ('tbr', 'res', 'quality', 'language_preference'),
ec5e77c5 126 }
127
128 def _extract_player(self, webpage, video_id, fatal=True):
129 return self._parse_json(
130 self._search_regex(
131 r'(?s)data-zdfplayer-jsb=(["\'])(?P<json>{.+?})\1', webpage,
132 'player JSON', default='{}' if not fatal else NO_DEFAULT,
133 group='json'),
134 video_id)
135
136
137class ZDFIE(ZDFBaseIE):
138 _VALID_URL = r'https?://www\.zdf\.de/(?:[^/]+/)*(?P<id>[^/?#&]+)\.html'
139 _TESTS = [{
50e93e03 140 # Same as https://www.phoenix.de/sendungen/ereignisse/corona-nachgehakt/wohin-fuehrt-der-protest-in-der-pandemie-a-2050630.html
141 'url': 'https://www.zdf.de/politik/phoenix-sendungen/wohin-fuehrt-der-protest-in-der-pandemie-100.html',
142 'md5': '34ec321e7eb34231fd88616c65c92db0',
143 'info_dict': {
144 'id': '210222_phx_nachgehakt_corona_protest',
145 'ext': 'mp4',
146 'title': 'Wohin führt der Protest in der Pandemie?',
147 'description': 'md5:7d643fe7f565e53a24aac036b2122fbd',
148 'duration': 1691,
149 'timestamp': 1613948400,
150 'upload_date': '20210221',
151 },
152 'skip': 'No longer available: "Diese Seite wurde leider nicht gefunden"',
153 }, {
154 # Same as https://www.3sat.de/film/ab-18/10-wochen-sommer-108.html
155 'url': 'https://www.zdf.de/dokumentation/ab-18/10-wochen-sommer-102.html',
156 'md5': '0aff3e7bc72c8813f5e0fae333316a1d',
157 'info_dict': {
158 'id': '141007_ab18_10wochensommer_film',
159 'ext': 'mp4',
160 'title': 'Ab 18! - 10 Wochen Sommer',
161 'description': 'md5:8253f41dc99ce2c3ff892dac2d65fe26',
162 'duration': 2660,
163 'timestamp': 1608604200,
164 'upload_date': '20201222',
165 },
166 'skip': 'No longer available: "Diese Seite wurde leider nicht gefunden"',
167 }, {
f5c2c2c9 168 'url': 'https://www.zdf.de/nachrichten/heute-journal/heute-journal-vom-30-12-2021-100.html',
ec5e77c5 169 'info_dict': {
f5c2c2c9 170 'id': '211230_sendung_hjo',
ec5e77c5 171 'ext': 'mp4',
f5c2c2c9 172 'description': 'md5:47dff85977bde9fb8cba9e9c9b929839',
173 'duration': 1890.0,
174 'upload_date': '20211230',
175 'chapters': list,
176 'thumbnail': 'md5:e65f459f741be5455c952cd820eb188e',
177 'title': 'heute journal vom 30.12.2021',
178 'timestamp': 1640897100,
0fe87a87 179 },
180 'skip': 'No longer available: "Diese Seite wurde leider nicht gefunden"',
ec5e77c5 181 }, {
182 'url': 'https://www.zdf.de/dokumentation/terra-x/die-magie-der-farben-von-koenigspurpur-und-jeansblau-100.html',
183 'info_dict': {
184 'id': '151025_magie_farben2_tex',
185 'ext': 'mp4',
186 'title': 'Die Magie der Farben (2/2)',
187 'description': 'md5:a89da10c928c6235401066b60a6d5c1a',
188 'duration': 2615,
189 'timestamp': 1465021200,
190 'upload_date': '20160604',
f5c2c2c9 191 'thumbnail': 'https://www.zdf.de/assets/mauve-im-labor-100~768x432?cb=1464909117806',
ec5e77c5 192 },
5f9aaac8 193 }, {
194 'url': 'https://www.zdf.de/funk/druck-11790/funk-alles-ist-verzaubert-102.html',
0fe87a87 195 'md5': '57af4423db0455a3975d2dc4578536bc',
5f9aaac8 196 'info_dict': {
197 'ext': 'mp4',
198 'id': 'video_funk_1770473',
199 'duration': 1278,
200 'description': 'Die Neue an der Schule verdreht Ismail den Kopf.',
201 'title': 'Alles ist verzaubert',
202 'timestamp': 1635520560,
f5c2c2c9 203 'upload_date': '20211029',
0fe87a87 204 'thumbnail': 'https://www.zdf.de/assets/teaser-funk-alles-ist-verzaubert-102~1920x1080?cb=1663848412907',
5f9aaac8 205 },
ec5e77c5 206 }, {
207 # Same as https://www.phoenix.de/sendungen/dokumentationen/gesten-der-maechtigen-i-a-89468.html?ref=suche
208 'url': 'https://www.zdf.de/politik/phoenix-sendungen/die-gesten-der-maechtigen-100.html',
209 'only_matching': True,
210 }, {
211 # Same as https://www.3sat.de/film/spielfilm/der-hauptmann-100.html
212 'url': 'https://www.zdf.de/filme/filme-sonstige/der-hauptmann-112.html',
213 'only_matching': True,
214 }, {
215 # Same as https://www.3sat.de/wissen/nano/nano-21-mai-2019-102.html, equal media ids
216 'url': 'https://www.zdf.de/wissen/nano/nano-21-mai-2019-102.html',
217 'only_matching': True,
218 }, {
219 'url': 'https://www.zdf.de/service-und-hilfe/die-neue-zdf-mediathek/zdfmediathek-trailer-100.html',
220 'only_matching': True,
221 }, {
222 'url': 'https://www.zdf.de/filme/taunuskrimi/die-lebenden-und-die-toten-1---ein-taunuskrimi-100.html',
223 'only_matching': True,
224 }, {
225 'url': 'https://www.zdf.de/dokumentation/planet-e/planet-e-uebersichtsseite-weitere-dokumentationen-von-planet-e-100.html',
226 'only_matching': True,
5f9aaac8 227 }, {
50e93e03 228 'url': 'https://www.zdf.de/arte/todliche-flucht/page-video-artede-toedliche-flucht-16-100.html',
229 'info_dict': {
230 'id': 'video_artede_083871-001-A',
231 'ext': 'mp4',
232 'title': 'Tödliche Flucht (1/6)',
233 'description': 'md5:e34f96a9a5f8abd839ccfcebad3d5315',
234 'duration': 3193.0,
235 'timestamp': 1641355200,
236 'upload_date': '20220105',
237 },
add96eb9 238 'skip': 'No longer available "Diese Seite wurde leider nicht gefunden"',
62b2b736
E
239 }, {
240 'url': 'https://www.zdf.de/serien/soko-stuttgart/das-geld-anderer-leute-100.html',
241 'info_dict': {
242 'id': '191205_1800_sendung_sok8',
243 'ext': 'mp4',
244 'title': 'Das Geld anderer Leute',
245 'description': 'md5:cb6f660850dc5eb7d1ab776ea094959d',
246 'duration': 2581.0,
0fe87a87 247 'timestamp': 1675160100,
248 'upload_date': '20230131',
62b2b736
E
249 'thumbnail': 'https://epg-image.zdf.de/fotobase-webdelivery/images/e2d7e55a-09f0-424e-ac73-6cac4dd65f35?layout=2400x1350',
250 },
0fe87a87 251 }, {
252 'url': 'https://www.zdf.de/dokumentation/terra-x/unser-gruener-planet-wuesten-doku-100.html',
253 'info_dict': {
254 'id': '220605_dk_gruener_planet_wuesten_tex',
255 'ext': 'mp4',
256 'title': 'Unser grüner Planet - Wüsten',
257 'description': 'md5:4fc647b6f9c3796eea66f4a0baea2862',
258 'duration': 2613.0,
259 'timestamp': 1654450200,
260 'upload_date': '20220605',
261 'format_note': 'uhd, main',
262 'thumbnail': 'https://www.zdf.de/assets/saguaro-kakteen-102~3840x2160?cb=1655910690796',
263 },
ec5e77c5 264 }]
265
266 def _extract_entry(self, url, player, content, video_id):
267 title = content.get('title') or content['teaserHeadline']
268
269 t = content['mainVideoContent']['http://zdf.de/rels/target']
db4678e4 270 ptmd_path = traverse_obj(t, (
271 (('streams', 'default'), None),
add96eb9 272 ('http://zdf.de/rels/streams/ptmd', 'http://zdf.de/rels/streams/ptmd-template'),
db4678e4 273 ), get_all=False)
ec5e77c5 274 if not ptmd_path:
db4678e4 275 raise ExtractorError('Could not extract ptmd_path')
ec5e77c5 276
277 info = self._extract_ptmd(
0fe87a87 278 urljoin(url, ptmd_path.replace('{playerId}', 'android_native_5')), video_id, player['apiToken'], url)
ec5e77c5 279
fb47cb5b
S
280 thumbnails = []
281 layouts = try_get(
282 content, lambda x: x['teaserImageRef']['layouts'], dict)
283 if layouts:
284 for layout_key, layout_url in layouts.items():
3052a30d
S
285 layout_url = url_or_none(layout_url)
286 if not layout_url:
fb47cb5b
S
287 continue
288 thumbnail = {
289 'url': layout_url,
290 'format_id': layout_key,
291 }
292 mobj = re.search(r'(?P<width>\d+)x(?P<height>\d+)', layout_key)
293 if mobj:
294 thumbnail.update({
295 'width': int(mobj.group('width')),
296 'height': int(mobj.group('height')),
297 })
298 thumbnails.append(thumbnail)
a5c1d955 299
f5c2c2c9 300 chapter_marks = t.get('streamAnchorTag') or []
301 chapter_marks.append({'anchorOffset': int_or_none(t.get('duration'))})
302 chapters = [{
303 'start_time': chap.get('anchorOffset'),
304 'end_time': next_chap.get('anchorOffset'),
add96eb9 305 'title': chap.get('anchorLabel'),
f5c2c2c9 306 } for chap, next_chap in zip(chapter_marks, chapter_marks[1:])]
307
ec5e77c5 308 return merge_dicts(info, {
fb47cb5b
S
309 'title': title,
310 'description': content.get('leadParagraph') or content.get('teasertext'),
311 'duration': int_or_none(t.get('duration')),
312 'timestamp': unified_timestamp(content.get('editorialDate')),
313 'thumbnails': thumbnails,
add96eb9 314 'chapters': chapters or None,
ec5e77c5 315 })
d5822b96 316
fb47cb5b 317 def _extract_regular(self, url, player, video_id):
50de3dba 318 content = self._call_api(
ec5e77c5 319 player['content'], video_id, 'content', player['apiToken'], url)
50de3dba 320 return self._extract_entry(player['content'], player, content, video_id)
b6de53ea 321
fb47cb5b 322 def _extract_mobile(self, video_id):
ec5e77c5 323 video = self._download_json(
add96eb9 324 f'https://zdf-cdn.live.cellular.de/mediathekV2/document/{video_id}',
ec5e77c5 325 video_id)
326
fb47cb5b 327 formats = []
db4678e4 328 formitaeten = try_get(video, lambda x: x['document']['formitaeten'], list)
329 document = formitaeten and video['document']
330 if formitaeten:
331 title = document['titel']
332 content_id = document['basename']
333
334 format_urls = set()
335 for f in formitaeten or []:
336 self._extract_format(content_id, formats, format_urls, f)
fb47cb5b
S
337
338 thumbnails = []
339 teaser_bild = document.get('teaserBild')
340 if isinstance(teaser_bild, dict):
341 for thumbnail_key, thumbnail in teaser_bild.items():
342 thumbnail_url = try_get(
add96eb9 343 thumbnail, lambda x: x['url'], str)
fb47cb5b
S
344 if thumbnail_url:
345 thumbnails.append({
346 'url': thumbnail_url,
347 'id': thumbnail_key,
348 'width': int_or_none(thumbnail.get('width')),
349 'height': int_or_none(thumbnail.get('height')),
350 })
b6de53ea 351
fb47cb5b 352 return {
ec5e77c5 353 'id': content_id,
fb47cb5b
S
354 'title': title,
355 'description': document.get('beschreibung'),
356 'duration': int_or_none(document.get('length')),
ec5e77c5 357 'timestamp': unified_timestamp(document.get('date')) or unified_timestamp(
add96eb9 358 try_get(video, lambda x: x['meta']['editorialDate'], str)),
fb47cb5b
S
359 'thumbnails': thumbnails,
360 'subtitles': self._extract_subtitles(document),
361 'formats': formats,
362 }
b6de53ea 363
fb47cb5b
S
364 def _real_extract(self, url):
365 video_id = self._match_id(url)
b6de53ea 366
fb47cb5b
S
367 webpage = self._download_webpage(url, video_id, fatal=False)
368 if webpage:
369 player = self._extract_player(webpage, url, fatal=False)
370 if player:
371 return self._extract_regular(url, player, video_id)
b6de53ea 372
fb47cb5b 373 return self._extract_mobile(video_id)
b6de53ea 374
b6de53ea 375
fb47cb5b
S
376class ZDFChannelIE(ZDFBaseIE):
377 _VALID_URL = r'https?://www\.zdf\.de/(?:[^/]+/)*(?P<id>[^/?#&]+)'
c2404463 378 _TESTS = [{
fb47cb5b 379 'url': 'https://www.zdf.de/sport/das-aktuelle-sportstudio',
8560c618 380 'info_dict': {
fb47cb5b 381 'id': 'das-aktuelle-sportstudio',
db4678e4 382 'title': 'das aktuelle sportstudio',
8560c618 383 },
db4678e4 384 'playlist_mincount': 18,
c2404463 385 }, {
fb47cb5b
S
386 'url': 'https://www.zdf.de/dokumentation/planet-e',
387 'info_dict': {
388 'id': 'planet-e',
389 'title': 'planet e.',
390 },
b4cbdbd4 391 'playlist_mincount': 50,
db4678e4 392 }, {
393 'url': 'https://www.zdf.de/gesellschaft/aktenzeichen-xy-ungeloest',
394 'info_dict': {
395 'id': 'aktenzeichen-xy-ungeloest',
396 'title': 'Aktenzeichen XY... ungelöst',
397 'entries': "lambda x: not any('xy580-fall1-kindermoerder-gesucht-100' in e['url'] for e in x)",
398 },
399 'playlist_mincount': 2,
c2404463 400 }, {
fb47cb5b 401 'url': 'https://www.zdf.de/filme/taunuskrimi/',
c2404463
S
402 'only_matching': True,
403 }]
fb47cb5b
S
404
405 @classmethod
406 def suitable(cls, url):
add96eb9 407 return False if ZDFIE.suitable(url) else super().suitable(url)
9abd500a 408
db4678e4 409 def _og_search_title(self, webpage, fatal=False):
add96eb9 410 title = super()._og_search_title(webpage, fatal=fatal)
db4678e4 411 return re.split(r'\s+[-|]\s+ZDF(?:mediathek)?$', title or '')[0] or None
412
9abd500a
PH
413 def _real_extract(self, url):
414 channel_id = self._match_id(url)
8560c618 415
fb47cb5b
S
416 webpage = self._download_webpage(url, channel_id)
417
db4678e4 418 matches = re.finditer(
add96eb9 419 rf'''<div\b[^>]*?\sdata-plusbar-id\s*=\s*(["'])(?P<p_id>[\w-]+)\1[^>]*?\sdata-plusbar-url=\1(?P<url>{ZDFIE._VALID_URL})\1''',
db4678e4 420 webpage)
421
422 if self._downloader.params.get('noplaylist', False):
423 entry = next(
424 (self.url_result(m.group('url'), ie=ZDFIE.ie_key()) for m in matches),
425 None)
426 self.to_screen('Downloading just the main video because of --no-playlist')
427 if entry:
428 return entry
429 else:
add96eb9 430 self.to_screen(f'Downloading playlist {channel_id} - add --no-playlist to download just the main video')
db4678e4 431
432 def check_video(m):
433 v_ref = self._search_regex(
add96eb9 434 r'''(<a\b[^>]*?\shref\s*=[^>]+?\sdata-target-id\s*=\s*(["']){}\2[^>]*>)'''.format(m.group('p_id')),
db4678e4 435 webpage, 'check id', default='')
436 v_ref = extract_attributes(v_ref)
437 return v_ref.get('data-target-video-type') != 'novideo'
438
439 return self.playlist_from_matches(
440 (m.group('url') for m in matches if check_video(m)),
441 channel_id, self._og_search_title(webpage, fatal=False))