]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/wdr.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / wdr.py
CommitLineData
b461641f 1import re
add96eb9 2import urllib.parse
b461641f
S
3
4from .common import InfoExtractor
1cc79574 5from ..utils import (
e897bd82 6 ExtractorError,
bec2c14f 7 determine_ext,
50e93e03 8 dict_get,
33a1ff71 9 js_to_json,
37f97295 10 strip_jsonp,
54e8f62e 11 try_get,
cd7481a3 12 unified_strdate,
6869d634 13 update_url_query,
29f7c58a 14 url_or_none,
e897bd82 15 urlhandle_detect_ext,
b461641f
S
16)
17
18
54e8f62e 19class WDRIE(InfoExtractor):
50e93e03 20 __API_URL_TPL = '//deviceids-medp.wdr.de/ondemand/%s/%s'
c98d4df2 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 '''
1915662d 26 _GEO_COUNTRIES = ['DE']
50e93e03 27 _TESTS = [{
54e8f62e
S
28 'url': 'http://deviceids-medp.wdr.de/ondemand/155/1557833.js',
29 'info_dict': {
0ce39bc5 30 'id': 'mdb-1557833',
54e8f62e 31 'ext': 'mp4',
0ce39bc5
S
32 'title': 'Biathlon-Staffel verpasst Podest bei Olympia-Generalprobe',
33 'upload_date': '20180112',
54e8f62e 34 },
50e93e03 35 }]
36
37 def _asset_url(self, wdr_id):
38 id_len = max(len(wdr_id), 5)
add96eb9 39 return ''.join(('https:', self.__API_URL_TPL % (wdr_id[:id_len - 4], wdr_id), '.js'))
6d0d4fc2 40
54e8f62e
S
41 def _real_extract(self, url):
42 video_id = self._match_id(url)
6d0d4fc2 43
50e93e03 44 if url.startswith('wdr:'):
45 video_id = url[4:]
46 url = self._asset_url(video_id)
47
6d0d4fc2 48 metadata = self._download_json(
54e8f62e 49 url, video_id, transform_source=strip_jsonp)
6d0d4fc2 50
54e8f62e
S
51 is_live = metadata.get('mediaType') == 'live'
52
53 tracker_data = metadata['trackerData']
29f7c58a 54 title = tracker_data['trackerClipTitle']
54e8f62e 55 media_resource = metadata['mediaResource']
6d0d4fc2
YCH
56
57 formats = []
c76eb41b 58 subtitles = {}
6d0d4fc2
YCH
59
60 # check if the metadata contains a direct URL to a file
29f7c58a 61 for kind, media in media_resource.items():
6de54568 62 if kind == 'captionsHash':
29f7c58a 63 for ext, url in media.items():
6de54568 64 subtitles.setdefault('de', []).append({
65 'url': url,
66 'ext': ext,
67 })
68 continue
69
6d0d4fc2
YCH
70 if kind not in ('dflt', 'alt'):
71 continue
29f7c58a 72 if not isinstance(media, dict):
73 continue
6d0d4fc2 74
29f7c58a 75 for tag_name, medium_url in media.items():
6d0d4fc2
YCH
76 if tag_name not in ('videoURL', 'audioURL'):
77 continue
78
79 ext = determine_ext(medium_url)
80 if ext == 'm3u8':
81 formats.extend(self._extract_m3u8_formats(
54e8f62e 82 medium_url, video_id, 'mp4', 'm3u8_native',
6d0d4fc2
YCH
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(
54e8f62e 88 manifest_url, video_id, f4m_id='hds', fatal=False))
6d0d4fc2
YCH
89 elif ext == 'smil':
90 formats.extend(self._extract_smil_formats(
91 medium_url, 'stream', fatal=False))
92 else:
93 a_format = {
add96eb9 94 'url': medium_url,
6d0d4fc2
YCH
95 }
96 if ext == 'unknown_video':
97 urlh = self._request_webpage(
54e8f62e 98 medium_url, video_id, note='Determining extension')
6d0d4fc2
YCH
99 ext = urlhandle_detect_ext(urlh)
100 a_format['ext'] = ext
101 formats.append(a_format)
102
29f7c58a 103 caption_url = media_resource.get('captionURL')
104 if caption_url:
105 subtitles['de'] = [{
106 'url': caption_url,
107 'ext': 'ttml',
108 }]
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,
118 })
6d0d4fc2
YCH
119
120 return {
54e8f62e 121 'id': tracker_data.get('trackerClipId', video_id),
39ca3b5c 122 'title': title,
54e8f62e 123 'alt_title': tracker_data.get('trackerClipSubcategory'),
6d0d4fc2
YCH
124 'formats': formats,
125 'subtitles': subtitles,
54e8f62e
S
126 'upload_date': unified_strdate(tracker_data.get('trackerClipAirTime')),
127 'is_live': is_live,
6d0d4fc2
YCH
128 }
129
130
6368e2e6 131class WDRPageIE(WDRIE): # XXX: Do not subclass from concrete IE
50e93e03 132 _MAUS_REGEX = r'https?://(?:www\.)wdrmaus.de/(?:[^/]+/)*?(?P<maus_id>[^/?#.]+)(?:/?|/index\.php5|\.php5)$'
54e8f62e 133 _PAGE_REGEX = r'/(?:mediathek/)?(?:[^/]+/)*(?P<display_id>[^/]+)\.html'
50e93e03 134 _VALID_URL = r'https?://(?:www\d?\.)?(?:(?:kinder\.)?wdr\d?|sportschau)\.de' + _PAGE_REGEX + '|' + _MAUS_REGEX
c0837a12 135
b461641f
S
136 _TESTS = [
137 {
c0837a12 138 'url': 'http://www1.wdr.de/mediathek/video/sendungen/doku-am-freitag/video-geheimnis-aachener-dom-100.html',
a4a8201c 139 # HDS download, MD5 is unstable
b461641f 140 'info_dict': {
c0837a12 141 'id': 'mdb-1058683',
b461641f 142 'ext': 'flv',
c0837a12
BW
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': [{
bc7e7adf
YCH
150 'url': 'http://ondemand-ww.wdr.de/medp/fsk0/105/1058683/1058683_12220974.xml',
151 'ext': 'ttml',
c0837a12 152 }]},
b461641f 153 },
54e8f62e 154 'skip': 'HTTP Error 404: Not Found',
b461641f
S
155 },
156 {
c0837a12
BW
157 'url': 'http://www1.wdr.de/mediathek/audio/wdr3/wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100.html',
158 'md5': 'f4c1f96d01cf285240f53ea4309663d8',
b461641f 159 'info_dict': {
c0837a12 160 'id': 'mdb-1072000',
b461641f 161 'ext': 'mp3',
c0837a12
BW
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,
add96eb9 168 'subtitles': {},
b461641f 169 },
54e8f62e 170 'skip': 'HTTP Error 404: Not Found',
b461641f 171 },
176cf9e0 172 {
5d039549 173 # FIXME: Asset JSON is directly embedded in webpage
c0837a12 174 'url': 'http://www1.wdr.de/mediathek/video/live/index.html',
dd8982f1 175 'info_dict': {
50e93e03 176 'id': 'mdb-2296252',
bec2c14f 177 'ext': 'mp4',
50e93e03 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}$',
c0837a12 179 'alt_title': 'WDR Fernsehen Live',
50e93e03 180 'upload_date': '20201112',
c0837a12 181 'is_live': True,
bec2c14f
BW
182 },
183 'params': {
184 'skip_download': True, # m3u8 download
185 },
b8988b63
AA
186 },
187 {
c0837a12 188 'url': 'http://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html',
50e93e03 189 'playlist_mincount': 6,
b8988b63 190 'info_dict': {
54e8f62e 191 'id': 'aktuelle-stunde-120',
b8988b63 192 },
14f7a2b8
BW
193 },
194 {
195 'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
196 'info_dict': {
50e93e03 197 'id': 'mdb-2627637',
f802c486 198 'ext': 'mp4',
14f7a2b8 199 'upload_date': 're:^[0-9]{8}$',
50e93e03 200 'title': 're:^Die Sendung (?:mit der Maus )?vom [0-9.]{10}$',
14f7a2b8 201 },
add96eb9 202 'skip': 'The id changes from week to week because of the new episode',
14f7a2b8 203 },
949fc42e 204 {
f802c486 205 'url': 'http://www.wdrmaus.de/filme/sachgeschichten/achterbahn.php5',
6d0d4fc2 206 'md5': '803138901f6368ee497b4d195bb164f2',
949fc42e
BW
207 'info_dict': {
208 'id': 'mdb-186083',
6d0d4fc2 209 'ext': 'mp4',
949fc42e
BW
210 'upload_date': '20130919',
211 'title': 'Sachgeschichte - Achterbahn ',
949fc42e 212 },
50e93e03 213 'skip': 'HTTP Error 404: Not Found',
949fc42e 214 },
50918c4e
YCH
215 {
216 'url': 'http://www1.wdr.de/radio/player/radioplayer116~_layout-popupVersion.html',
6869d634 217 # Live stream, MD5 unstable
50918c4e
YCH
218 'info_dict': {
219 'id': 'mdb-869971',
54e8f62e
S
220 'ext': 'mp4',
221 'title': r're:^COSMO Livestream [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
5d039549
SG
222 'alt_title': 'COSMO Livestream',
223 'live_status': 'is_live',
50918c4e
YCH
224 'upload_date': '20160101',
225 },
54e8f62e
S
226 'params': {
227 'skip_download': True, # m3u8 download
add96eb9 228 },
54e8f62e
S
229 },
230 {
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',
237 },
238 'params': {
239 'skip_download': True,
240 },
50e93e03 241 'skip': 'HTTP Error 404: Not Found',
54e8f62e
S
242 },
243 {
244 'url': 'http://www.sportschau.de/handballem2018/audio-vorschau---die-handball-em-startet-mit-grossem-favoritenfeld-100.html',
245 'only_matching': True,
29f7c58a 246 },
247 {
248 'url': 'https://kinder.wdr.de/tv/die-sendung-mit-dem-elefanten/av/video-folge---astronaut-100.html',
249 'only_matching': True,
250 },
5d039549
SG
251 {
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',
259 },
add96eb9 260 },
b461641f
S
261 ]
262
263 def _real_extract(self, url):
5ad28e7f 264 mobj = self._match_valid_url(url)
50e93e03 265 display_id = dict_get(mobj.groupdict(), ('display_id', 'maus_id'), 'wdrmaus')
c0837a12 266 webpage = self._download_webpage(url, display_id)
b461641f 267
54e8f62e 268 entries = []
b8988b63 269
54e8f62e 270 # Article with several videos
b461641f 271
5d039549 272 # for wdr.de the data-extension-ard is in a tag with the class "mediaLink"
54e8f62e
S
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=
278 (?:
279 (["\'])(?:mediaLink|wdrrPlayerPlayBtn|videoButton)\b.*?\1[^>]+|
280 (["\'])videoLink\b.*?\2[\s]*>\n[^\n]*
5d039549 281 )data-extension(?:-ard)?=(["\'])(?P<data>(?:(?!\3).)+)\3
54e8f62e
S
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(
add96eb9 289 media_link_obj, lambda x: x['mediaObj']['url'], str)
54e8f62e 290 if jsonp_url:
50e93e03 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)
add96eb9 296 clip_id = try_get(asset, lambda x: x['trackerData']['trackerClipId'], str)
50e93e03 297 if clip_id:
298 jsonp_url = self._asset_url(clip_id[4:])
54e8f62e 299 entries.append(self.url_result(jsonp_url, ie=WDRIE.ie_key()))
6d0d4fc2 300
54e8f62e
S
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(
add96eb9 305 urllib.parse.urljoin(url, mobj.group('href')),
54e8f62e
S
306 ie=WDRPageIE.ie_key())
307 for mobj in re.finditer(
5d039549 308 r'<a[^>]+\bhref=(["\'])(?P<href>(?:(?!\1).)+)\1[^>]+\bdata-extension(?:-ard)?=',
54e8f62e
S
309 webpage) if re.match(self._PAGE_REGEX, mobj.group('href'))
310 ]
cd7481a3 311
54e8f62e 312 return self.playlist_result(entries, playlist_id=display_id)
cd7481a3 313
2d8bb80c 314
54e8f62e
S
315class WDRElefantIE(InfoExtractor):
316 _VALID_URL = r'https?://(?:www\.)wdrmaus\.de/elefantenseite/#(?P<id>.+)'
317 _TEST = {
50e93e03 318 'url': 'http://www.wdrmaus.de/elefantenseite/#elefantenkino_wippe',
319 # adaptive stream: unstable file MD5
54e8f62e 320 'info_dict': {
50e93e03 321 'title': 'Wippe',
322 'id': 'mdb-1198320',
54e8f62e 323 'ext': 'mp4',
add96eb9 324 'upload_date': '20071003',
54e8f62e
S
325 },
326 }
2d8bb80c
SL
327
328 def _real_extract(self, url):
54e8f62e 329 display_id = self._match_id(url)
2d8bb80c
SL
330
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(
54e8f62e
S
334 'https://www.wdrmaus.de/elefantenseite/data/tableOfContentsJS.php5',
335 display_id)
2d8bb80c
SL
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(
54e8f62e
S
343 'https://www.wdrmaus.de/elefantenseite/' + xml_metadata_path,
344 display_id)
2d8bb80c
SL
345 zmdb_url_element = xml_metadata.find('./movie/zmdb_url')
346 if zmdb_url_element is None:
347 raise ExtractorError(
add96eb9 348 f'{display_id} is not a video', expected=True)
54e8f62e 349 return self.url_result(zmdb_url_element.text, ie=WDRIE.ie_key())
2d8bb80c
SL
350
351
e4cbb5f3
PH
352class 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'
50e93e03 359 _WORKING = False # no such domain
e4cbb5f3
PH
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',
7807ee66 365 'ext': 'mp4',
e4cbb5f3
PH
366 'age_limit': 0,
367 },
add96eb9 368 'skip': 'Problems with loading data.',
e4cbb5f3
PH
369 }
370
371 def _real_extract(self, url):
5ad28e7f 372 mobj = self._match_valid_url(url)
e4cbb5f3
PH
373 return {
374 'id': mobj.group('id'),
375 'title': mobj.group('title'),
376 'age_limit': int(mobj.group('age_limit')),
377 'url': url,
e1554a40
JMF
378 'http_headers': {
379 'User-Agent': 'mobile',
380 },
e4cbb5f3 381 }