[ie/youtube] Fix DASH formats incorrectly skipped in some situations (#11910)
[yt-dlp.git] / yt_dlp / extractor / bpb.py
blobd7bf58b366237193eaf7fb3f55d9d1dc82702835
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 clean_html,
6 extract_attributes,
7 join_nonempty,
8 js_to_json,
9 mimetype2ext,
10 unified_strdate,
11 url_or_none,
12 urljoin,
14 from ..utils.traversal import (
15 find_element,
16 traverse_obj,
20 class BpbIE(InfoExtractor):
21 IE_DESC = 'Bundeszentrale für politische Bildung'
22 _VALID_URL = r'https?://(?:www\.|m\.)?bpb\.de/(?:[^/?#]+/)*(?P<id>\d+)(?:[/?#]|$)'
24 _TESTS = [{
25 'url': 'http://www.bpb.de/mediathek/297/joachim-gauck-zu-1989-und-die-erinnerung-an-die-ddr',
26 'info_dict': {
27 'id': '297',
28 'ext': 'mp4',
29 'creators': ['Kooperative Berlin'],
30 'description': r're:Joachim Gauck, .*\n\nKamera: .*',
31 'release_date': '20150716',
32 'series': 'Interview auf dem Geschichtsforum 1989 | 2009',
33 'tags': [],
34 'thumbnail': r're:https?://www\.bpb\.de/cache/images/7/297_teaser_16x9_1240\.jpg.*',
35 'title': 'Joachim Gauck zu 1989 und die Erinnerung an die DDR',
36 'uploader': 'Bundeszentrale für politische Bildung',
38 }, {
39 'url': 'https://www.bpb.de/mediathek/video/522184/krieg-flucht-und-falschmeldungen-wirstattdesinformation-2/',
40 'info_dict': {
41 'id': '522184',
42 'ext': 'mp4',
43 'creators': ['Institute for Strategic Dialogue Germany gGmbH (ISD)'],
44 'description': 'md5:f83c795ff8f825a69456a9e51fc15903',
45 'release_date': '20230621',
46 'series': 'Narrative über den Krieg Russlands gegen die Ukraine (NUK)',
47 'tags': [],
48 'thumbnail': r're:https://www\.bpb\.de/cache/images/4/522184_teaser_16x9_1240\.png.*',
49 'title': 'md5:9b01ccdbf58dbf9e5c9f6e771a803b1c',
50 'uploader': 'Bundeszentrale für politische Bildung',
52 }, {
53 'url': 'https://www.bpb.de/lernen/bewegtbild-und-politische-bildung/webvideo/518789/krieg-flucht-und-falschmeldungen-wirstattdesinformation-1/',
54 'info_dict': {
55 'id': '518789',
56 'ext': 'mp4',
57 'creators': ['Institute for Strategic Dialogue Germany gGmbH (ISD)'],
58 'description': 'md5:85228aed433e84ff0ff9bc582abd4ea8',
59 'release_date': '20230302',
60 'series': 'Narrative über den Krieg Russlands gegen die Ukraine (NUK)',
61 'tags': [],
62 'thumbnail': r're:https://www\.bpb\.de/cache/images/9/518789_teaser_16x9_1240\.jpeg.*',
63 'title': 'md5:3e956f264bb501f6383f10495a401da4',
64 'uploader': 'Bundeszentrale für politische Bildung',
66 }, {
67 'url': 'https://www.bpb.de/mediathek/podcasts/apuz-podcast/539727/apuz-20-china/',
68 'only_matching': True,
69 }, {
70 'url': 'https://www.bpb.de/mediathek/audio/315813/folge-1-eine-einfuehrung/',
71 'info_dict': {
72 'id': '315813',
73 'ext': 'mp3',
74 'creators': ['Axel Schröder'],
75 'description': 'md5:eda9d1af34e5912efef5baf54fba4427',
76 'release_date': '20200921',
77 'series': 'Auf Endlagersuche. Der deutsche Weg zu einem sicheren Atommülllager',
78 'tags': ['Atomenergie', 'Endlager', 'hoch-radioaktiver Abfall', 'Endlagersuche', 'Atommüll', 'Atomendlager', 'Gorleben', 'Deutschland'],
79 'thumbnail': r're:https://www\.bpb\.de/cache/images/3/315813_teaser_16x9_1240\.png.*',
80 'title': 'Folge 1: Eine Einführung',
81 'uploader': 'Bundeszentrale für politische Bildung',
83 }, {
84 'url': 'https://www.bpb.de/517806/die-weltanschauung-der-neuen-rechten/',
85 'info_dict': {
86 'id': '517806',
87 'ext': 'mp3',
88 'creators': ['Bundeszentrale für politische Bildung'],
89 'description': 'md5:594689600e919912aade0b2871cc3fed',
90 'release_date': '20230127',
91 'series': 'Vorträge des Fachtags "Modernisierer. Grenzgänger. Anstifter. Sechs Jahrzehnte \'Neue Rechte\'"',
92 'tags': ['Rechtsextremismus', 'Konservatismus', 'Konservativismus', 'neue Rechte', 'Rechtspopulismus', 'Schnellroda', 'Deutschland'],
93 'thumbnail': r're:https://www\.bpb\.de/cache/images/6/517806_teaser_16x9_1240\.png.*',
94 'title': 'Die Weltanschauung der "Neuen Rechten"',
95 'uploader': 'Bundeszentrale für politische Bildung',
97 }, {
98 'url': 'https://www.bpb.de/mediathek/reihen/zahlen-und-fakten-soziale-situation-filme/520153/zahlen-und-fakten-die-soziale-situation-in-deutschland-migration/',
99 'only_matching': True,
102 _TITLE_RE = re.compile('(?P<title>[^<]*)<[^>]+>(?P<series>[^<]*)')
104 def _parse_vue_attributes(self, name, string, video_id):
105 attributes = extract_attributes(self._search_regex(rf'(<{name}(?:"[^"]*?"|[^>])*>)', string, name))
107 for key, value in attributes.items():
108 if key.startswith(':'):
109 attributes[key] = self._parse_json(value, video_id, transform_source=js_to_json, fatal=False)
111 return attributes
113 @staticmethod
114 def _process_source(source):
115 url = url_or_none(source['src'])
116 if not url:
117 return None
119 source_type = source.get('type', '')
120 extension = mimetype2ext(source_type)
121 is_video = source_type.startswith('video')
122 note = url.rpartition('.')[0].rpartition('_')[2] if is_video else None
124 return {
125 'url': url,
126 'ext': extension,
127 'vcodec': None if is_video else 'none',
128 'quality': 10 if note == 'high' else 0,
129 'format_note': note,
130 'format_id': join_nonempty(extension, note),
133 def _real_extract(self, url):
134 video_id = self._match_id(url)
135 webpage = self._download_webpage(url, video_id)
137 title_result = traverse_obj(webpage, ({find_element(cls='opening-header__title')}, {self._TITLE_RE.match}))
138 json_lds = list(self._yield_json_ld(webpage, video_id, fatal=False))
140 return {
141 'id': video_id,
142 'title': traverse_obj(title_result, ('title', {str.strip})) or None,
143 # This metadata could be interpreted otherwise, but it fits "series" the most
144 'series': traverse_obj(title_result, ('series', {str.strip})) or None,
145 'description': join_nonempty(*traverse_obj(webpage, [(
146 {find_element(cls='opening-intro')},
147 [{find_element(tag='bpb-accordion-item')}, {find_element(cls='text-content')}],
148 ), {clean_html}]), delim='\n\n') or None,
149 'creators': traverse_obj(self._html_search_meta('author', webpage), all),
150 'uploader': self._html_search_meta('publisher', webpage),
151 'release_date': unified_strdate(self._html_search_meta('date', webpage)),
152 'tags': traverse_obj(json_lds, (..., 'keywords', {lambda x: x.split(',')}, ...)),
153 **traverse_obj(self._parse_vue_attributes('bpb-player', webpage, video_id), {
154 'formats': (':sources', ..., {self._process_source}),
155 'thumbnail': ('poster', {urljoin(url)}),