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