[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / wdr.py
blob1c1f0ed0526f210ca9eda2ac989293394ef799cb
1 import re
2 import urllib.parse
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 determine_ext,
8 dict_get,
9 js_to_json,
10 strip_jsonp,
11 try_get,
12 unified_strdate,
13 update_url_query,
14 url_or_none,
15 urlhandle_detect_ext,
19 class WDRIE(InfoExtractor):
20 __API_URL_TPL = '//deviceids-medp.wdr.de/ondemand/%s/%s'
21 _VALID_URL = r'''(?x)https?://
22 (?:deviceids-medp\.wdr\.de/ondemand/\d+/|
23 kinder\.wdr\.de/(?!mediathek/)[^#?]+-)
24 (?P<id>\d+)\.(?:js|assetjsonp)
25 '''
26 _GEO_COUNTRIES = ['DE']
27 _TESTS = [{
28 'url': 'http://deviceids-medp.wdr.de/ondemand/155/1557833.js',
29 'info_dict': {
30 'id': 'mdb-1557833',
31 'ext': 'mp4',
32 'title': 'Biathlon-Staffel verpasst Podest bei Olympia-Generalprobe',
33 'upload_date': '20180112',
37 def _asset_url(self, wdr_id):
38 id_len = max(len(wdr_id), 5)
39 return ''.join(('https:', self.__API_URL_TPL % (wdr_id[:id_len - 4], wdr_id), '.js'))
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
44 if url.startswith('wdr:'):
45 video_id = url[4:]
46 url = self._asset_url(video_id)
48 metadata = self._download_json(
49 url, video_id, transform_source=strip_jsonp)
51 is_live = metadata.get('mediaType') == 'live'
53 tracker_data = metadata['trackerData']
54 title = tracker_data['trackerClipTitle']
55 media_resource = metadata['mediaResource']
57 formats = []
58 subtitles = {}
60 # check if the metadata contains a direct URL to a file
61 for kind, media in media_resource.items():
62 if kind == 'captionsHash':
63 for ext, url in media.items():
64 subtitles.setdefault('de', []).append({
65 'url': url,
66 'ext': ext,
68 continue
70 if kind not in ('dflt', 'alt'):
71 continue
72 if not isinstance(media, dict):
73 continue
75 for tag_name, medium_url in media.items():
76 if tag_name not in ('videoURL', 'audioURL'):
77 continue
79 ext = determine_ext(medium_url)
80 if ext == 'm3u8':
81 formats.extend(self._extract_m3u8_formats(
82 medium_url, video_id, 'mp4', 'm3u8_native',
83 m3u8_id='hls'))
84 elif ext == 'f4m':
85 manifest_url = update_url_query(
86 medium_url, {'hdcore': '3.2.0', 'plugin': 'aasp-3.2.0.77.18'})
87 formats.extend(self._extract_f4m_formats(
88 manifest_url, video_id, f4m_id='hds', fatal=False))
89 elif ext == 'smil':
90 formats.extend(self._extract_smil_formats(
91 medium_url, 'stream', fatal=False))
92 else:
93 a_format = {
94 'url': medium_url,
96 if ext == 'unknown_video':
97 urlh = self._request_webpage(
98 medium_url, video_id, note='Determining extension')
99 ext = urlhandle_detect_ext(urlh)
100 a_format['ext'] = ext
101 formats.append(a_format)
103 caption_url = media_resource.get('captionURL')
104 if caption_url:
105 subtitles['de'] = [{
106 'url': caption_url,
107 'ext': 'ttml',
109 captions_hash = media_resource.get('captionsHash')
110 if isinstance(captions_hash, dict):
111 for ext, format_url in captions_hash.items():
112 format_url = url_or_none(format_url)
113 if not format_url:
114 continue
115 subtitles.setdefault('de', []).append({
116 'url': format_url,
117 'ext': determine_ext(format_url, None) or ext,
120 return {
121 'id': tracker_data.get('trackerClipId', video_id),
122 'title': title,
123 'alt_title': tracker_data.get('trackerClipSubcategory'),
124 'formats': formats,
125 'subtitles': subtitles,
126 'upload_date': unified_strdate(tracker_data.get('trackerClipAirTime')),
127 'is_live': is_live,
131 class WDRPageIE(WDRIE): # XXX: Do not subclass from concrete IE
132 _MAUS_REGEX = r'https?://(?:www\.)wdrmaus.de/(?:[^/]+/)*?(?P<maus_id>[^/?#.]+)(?:/?|/index\.php5|\.php5)$'
133 _PAGE_REGEX = r'/(?:mediathek/)?(?:[^/]+/)*(?P<display_id>[^/]+)\.html'
134 _VALID_URL = r'https?://(?:www\d?\.)?(?:(?:kinder\.)?wdr\d?|sportschau)\.de' + _PAGE_REGEX + '|' + _MAUS_REGEX
136 _TESTS = [
138 'url': 'http://www1.wdr.de/mediathek/video/sendungen/doku-am-freitag/video-geheimnis-aachener-dom-100.html',
139 # HDS download, MD5 is unstable
140 'info_dict': {
141 'id': 'mdb-1058683',
142 'ext': 'flv',
143 'display_id': 'doku-am-freitag/video-geheimnis-aachener-dom-100',
144 'title': 'Geheimnis Aachener Dom',
145 'alt_title': 'Doku am Freitag',
146 'upload_date': '20160304',
147 'description': 'md5:87be8ff14d8dfd7a7ee46f0299b52318',
148 'is_live': False,
149 'subtitles': {'de': [{
150 'url': 'http://ondemand-ww.wdr.de/medp/fsk0/105/1058683/1058683_12220974.xml',
151 'ext': 'ttml',
152 }]},
154 'skip': 'HTTP Error 404: Not Found',
157 'url': 'http://www1.wdr.de/mediathek/audio/wdr3/wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100.html',
158 'md5': 'f4c1f96d01cf285240f53ea4309663d8',
159 'info_dict': {
160 'id': 'mdb-1072000',
161 'ext': 'mp3',
162 'display_id': 'wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100',
163 'title': 'Schriftstellerin Juli Zeh',
164 'alt_title': 'WDR 3 Gespräch am Samstag',
165 'upload_date': '20160312',
166 'description': 'md5:e127d320bc2b1f149be697ce044a3dd7',
167 'is_live': False,
168 'subtitles': {},
170 'skip': 'HTTP Error 404: Not Found',
173 # FIXME: Asset JSON is directly embedded in webpage
174 'url': 'http://www1.wdr.de/mediathek/video/live/index.html',
175 'info_dict': {
176 'id': 'mdb-2296252',
177 'ext': 'mp4',
178 'title': r're:^WDR Fernsehen im Livestream (?:\(nur in Deutschland erreichbar\) )?[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
179 'alt_title': 'WDR Fernsehen Live',
180 'upload_date': '20201112',
181 'is_live': True,
183 'params': {
184 'skip_download': True, # m3u8 download
188 'url': 'http://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html',
189 'playlist_mincount': 6,
190 'info_dict': {
191 'id': 'aktuelle-stunde-120',
195 'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
196 'info_dict': {
197 'id': 'mdb-2627637',
198 'ext': 'mp4',
199 'upload_date': 're:^[0-9]{8}$',
200 'title': 're:^Die Sendung (?:mit der Maus )?vom [0-9.]{10}$',
202 'skip': 'The id changes from week to week because of the new episode',
205 'url': 'http://www.wdrmaus.de/filme/sachgeschichten/achterbahn.php5',
206 'md5': '803138901f6368ee497b4d195bb164f2',
207 'info_dict': {
208 'id': 'mdb-186083',
209 'ext': 'mp4',
210 'upload_date': '20130919',
211 'title': 'Sachgeschichte - Achterbahn ',
213 'skip': 'HTTP Error 404: Not Found',
216 'url': 'http://www1.wdr.de/radio/player/radioplayer116~_layout-popupVersion.html',
217 # Live stream, MD5 unstable
218 'info_dict': {
219 'id': 'mdb-869971',
220 'ext': 'mp4',
221 'title': r're:^COSMO Livestream [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
222 'alt_title': 'COSMO Livestream',
223 'live_status': 'is_live',
224 'upload_date': '20160101',
226 'params': {
227 'skip_download': True, # m3u8 download
231 'url': 'http://www.sportschau.de/handballem2018/handball-nationalmannschaft-em-stolperstein-vorrunde-100.html',
232 'info_dict': {
233 'id': 'mdb-1556012',
234 'ext': 'mp4',
235 'title': 'DHB-Vizepräsident Bob Hanning - "Die Weltspitze ist extrem breit"',
236 'upload_date': '20180111',
238 'params': {
239 'skip_download': True,
241 'skip': 'HTTP Error 404: Not Found',
244 'url': 'http://www.sportschau.de/handballem2018/audio-vorschau---die-handball-em-startet-mit-grossem-favoritenfeld-100.html',
245 'only_matching': True,
248 'url': 'https://kinder.wdr.de/tv/die-sendung-mit-dem-elefanten/av/video-folge---astronaut-100.html',
249 'only_matching': True,
252 'url': 'https://www1.wdr.de/mediathek/video/sendungen/rockpalast/video-baroness---freak-valley-festival--100.html',
253 'info_dict': {
254 'id': 'mdb-2741028',
255 'ext': 'mp4',
256 'title': 'Baroness - Freak Valley Festival 2022',
257 'alt_title': 'Rockpalast',
258 'upload_date': '20220725',
263 def _real_extract(self, url):
264 mobj = self._match_valid_url(url)
265 display_id = dict_get(mobj.groupdict(), ('display_id', 'maus_id'), 'wdrmaus')
266 webpage = self._download_webpage(url, display_id)
268 entries = []
270 # Article with several videos
272 # for wdr.de the data-extension-ard is in a tag with the class "mediaLink"
273 # for wdr.de radio players, in a tag with the class "wdrrPlayerPlayBtn"
274 # for wdrmaus, in a tag with the class "videoButton" (previously a link
275 # to the page in a multiline "videoLink"-tag)
276 for mobj in re.finditer(
277 r'''(?sx)class=
279 (["\'])(?:mediaLink|wdrrPlayerPlayBtn|videoButton)\b.*?\1[^>]+|
280 (["\'])videoLink\b.*?\2[\s]*>\n[^\n]*
281 )data-extension(?:-ard)?=(["\'])(?P<data>(?:(?!\3).)+)\3
282 ''', webpage):
283 media_link_obj = self._parse_json(
284 mobj.group('data'), display_id, transform_source=js_to_json,
285 fatal=False)
286 if not media_link_obj:
287 continue
288 jsonp_url = try_get(
289 media_link_obj, lambda x: x['mediaObj']['url'], str)
290 if jsonp_url:
291 # metadata, or player JS with ['ref'] giving WDR id, or just media, perhaps
292 clip_id = media_link_obj['mediaObj'].get('ref')
293 if jsonp_url.endswith('.assetjsonp'):
294 asset = self._download_json(
295 jsonp_url, display_id, fatal=False, transform_source=strip_jsonp)
296 clip_id = try_get(asset, lambda x: x['trackerData']['trackerClipId'], str)
297 if clip_id:
298 jsonp_url = self._asset_url(clip_id[4:])
299 entries.append(self.url_result(jsonp_url, ie=WDRIE.ie_key()))
301 # Playlist (e.g. https://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html)
302 if not entries:
303 entries = [
304 self.url_result(
305 urllib.parse.urljoin(url, mobj.group('href')),
306 ie=WDRPageIE.ie_key())
307 for mobj in re.finditer(
308 r'<a[^>]+\bhref=(["\'])(?P<href>(?:(?!\1).)+)\1[^>]+\bdata-extension(?:-ard)?=',
309 webpage) if re.match(self._PAGE_REGEX, mobj.group('href'))
312 return self.playlist_result(entries, playlist_id=display_id)
315 class WDRElefantIE(InfoExtractor):
316 _VALID_URL = r'https?://(?:www\.)wdrmaus\.de/elefantenseite/#(?P<id>.+)'
317 _TEST = {
318 'url': 'http://www.wdrmaus.de/elefantenseite/#elefantenkino_wippe',
319 # adaptive stream: unstable file MD5
320 'info_dict': {
321 'title': 'Wippe',
322 'id': 'mdb-1198320',
323 'ext': 'mp4',
324 'upload_date': '20071003',
328 def _real_extract(self, url):
329 display_id = self._match_id(url)
331 # Table of Contents seems to always be at this address, so fetch it directly.
332 # The website fetches configurationJS.php5, which links to tableOfContentsJS.php5.
333 table_of_contents = self._download_json(
334 'https://www.wdrmaus.de/elefantenseite/data/tableOfContentsJS.php5',
335 display_id)
336 if display_id not in table_of_contents:
337 raise ExtractorError(
338 'No entry in site\'s table of contents for this URL. '
339 'Is the fragment part of the URL (after the #) correct?',
340 expected=True)
341 xml_metadata_path = table_of_contents[display_id]['xmlPath']
342 xml_metadata = self._download_xml(
343 'https://www.wdrmaus.de/elefantenseite/' + xml_metadata_path,
344 display_id)
345 zmdb_url_element = xml_metadata.find('./movie/zmdb_url')
346 if zmdb_url_element is None:
347 raise ExtractorError(
348 f'{display_id} is not a video', expected=True)
349 return self.url_result(zmdb_url_element.text, ie=WDRIE.ie_key())
352 class WDRMobileIE(InfoExtractor):
353 _VALID_URL = r'''(?x)
354 https?://mobile-ondemand\.wdr\.de/
355 .*?/fsk(?P<age_limit>[0-9]+)
356 /[0-9]+/[0-9]+/
357 (?P<id>[0-9]+)_(?P<title>[0-9]+)'''
358 IE_NAME = 'wdr:mobile'
359 _WORKING = False # no such domain
360 _TEST = {
361 'url': 'http://mobile-ondemand.wdr.de/CMS2010/mdb/ondemand/weltweit/fsk0/42/421735/421735_4283021.mp4',
362 'info_dict': {
363 'title': '4283021',
364 'id': '421735',
365 'ext': 'mp4',
366 'age_limit': 0,
368 'skip': 'Problems with loading data.',
371 def _real_extract(self, url):
372 mobj = self._match_valid_url(url)
373 return {
374 'id': mobj.group('id'),
375 'title': mobj.group('title'),
376 'age_limit': int(mobj.group('age_limit')),
377 'url': url,
378 'http_headers': {
379 'User-Agent': 'mobile',