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