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