[ard:mediathek] Remove dead test
[youtube-dl] / 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     get_element_by_attribute,
12     qualities,
13     int_or_none,
14     parse_duration,
15     unified_strdate,
16     xpath_text,
17     parse_xml,
18 )
19
20
21 class ARDMediathekIE(InfoExtractor):
22     IE_NAME = 'ARD:mediathek'
23     _VALID_URL = r'^https?://(?:(?:www\.)?ardmediathek\.de|mediathek\.daserste\.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     }, {
39         # audio
40         'url': 'http://www.ardmediathek.de/tv/WDR-H%C3%B6rspiel-Speicher/Tod-eines-Fu%C3%9Fballers/WDR-3/Audio-Podcast?documentId=28488308&bcastId=23074086',
41         'md5': '219d94d8980b4f538c7fcb0865eb7f2c',
42         'info_dict': {
43             'id': '28488308',
44             'ext': 'mp3',
45             'title': 'Tod eines Fußballers',
46             'description': 'md5:f6e39f3461f0e1f54bfa48c8875c86ef',
47             'duration': 3240,
48         },
49     }, {
50         'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
51         'only_matching': True,
52     }]
53
54     def _extract_media_info(self, media_info_url, webpage, video_id):
55         media_info = self._download_json(
56             media_info_url, video_id, 'Downloading media JSON')
57
58         formats = self._extract_formats(media_info, video_id)
59
60         if not formats:
61             if '"fsk"' in webpage:
62                 raise ExtractorError(
63                     'This video is only available after 20:00', expected=True)
64             elif media_info.get('_geoblocked'):
65                 raise ExtractorError('This video is not available due to geo restriction', expected=True)
66
67         self._sort_formats(formats)
68
69         duration = int_or_none(media_info.get('_duration'))
70         thumbnail = media_info.get('_previewImage')
71
72         subtitles = {}
73         subtitle_url = media_info.get('_subtitleUrl')
74         if subtitle_url:
75             subtitles['de'] = [{
76                 'ext': 'srt',
77                 'url': subtitle_url,
78             }]
79
80         return {
81             'id': video_id,
82             'duration': duration,
83             'thumbnail': thumbnail,
84             'formats': formats,
85             'subtitles': subtitles,
86         }
87
88     def _extract_formats(self, media_info, video_id):
89         type_ = media_info.get('_type')
90         media_array = media_info.get('_mediaArray', [])
91         formats = []
92         for num, media in enumerate(media_array):
93             for stream in media.get('_mediaStreamArray', []):
94                 stream_urls = stream.get('_stream')
95                 if not stream_urls:
96                     continue
97                 if not isinstance(stream_urls, list):
98                     stream_urls = [stream_urls]
99                 quality = stream.get('_quality')
100                 server = stream.get('_server')
101                 for stream_url in stream_urls:
102                     ext = determine_ext(stream_url)
103                     if ext == 'f4m':
104                         formats.extend(self._extract_f4m_formats(
105                             stream_url + '?hdcore=3.1.1&plugin=aasp-3.1.1.69.124',
106                             video_id, preference=-1, f4m_id='hds'))
107                     elif ext == 'm3u8':
108                         formats.extend(self._extract_m3u8_formats(
109                             stream_url, video_id, 'mp4', preference=1, m3u8_id='hls'))
110                     else:
111                         if server and server.startswith('rtmp'):
112                             f = {
113                                 'url': server,
114                                 'play_path': stream_url,
115                                 'format_id': 'a%s-rtmp-%s' % (num, quality),
116                             }
117                         elif stream_url.startswith('http'):
118                             f = {
119                                 'url': stream_url,
120                                 'format_id': 'a%s-%s-%s' % (num, ext, quality)
121                             }
122                         else:
123                             continue
124                         m = re.search(r'_(?P<width>\d+)x(?P<height>\d+)\.mp4$', stream_url)
125                         if m:
126                             f.update({
127                                 'width': int(m.group('width')),
128                                 'height': int(m.group('height')),
129                             })
130                         if type_ == 'audio':
131                             f['vcodec'] = 'none'
132                         formats.append(f)
133         return formats
134
135     def _real_extract(self, url):
136         # determine video id from url
137         m = re.match(self._VALID_URL, url)
138
139         numid = re.search(r'documentId=([0-9]+)', url)
140         if numid:
141             video_id = numid.group(1)
142         else:
143             video_id = m.group('video_id')
144
145         webpage = self._download_webpage(url, video_id)
146
147         if '>Der gewünschte Beitrag ist nicht mehr verfügbar.<' in webpage:
148             raise ExtractorError('Video %s is no longer available' % video_id, expected=True)
149
150         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:
151             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)
152
153         if re.search(r'[\?&]rss($|[=&])', url):
154             doc = parse_xml(webpage)
155             if doc.tag == 'rss':
156                 return GenericIE()._extract_rss(url, video_id, doc)
157
158         title = self._html_search_regex(
159             [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
160              r'<meta name="dcterms.title" content="(.*?)"/>',
161              r'<h4 class="headline">(.*?)</h4>'],
162             webpage, 'title')
163         description = self._html_search_meta(
164             'dcterms.abstract', webpage, 'description', default=None)
165         if description is None:
166             description = self._html_search_meta(
167                 'description', webpage, 'meta description')
168
169         # Thumbnail is sometimes not present.
170         # It is in the mobile version, but that seems to use a different URL
171         # structure altogether.
172         thumbnail = self._og_search_thumbnail(webpage, default=None)
173
174         media_streams = re.findall(r'''(?x)
175             mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
176             "([^"]+)"''', webpage)
177
178         if media_streams:
179             QUALITIES = qualities(['lo', 'hi', 'hq'])
180             formats = []
181             for furl in set(media_streams):
182                 if furl.endswith('.f4m'):
183                     fid = 'f4m'
184                 else:
185                     fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
186                     fid = fid_m.group(1) if fid_m else None
187                 formats.append({
188                     'quality': QUALITIES(fid),
189                     'format_id': fid,
190                     'url': furl,
191                 })
192             self._sort_formats(formats)
193             info = {
194                 'formats': formats,
195             }
196         else:  # request JSON file
197             info = self._extract_media_info(
198                 'http://www.ardmediathek.de/play/media/%s' % video_id, webpage, video_id)
199
200         info.update({
201             'id': video_id,
202             'title': title,
203             'description': description,
204             'thumbnail': thumbnail,
205         })
206
207         return info
208
209
210 class ARDIE(InfoExtractor):
211     _VALID_URL = '(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
212     _TEST = {
213         'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
214         'md5': 'd216c3a86493f9322545e045ddc3eb35',
215         'info_dict': {
216             'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
217             'id': '100',
218             'ext': 'mp4',
219             'duration': 2600,
220             'title': 'Die Story im Ersten: Mission unter falscher Flagge',
221             'upload_date': '20140804',
222             'thumbnail': 're:^https?://.*\.jpg$',
223         }
224     }
225
226     def _real_extract(self, url):
227         mobj = re.match(self._VALID_URL, url)
228         display_id = mobj.group('display_id')
229
230         player_url = mobj.group('mainurl') + '~playerXml.xml'
231         doc = self._download_xml(player_url, display_id)
232         video_node = doc.find('./video')
233         upload_date = unified_strdate(xpath_text(
234             video_node, './broadcastDate'))
235         thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
236
237         formats = []
238         for a in video_node.findall('.//asset'):
239             f = {
240                 'format_id': a.attrib['type'],
241                 'width': int_or_none(a.find('./frameWidth').text),
242                 'height': int_or_none(a.find('./frameHeight').text),
243                 'vbr': int_or_none(a.find('./bitrateVideo').text),
244                 'abr': int_or_none(a.find('./bitrateAudio').text),
245                 'vcodec': a.find('./codecVideo').text,
246                 'tbr': int_or_none(a.find('./totalBitrate').text),
247             }
248             if a.find('./serverPrefix').text:
249                 f['url'] = a.find('./serverPrefix').text
250                 f['playpath'] = a.find('./fileName').text
251             else:
252                 f['url'] = a.find('./fileName').text
253             formats.append(f)
254         self._sort_formats(formats)
255
256         return {
257             'id': mobj.group('id'),
258             'formats': formats,
259             'display_id': display_id,
260             'title': video_node.find('./title').text,
261             'duration': parse_duration(video_node.find('./duration').text),
262             'upload_date': upload_date,
263             'thumbnail': thumbnail,
264         }
265
266
267 class SportschauIE(ARDMediathekIE):
268     IE_NAME = 'Sportschau'
269     _VALID_URL = r'(?P<baseurl>https?://(?:www\.)?sportschau\.de/(?:[^/]+/)+video(?P<id>[^/#?]+))\.html'
270     _TESTS = [{
271         'url': 'http://www.sportschau.de/tourdefrance/videoseppeltkokainhatnichtsmitklassischemdopingzutun100.html',
272         'info_dict': {
273             'id': 'seppeltkokainhatnichtsmitklassischemdopingzutun100',
274             'ext': 'mp4',
275             'title': 'Seppelt: "Kokain hat nichts mit klassischem Doping zu tun"',
276             'thumbnail': 're:^https?://.*\.jpg$',
277             '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.',
278         },
279         'params': {
280             # m3u8 download
281             'skip_download': True,
282         },
283     }]
284
285     def _real_extract(self, url):
286         mobj = re.match(self._VALID_URL, url)
287         video_id = mobj.group('id')
288         base_url = mobj.group('baseurl')
289
290         webpage = self._download_webpage(url, video_id)
291         title = get_element_by_attribute('class', 'headline', webpage)
292         description = self._html_search_meta('description', webpage, 'description')
293
294         info = self._extract_media_info(
295             base_url + '-mc_defaultQuality-h.json', webpage, video_id)
296
297         info.update({
298             'title': title,
299             'description': description,
300         })
301
302         return info