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