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