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