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