]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/br.py
309452d23ec7a752d912ce1478b519b8c9c33720
[yt-dlp.git] / yt_dlp / extractor / br.py
1 import json
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 ExtractorError,
7 int_or_none,
8 parse_duration,
9 parse_iso8601,
10 xpath_element,
11 xpath_text,
12 )
13
14
15 class BRIE(InfoExtractor):
16 IE_DESC = 'Bayerischer Rundfunk'
17 _VALID_URL = r'(?P<base_url>https?://(?:www\.)?br(?:-klassik)?\.de)/(?:[a-z0-9\-_]+/)+(?P<id>[a-z0-9\-_]+)\.html'
18
19 _TESTS = [
20 {
21 'url': 'http://www.br.de/mediathek/video/sendungen/abendschau/betriebliche-altersvorsorge-104.html',
22 'md5': '83a0477cf0b8451027eb566d88b51106',
23 'info_dict': {
24 'id': '48f656ef-287e-486f-be86-459122db22cc',
25 'ext': 'mp4',
26 'title': 'Die böse Überraschung',
27 'description': 'md5:ce9ac81b466ce775b8018f6801b48ac9',
28 'duration': 180,
29 'uploader': 'Reinhard Weber',
30 'upload_date': '20150422',
31 },
32 'skip': '404 not found',
33 },
34 {
35 'url': 'http://www.br.de/nachrichten/oberbayern/inhalt/muenchner-polizeipraesident-schreiber-gestorben-100.html',
36 'md5': 'af3a3a4aa43ff0ce6a89504c67f427ef',
37 'info_dict': {
38 'id': 'a4b83e34-123d-4b81-9f4e-c0d3121a4e05',
39 'ext': 'flv',
40 'title': 'Manfred Schreiber ist tot',
41 'description': 'md5:b454d867f2a9fc524ebe88c3f5092d97',
42 'duration': 26,
43 },
44 'skip': '404 not found',
45 },
46 {
47 'url': 'https://www.br-klassik.de/audio/peeping-tom-premierenkritik-dance-festival-muenchen-100.html',
48 'md5': '8b5b27c0b090f3b35eac4ab3f7a73d3d',
49 'info_dict': {
50 'id': '74c603c9-26d3-48bb-b85b-079aeed66e0b',
51 'ext': 'aac',
52 'title': 'Kurzweilig und sehr bewegend',
53 'description': 'md5:0351996e3283d64adeb38ede91fac54e',
54 'duration': 296,
55 },
56 'skip': '404 not found',
57 },
58 {
59 'url': 'http://www.br.de/radio/bayern1/service/team/videos/team-video-erdelt100.html',
60 'md5': 'dbab0aef2e047060ea7a21fc1ce1078a',
61 'info_dict': {
62 'id': '6ba73750-d405-45d3-861d-1ce8c524e059',
63 'ext': 'mp4',
64 'title': 'Umweltbewusster Häuslebauer',
65 'description': 'md5:d52dae9792d00226348c1dbb13c9bae2',
66 'duration': 116,
67 }
68 },
69 {
70 'url': 'http://www.br.de/fernsehen/br-alpha/sendungen/kant-fuer-anfaenger/kritik-der-reinen-vernunft/kant-kritik-01-metaphysik100.html',
71 'md5': '23bca295f1650d698f94fc570977dae3',
72 'info_dict': {
73 'id': 'd982c9ce-8648-4753-b358-98abb8aec43d',
74 'ext': 'mp4',
75 'title': 'Folge 1 - Metaphysik',
76 'description': 'md5:bb659990e9e59905c3d41e369db1fbe3',
77 'duration': 893,
78 'uploader': 'Eva Maria Steimle',
79 'upload_date': '20170208',
80 }
81 },
82 ]
83
84 def _real_extract(self, url):
85 base_url, display_id = self._match_valid_url(url).groups()
86 page = self._download_webpage(url, display_id)
87 xml_url = self._search_regex(
88 r"return BRavFramework\.register\(BRavFramework\('avPlayer_(?:[a-f0-9-]{36})'\)\.setup\({dataURL:'(/(?:[a-z0-9\-]+/)+[a-z0-9/~_.-]+)'}\)\);", page, 'XMLURL')
89 xml = self._download_xml(base_url + xml_url, display_id)
90
91 medias = []
92
93 for xml_media in xml.findall('video') + xml.findall('audio'):
94 media_id = xml_media.get('externalId')
95 media = {
96 'id': media_id,
97 'title': xpath_text(xml_media, 'title', 'title', True),
98 'duration': parse_duration(xpath_text(xml_media, 'duration')),
99 'formats': self._extract_formats(xpath_element(
100 xml_media, 'assets'), media_id),
101 'thumbnails': self._extract_thumbnails(xpath_element(
102 xml_media, 'teaserImage/variants'), base_url),
103 'description': xpath_text(xml_media, 'desc'),
104 'webpage_url': xpath_text(xml_media, 'permalink'),
105 'uploader': xpath_text(xml_media, 'author'),
106 }
107 broadcast_date = xpath_text(xml_media, 'broadcastDate')
108 if broadcast_date:
109 media['upload_date'] = ''.join(reversed(broadcast_date.split('.')))
110 medias.append(media)
111
112 if len(medias) > 1:
113 self.report_warning(
114 'found multiple medias; please '
115 'report this with the video URL to http://yt-dl.org/bug')
116 if not medias:
117 raise ExtractorError('No media entries found')
118 return medias[0]
119
120 def _extract_formats(self, assets, media_id):
121 formats = []
122 for asset in assets.findall('asset'):
123 format_url = xpath_text(asset, ['downloadUrl', 'url'])
124 asset_type = asset.get('type')
125 if asset_type.startswith('HDS'):
126 formats.extend(self._extract_f4m_formats(
127 format_url + '?hdcore=3.2.0', media_id, f4m_id='hds', fatal=False))
128 elif asset_type.startswith('HLS'):
129 formats.extend(self._extract_m3u8_formats(
130 format_url, media_id, 'mp4', 'm3u8_native', m3u8_id='hds', fatal=False))
131 else:
132 format_info = {
133 'ext': xpath_text(asset, 'mediaType'),
134 'width': int_or_none(xpath_text(asset, 'frameWidth')),
135 'height': int_or_none(xpath_text(asset, 'frameHeight')),
136 'tbr': int_or_none(xpath_text(asset, 'bitrateVideo')),
137 'abr': int_or_none(xpath_text(asset, 'bitrateAudio')),
138 'vcodec': xpath_text(asset, 'codecVideo'),
139 'acodec': xpath_text(asset, 'codecAudio'),
140 'container': xpath_text(asset, 'mediaType'),
141 'filesize': int_or_none(xpath_text(asset, 'size')),
142 }
143 format_url = self._proto_relative_url(format_url)
144 if format_url:
145 http_format_info = format_info.copy()
146 http_format_info.update({
147 'url': format_url,
148 'format_id': 'http-%s' % asset_type,
149 })
150 formats.append(http_format_info)
151 server_prefix = xpath_text(asset, 'serverPrefix')
152 if server_prefix:
153 rtmp_format_info = format_info.copy()
154 rtmp_format_info.update({
155 'url': server_prefix,
156 'play_path': xpath_text(asset, 'fileName'),
157 'format_id': 'rtmp-%s' % asset_type,
158 })
159 formats.append(rtmp_format_info)
160 return formats
161
162 def _extract_thumbnails(self, variants, base_url):
163 thumbnails = [{
164 'url': base_url + xpath_text(variant, 'url'),
165 'width': int_or_none(xpath_text(variant, 'width')),
166 'height': int_or_none(xpath_text(variant, 'height')),
167 } for variant in variants.findall('variant') if xpath_text(variant, 'url')]
168 thumbnails.sort(key=lambda x: x['width'] * x['height'], reverse=True)
169 return thumbnails
170
171
172 class BRMediathekIE(InfoExtractor):
173 IE_DESC = 'Bayerischer Rundfunk Mediathek'
174 _VALID_URL = r'https?://(?:www\.)?br\.de/mediathek//?video/(?:[^/?&#]+?-)?(?P<id>av:[0-9a-f]{24})'
175
176 _TESTS = [{
177 'url': 'https://www.br.de/mediathek/video/gesundheit-die-sendung-vom-28112017-av:5a1e6a6e8fce6d001871cc8e',
178 'md5': 'fdc3d485835966d1622587d08ba632ec',
179 'info_dict': {
180 'id': 'av:5a1e6a6e8fce6d001871cc8e',
181 'ext': 'mp4',
182 'title': 'Die Sendung vom 28.11.2017',
183 'description': 'md5:6000cdca5912ab2277e5b7339f201ccc',
184 'timestamp': 1511942766,
185 'upload_date': '20171129',
186 }
187 }, {
188 'url': 'https://www.br.de/mediathek//video/av:61b0db581aed360007558c12',
189 'only_matching': True,
190 }]
191
192 def _real_extract(self, url):
193 clip_id = self._match_id(url)
194
195 clip = self._download_json(
196 'https://proxy-base.master.mango.express/graphql',
197 clip_id, data=json.dumps({
198 "query": """{
199 viewer {
200 clip(id: "%s") {
201 title
202 description
203 duration
204 createdAt
205 ageRestriction
206 videoFiles {
207 edges {
208 node {
209 publicLocation
210 fileSize
211 videoProfile {
212 width
213 height
214 bitrate
215 encoding
216 }
217 }
218 }
219 }
220 captionFiles {
221 edges {
222 node {
223 publicLocation
224 }
225 }
226 }
227 teaserImages {
228 edges {
229 node {
230 imageFiles {
231 edges {
232 node {
233 publicLocation
234 width
235 height
236 }
237 }
238 }
239 }
240 }
241 }
242 }
243 }
244 }""" % clip_id}).encode(), headers={
245 'Content-Type': 'application/json',
246 })['data']['viewer']['clip']
247 title = clip['title']
248
249 formats = []
250 for edge in clip.get('videoFiles', {}).get('edges', []):
251 node = edge.get('node', {})
252 n_url = node.get('publicLocation')
253 if not n_url:
254 continue
255 ext = determine_ext(n_url)
256 if ext == 'm3u8':
257 formats.extend(self._extract_m3u8_formats(
258 n_url, clip_id, 'mp4', 'm3u8_native',
259 m3u8_id='hls', fatal=False))
260 else:
261 video_profile = node.get('videoProfile', {})
262 tbr = int_or_none(video_profile.get('bitrate'))
263 format_id = 'http'
264 if tbr:
265 format_id += '-%d' % tbr
266 formats.append({
267 'format_id': format_id,
268 'url': n_url,
269 'width': int_or_none(video_profile.get('width')),
270 'height': int_or_none(video_profile.get('height')),
271 'tbr': tbr,
272 'filesize': int_or_none(node.get('fileSize')),
273 })
274
275 subtitles = {}
276 for edge in clip.get('captionFiles', {}).get('edges', []):
277 node = edge.get('node', {})
278 n_url = node.get('publicLocation')
279 if not n_url:
280 continue
281 subtitles.setdefault('de', []).append({
282 'url': n_url,
283 })
284
285 thumbnails = []
286 for edge in clip.get('teaserImages', {}).get('edges', []):
287 for image_edge in edge.get('node', {}).get('imageFiles', {}).get('edges', []):
288 node = image_edge.get('node', {})
289 n_url = node.get('publicLocation')
290 if not n_url:
291 continue
292 thumbnails.append({
293 'url': n_url,
294 'width': int_or_none(node.get('width')),
295 'height': int_or_none(node.get('height')),
296 })
297
298 return {
299 'id': clip_id,
300 'title': title,
301 'description': clip.get('description'),
302 'duration': int_or_none(clip.get('duration')),
303 'timestamp': parse_iso8601(clip.get('createdAt')),
304 'age_limit': int_or_none(clip.get('ageRestriction')),
305 'formats': formats,
306 'subtitles': subtitles,
307 'thumbnails': thumbnails,
308 }