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