]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/wdr.py
621de1e1efb73a9a377a46fe0fa702e595c3cde5
[youtubedl] / youtube_dl / 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 ..utils import (
8 determine_ext,
9 ExtractorError,
10 js_to_json,
11 strip_jsonp,
12 unified_strdate,
13 update_url_query,
14 urlhandle_detect_ext,
15 )
16
17
18 class WDRBaseIE(InfoExtractor):
19 def _extract_wdr_video(self, webpage, display_id):
20 # for wdr.de the data-extension is in a tag with the class "mediaLink"
21 # for wdr.de radio players, in a tag with the class "wdrrPlayerPlayBtn"
22 # for wdrmaus, in a tag with the class "videoButton" (previously a link
23 # to the page in a multiline "videoLink"-tag)
24 json_metadata = self._html_search_regex(
25 r'''(?sx)class=
26 (?:
27 (["\'])(?:mediaLink|wdrrPlayerPlayBtn|videoButton)\b.*?\1[^>]+|
28 (["\'])videoLink\b.*?\2[\s]*>\n[^\n]*
29 )data-extension=(["\'])(?P<data>(?:(?!\3).)+)\3
30 ''',
31 webpage, 'media link', default=None, group='data')
32
33 if not json_metadata:
34 return
35
36 media_link_obj = self._parse_json(json_metadata, display_id,
37 transform_source=js_to_json)
38 jsonp_url = media_link_obj['mediaObj']['url']
39
40 metadata = self._download_json(
41 jsonp_url, display_id, transform_source=strip_jsonp)
42
43 metadata_tracker_data = metadata['trackerData']
44 metadata_media_resource = metadata['mediaResource']
45
46 formats = []
47
48 # check if the metadata contains a direct URL to a file
49 for kind, media_resource in metadata_media_resource.items():
50 if kind not in ('dflt', 'alt'):
51 continue
52
53 for tag_name, medium_url in media_resource.items():
54 if tag_name not in ('videoURL', 'audioURL'):
55 continue
56
57 ext = determine_ext(medium_url)
58 if ext == 'm3u8':
59 formats.extend(self._extract_m3u8_formats(
60 medium_url, display_id, 'mp4', 'm3u8_native',
61 m3u8_id='hls'))
62 elif ext == 'f4m':
63 manifest_url = update_url_query(
64 medium_url, {'hdcore': '3.2.0', 'plugin': 'aasp-3.2.0.77.18'})
65 formats.extend(self._extract_f4m_formats(
66 manifest_url, display_id, f4m_id='hds', fatal=False))
67 elif ext == 'smil':
68 formats.extend(self._extract_smil_formats(
69 medium_url, 'stream', fatal=False))
70 else:
71 a_format = {
72 'url': medium_url
73 }
74 if ext == 'unknown_video':
75 urlh = self._request_webpage(
76 medium_url, display_id, note='Determining extension')
77 ext = urlhandle_detect_ext(urlh)
78 a_format['ext'] = ext
79 formats.append(a_format)
80
81 self._sort_formats(formats)
82
83 subtitles = {}
84 caption_url = metadata_media_resource.get('captionURL')
85 if caption_url:
86 subtitles['de'] = [{
87 'url': caption_url,
88 'ext': 'ttml',
89 }]
90
91 title = metadata_tracker_data['trackerClipTitle']
92
93 return {
94 'id': metadata_tracker_data.get('trackerClipId', display_id),
95 'display_id': display_id,
96 'title': title,
97 'alt_title': metadata_tracker_data.get('trackerClipSubcategory'),
98 'formats': formats,
99 'subtitles': subtitles,
100 'upload_date': unified_strdate(metadata_tracker_data.get('trackerClipAirTime')),
101 }
102
103
104 class WDRIE(WDRBaseIE):
105 _CURRENT_MAUS_URL = r'https?://(?:www\.)wdrmaus.de/(?:[^/]+/){1,2}[^/?#]+\.php5'
106 _PAGE_REGEX = r'/(?:mediathek/)?[^/]+/(?P<type>[^/]+)/(?P<display_id>.+)\.html'
107 _VALID_URL = r'(?P<page_url>https?://(?:www\d\.)?wdr\d?\.de)' + _PAGE_REGEX + '|' + _CURRENT_MAUS_URL
108
109 _TESTS = [
110 {
111 'url': 'http://www1.wdr.de/mediathek/video/sendungen/doku-am-freitag/video-geheimnis-aachener-dom-100.html',
112 # HDS download, MD5 is unstable
113 'info_dict': {
114 'id': 'mdb-1058683',
115 'ext': 'flv',
116 'display_id': 'doku-am-freitag/video-geheimnis-aachener-dom-100',
117 'title': 'Geheimnis Aachener Dom',
118 'alt_title': 'Doku am Freitag',
119 'upload_date': '20160304',
120 'description': 'md5:87be8ff14d8dfd7a7ee46f0299b52318',
121 'is_live': False,
122 'subtitles': {'de': [{
123 'url': 'http://ondemand-ww.wdr.de/medp/fsk0/105/1058683/1058683_12220974.xml',
124 'ext': 'ttml',
125 }]},
126 },
127 },
128 {
129 'url': 'http://www1.wdr.de/mediathek/audio/wdr3/wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100.html',
130 'md5': 'f4c1f96d01cf285240f53ea4309663d8',
131 'info_dict': {
132 'id': 'mdb-1072000',
133 'ext': 'mp3',
134 'display_id': 'wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100',
135 'title': 'Schriftstellerin Juli Zeh',
136 'alt_title': 'WDR 3 Gespräch am Samstag',
137 'upload_date': '20160312',
138 'description': 'md5:e127d320bc2b1f149be697ce044a3dd7',
139 'is_live': False,
140 'subtitles': {}
141 },
142 },
143 {
144 'url': 'http://www1.wdr.de/mediathek/video/live/index.html',
145 'info_dict': {
146 'id': 'mdb-103364',
147 'ext': 'mp4',
148 'display_id': 'index',
149 'title': r're:^WDR Fernsehen im Livestream [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
150 'alt_title': 'WDR Fernsehen Live',
151 'upload_date': None,
152 'description': 'md5:ae2ff888510623bf8d4b115f95a9b7c9',
153 'is_live': True,
154 'subtitles': {}
155 },
156 'params': {
157 'skip_download': True, # m3u8 download
158 },
159 },
160 {
161 'url': 'http://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html',
162 'playlist_mincount': 8,
163 'info_dict': {
164 'id': 'aktuelle-stunde/aktuelle-stunde-120',
165 },
166 },
167 {
168 'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
169 'info_dict': {
170 'id': 'mdb-1323501',
171 'ext': 'mp4',
172 'upload_date': 're:^[0-9]{8}$',
173 'title': 're:^Die Sendung mit der Maus vom [0-9.]{10}$',
174 'description': 'Die Seite mit der Maus -',
175 },
176 'skip': 'The id changes from week to week because of the new episode'
177 },
178 {
179 'url': 'http://www.wdrmaus.de/filme/sachgeschichten/achterbahn.php5',
180 'md5': '803138901f6368ee497b4d195bb164f2',
181 'info_dict': {
182 'id': 'mdb-186083',
183 'ext': 'mp4',
184 'upload_date': '20130919',
185 'title': 'Sachgeschichte - Achterbahn ',
186 'description': 'Die Seite mit der Maus -',
187 },
188 },
189 {
190 'url': 'http://www1.wdr.de/radio/player/radioplayer116~_layout-popupVersion.html',
191 # Live stream, MD5 unstable
192 'info_dict': {
193 'id': 'mdb-869971',
194 'ext': 'flv',
195 'title': 'COSMO Livestream',
196 'description': 'md5:2309992a6716c347891c045be50992e4',
197 'upload_date': '20160101',
198 },
199 }
200 ]
201
202 def _real_extract(self, url):
203 mobj = re.match(self._VALID_URL, url)
204 url_type = mobj.group('type')
205 page_url = mobj.group('page_url')
206 display_id = mobj.group('display_id')
207 webpage = self._download_webpage(url, display_id)
208
209 info_dict = self._extract_wdr_video(webpage, display_id)
210
211 if not info_dict:
212 entries = [
213 self.url_result(page_url + href[0], 'WDR')
214 for href in re.findall(
215 r'<a href="(%s)"[^>]+data-extension=' % self._PAGE_REGEX,
216 webpage)
217 ]
218
219 if entries: # Playlist page
220 return self.playlist_result(entries, playlist_id=display_id)
221
222 raise ExtractorError('No downloadable streams found', expected=True)
223
224 is_live = url_type == 'live'
225
226 if is_live:
227 info_dict.update({
228 'title': self._live_title(info_dict['title']),
229 'upload_date': None,
230 })
231 elif 'upload_date' not in info_dict:
232 info_dict['upload_date'] = unified_strdate(self._html_search_meta('DC.Date', webpage, 'upload date'))
233
234 info_dict.update({
235 'description': self._html_search_meta('Description', webpage),
236 'is_live': is_live,
237 })
238
239 return info_dict
240
241
242 class WDRMobileIE(InfoExtractor):
243 _VALID_URL = r'''(?x)
244 https?://mobile-ondemand\.wdr\.de/
245 .*?/fsk(?P<age_limit>[0-9]+)
246 /[0-9]+/[0-9]+/
247 (?P<id>[0-9]+)_(?P<title>[0-9]+)'''
248 IE_NAME = 'wdr:mobile'
249 _TEST = {
250 'url': 'http://mobile-ondemand.wdr.de/CMS2010/mdb/ondemand/weltweit/fsk0/42/421735/421735_4283021.mp4',
251 'info_dict': {
252 'title': '4283021',
253 'id': '421735',
254 'ext': 'mp4',
255 'age_limit': 0,
256 },
257 'skip': 'Problems with loading data.'
258 }
259
260 def _real_extract(self, url):
261 mobj = re.match(self._VALID_URL, url)
262 return {
263 'id': mobj.group('id'),
264 'title': mobj.group('title'),
265 'age_limit': int(mobj.group('age_limit')),
266 'url': url,
267 'http_headers': {
268 'User-Agent': 'mobile',
269 },
270 }