]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ard.py
debian/copyright: Use HTTPS for Format: field.
[youtubedl] / youtube_dl / extractor / ard.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from .generic import GenericIE
8 from ..utils import (
9 determine_ext,
10 ExtractorError,
11 qualities,
12 int_or_none,
13 parse_duration,
14 unified_strdate,
15 xpath_text,
16 update_url_query,
17 )
18 from ..compat import compat_etree_fromstring
19
20
21 class ARDMediathekIE(InfoExtractor):
22 IE_NAME = 'ARD:mediathek'
23 _VALID_URL = r'^https?://(?:(?:www\.)?ardmediathek\.de|mediathek\.(?:daserste|rbb-online)\.de)/(?:.*/)(?P<video_id>[0-9]+|[^0-9][^/\?]+)[^/\?]*(?:\?.*)?'
24
25 _TESTS = [{
26 'url': 'http://www.ardmediathek.de/tv/Dokumentation-und-Reportage/Ich-liebe-das-Leben-trotzdem/rbb-Fernsehen/Video?documentId=29582122&bcastId=3822114',
27 'info_dict': {
28 'id': '29582122',
29 'ext': 'mp4',
30 'title': 'Ich liebe das Leben trotzdem',
31 'description': 'md5:45e4c225c72b27993314b31a84a5261c',
32 'duration': 4557,
33 },
34 'params': {
35 # m3u8 download
36 'skip_download': True,
37 },
38 'skip': 'HTTP Error 404: Not Found',
39 }, {
40 'url': 'http://www.ardmediathek.de/tv/Tatort/Tatort-Scheinwelten-H%C3%B6rfassung-Video/Das-Erste/Video?documentId=29522730&bcastId=602916',
41 'md5': 'f4d98b10759ac06c0072bbcd1f0b9e3e',
42 'info_dict': {
43 'id': '29522730',
44 'ext': 'mp4',
45 'title': 'Tatort: Scheinwelten - Hörfassung (Video tgl. ab 20 Uhr)',
46 'description': 'md5:196392e79876d0ac94c94e8cdb2875f1',
47 'duration': 5252,
48 },
49 'skip': 'HTTP Error 404: Not Found',
50 }, {
51 # audio
52 'url': 'http://www.ardmediathek.de/tv/WDR-H%C3%B6rspiel-Speicher/Tod-eines-Fu%C3%9Fballers/WDR-3/Audio-Podcast?documentId=28488308&bcastId=23074086',
53 'md5': '219d94d8980b4f538c7fcb0865eb7f2c',
54 'info_dict': {
55 'id': '28488308',
56 'ext': 'mp3',
57 'title': 'Tod eines Fußballers',
58 'description': 'md5:f6e39f3461f0e1f54bfa48c8875c86ef',
59 'duration': 3240,
60 },
61 'skip': 'HTTP Error 404: Not Found',
62 }, {
63 'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
64 'only_matching': True,
65 }, {
66 # audio
67 'url': 'http://mediathek.rbb-online.de/radio/Hörspiel/Vor-dem-Fest/kulturradio/Audio?documentId=30796318&topRessort=radio&bcastId=9839158',
68 'md5': '4e8f00631aac0395fee17368ac0e9867',
69 'info_dict': {
70 'id': '30796318',
71 'ext': 'mp3',
72 'title': 'Vor dem Fest',
73 'description': 'md5:c0c1c8048514deaed2a73b3a60eecacb',
74 'duration': 3287,
75 },
76 'skip': 'Video is no longer available',
77 }]
78
79 def _extract_media_info(self, media_info_url, webpage, video_id):
80 media_info = self._download_json(
81 media_info_url, video_id, 'Downloading media JSON')
82
83 formats = self._extract_formats(media_info, video_id)
84
85 if not formats:
86 if '"fsk"' in webpage:
87 raise ExtractorError(
88 'This video is only available after 20:00', expected=True)
89 elif media_info.get('_geoblocked'):
90 raise ExtractorError('This video is not available due to geo restriction', expected=True)
91
92 self._sort_formats(formats)
93
94 duration = int_or_none(media_info.get('_duration'))
95 thumbnail = media_info.get('_previewImage')
96 is_live = media_info.get('_isLive') is True
97
98 subtitles = {}
99 subtitle_url = media_info.get('_subtitleUrl')
100 if subtitle_url:
101 subtitles['de'] = [{
102 'ext': 'ttml',
103 'url': subtitle_url,
104 }]
105
106 return {
107 'id': video_id,
108 'duration': duration,
109 'thumbnail': thumbnail,
110 'is_live': is_live,
111 'formats': formats,
112 'subtitles': subtitles,
113 }
114
115 def _extract_formats(self, media_info, video_id):
116 type_ = media_info.get('_type')
117 media_array = media_info.get('_mediaArray', [])
118 formats = []
119 for num, media in enumerate(media_array):
120 for stream in media.get('_mediaStreamArray', []):
121 stream_urls = stream.get('_stream')
122 if not stream_urls:
123 continue
124 if not isinstance(stream_urls, list):
125 stream_urls = [stream_urls]
126 quality = stream.get('_quality')
127 server = stream.get('_server')
128 for stream_url in stream_urls:
129 ext = determine_ext(stream_url)
130 if quality != 'auto' and ext in ('f4m', 'm3u8'):
131 continue
132 if ext == 'f4m':
133 formats.extend(self._extract_f4m_formats(
134 update_url_query(stream_url, {
135 'hdcore': '3.1.1',
136 'plugin': 'aasp-3.1.1.69.124'
137 }),
138 video_id, f4m_id='hds', fatal=False))
139 elif ext == 'm3u8':
140 formats.extend(self._extract_m3u8_formats(
141 stream_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
142 else:
143 if server and server.startswith('rtmp'):
144 f = {
145 'url': server,
146 'play_path': stream_url,
147 'format_id': 'a%s-rtmp-%s' % (num, quality),
148 }
149 elif stream_url.startswith('http'):
150 f = {
151 'url': stream_url,
152 'format_id': 'a%s-%s-%s' % (num, ext, quality)
153 }
154 else:
155 continue
156 m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
157 if m:
158 f.update({
159 'width': int(m.group('width')),
160 'height': int(m.group('height')),
161 })
162 if type_ == 'audio':
163 f['vcodec'] = 'none'
164 formats.append(f)
165 return formats
166
167 def _real_extract(self, url):
168 # determine video id from url
169 m = re.match(self._VALID_URL, url)
170
171 document_id = None
172
173 numid = re.search(r'documentId=([0-9]+)', url)
174 if numid:
175 document_id = video_id = numid.group(1)
176 else:
177 video_id = m.group('video_id')
178
179 webpage = self._download_webpage(url, video_id)
180
181 ERRORS = (
182 ('>Leider liegt eine Störung vor.', 'Video %s is unavailable'),
183 ('>Der gewünschte Beitrag ist nicht mehr verfügbar.<',
184 'Video %s is no longer available'),
185 )
186
187 for pattern, message in ERRORS:
188 if pattern in webpage:
189 raise ExtractorError(message % video_id, expected=True)
190
191 if re.search(r'[\?&]rss($|[=&])', url):
192 doc = compat_etree_fromstring(webpage.encode('utf-8'))
193 if doc.tag == 'rss':
194 return GenericIE()._extract_rss(url, video_id, doc)
195
196 title = self._html_search_regex(
197 [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
198 r'<meta name="dcterms\.title" content="(.*?)"/>',
199 r'<h4 class="headline">(.*?)</h4>'],
200 webpage, 'title')
201 description = self._html_search_meta(
202 'dcterms.abstract', webpage, 'description', default=None)
203 if description is None:
204 description = self._html_search_meta(
205 'description', webpage, 'meta description')
206
207 # Thumbnail is sometimes not present.
208 # It is in the mobile version, but that seems to use a different URL
209 # structure altogether.
210 thumbnail = self._og_search_thumbnail(webpage, default=None)
211
212 media_streams = re.findall(r'''(?x)
213 mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
214 "([^"]+)"''', webpage)
215
216 if media_streams:
217 QUALITIES = qualities(['lo', 'hi', 'hq'])
218 formats = []
219 for furl in set(media_streams):
220 if furl.endswith('.f4m'):
221 fid = 'f4m'
222 else:
223 fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
224 fid = fid_m.group(1) if fid_m else None
225 formats.append({
226 'quality': QUALITIES(fid),
227 'format_id': fid,
228 'url': furl,
229 })
230 self._sort_formats(formats)
231 info = {
232 'formats': formats,
233 }
234 else: # request JSON file
235 if not document_id:
236 video_id = self._search_regex(
237 r'/play/(?:config|media)/(\d+)', webpage, 'media id')
238 info = self._extract_media_info(
239 'http://www.ardmediathek.de/play/media/%s' % video_id,
240 webpage, video_id)
241
242 info.update({
243 'id': video_id,
244 'title': self._live_title(title) if info.get('is_live') else title,
245 'description': description,
246 'thumbnail': thumbnail,
247 })
248
249 return info
250
251
252 class ARDIE(InfoExtractor):
253 _VALID_URL = r'(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
254 _TEST = {
255 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
256 'md5': 'd216c3a86493f9322545e045ddc3eb35',
257 'info_dict': {
258 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
259 'id': '100',
260 'ext': 'mp4',
261 'duration': 2600,
262 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
263 'upload_date': '20140804',
264 'thumbnail': r're:^https?://.*\.jpg$',
265 },
266 'skip': 'HTTP Error 404: Not Found',
267 }
268
269 def _real_extract(self, url):
270 mobj = re.match(self._VALID_URL, url)
271 display_id = mobj.group('display_id')
272
273 player_url = mobj.group('mainurl') + '~playerXml.xml'
274 doc = self._download_xml(player_url, display_id)
275 video_node = doc.find('./video')
276 upload_date = unified_strdate(xpath_text(
277 video_node, './broadcastDate'))
278 thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
279
280 formats = []
281 for a in video_node.findall('.//asset'):
282 f = {
283 'format_id': a.attrib['type'],
284 'width': int_or_none(a.find('./frameWidth').text),
285 'height': int_or_none(a.find('./frameHeight').text),
286 'vbr': int_or_none(a.find('./bitrateVideo').text),
287 'abr': int_or_none(a.find('./bitrateAudio').text),
288 'vcodec': a.find('./codecVideo').text,
289 'tbr': int_or_none(a.find('./totalBitrate').text),
290 }
291 if a.find('./serverPrefix').text:
292 f['url'] = a.find('./serverPrefix').text
293 f['playpath'] = a.find('./fileName').text
294 else:
295 f['url'] = a.find('./fileName').text
296 formats.append(f)
297 self._sort_formats(formats)
298
299 return {
300 'id': mobj.group('id'),
301 'formats': formats,
302 'display_id': display_id,
303 'title': video_node.find('./title').text,
304 'duration': parse_duration(video_node.find('./duration').text),
305 'upload_date': upload_date,
306 'thumbnail': thumbnail,
307 }