]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/ard.py
Update changelog.
[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 parse_xml,
17 )
18
19
20 class ARDMediathekIE(InfoExtractor):
21 IE_NAME = 'ARD:mediathek'
22 _VALID_URL = r'^https?://(?:(?:www\.)?ardmediathek\.de|mediathek\.daserste\.de)/(?:.*/)(?P<video_id>[0-9]+|[^0-9][^/\?]+)[^/\?]*(?:\?.*)?'
23
24 _TESTS = [{
25 'url': 'http://mediathek.daserste.de/sendungen_a-z/328454_anne-will/22429276_vertrauen-ist-gut-spionieren-ist-besser-geht',
26 'only_matching': True,
27 }, {
28 'url': 'http://www.ardmediathek.de/tv/Tatort/Das-Wunder-von-Wolbeck-Video-tgl-ab-20/Das-Erste/Video?documentId=22490580&bcastId=602916',
29 'info_dict': {
30 'id': '22490580',
31 'ext': 'mp4',
32 'title': 'Das Wunder von Wolbeck (Video tgl. ab 20 Uhr)',
33 'description': 'Auf einem restaurierten Hof bei Wolbeck wird der Heilpraktiker Raffael Lembeck eines morgens von seiner Frau Stella tot aufgefunden. Das Opfer war offensichtlich in seiner Praxis zu Fall gekommen und ist dann verblutet, erklärt Prof. Boerne am Tatort.',
34 },
35 'skip': 'Blocked outside of Germany',
36 }]
37
38 def _real_extract(self, url):
39 # determine video id from url
40 m = re.match(self._VALID_URL, url)
41
42 numid = re.search(r'documentId=([0-9]+)', url)
43 if numid:
44 video_id = numid.group(1)
45 else:
46 video_id = m.group('video_id')
47
48 webpage = self._download_webpage(url, video_id)
49
50 if '>Der gewünschte Beitrag ist nicht mehr verfügbar.<' in webpage:
51 raise ExtractorError('Video %s is no longer available' % video_id, expected=True)
52
53 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:
54 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)
55
56 if re.search(r'[\?&]rss($|[=&])', url):
57 doc = parse_xml(webpage)
58 if doc.tag == 'rss':
59 return GenericIE()._extract_rss(url, video_id, doc)
60
61 title = self._html_search_regex(
62 [r'<h1(?:\s+class="boxTopHeadline")?>(.*?)</h1>',
63 r'<meta name="dcterms.title" content="(.*?)"/>',
64 r'<h4 class="headline">(.*?)</h4>'],
65 webpage, 'title')
66 description = self._html_search_meta(
67 'dcterms.abstract', webpage, 'description', default=None)
68 if description is None:
69 description = self._html_search_meta(
70 'description', webpage, 'meta description')
71
72 # Thumbnail is sometimes not present.
73 # It is in the mobile version, but that seems to use a different URL
74 # structure altogether.
75 thumbnail = self._og_search_thumbnail(webpage, default=None)
76
77 media_streams = re.findall(r'''(?x)
78 mediaCollection\.addMediaStream\([0-9]+,\s*[0-9]+,\s*"[^"]*",\s*
79 "([^"]+)"''', webpage)
80
81 if media_streams:
82 QUALITIES = qualities(['lo', 'hi', 'hq'])
83 formats = []
84 for furl in set(media_streams):
85 if furl.endswith('.f4m'):
86 fid = 'f4m'
87 else:
88 fid_m = re.match(r'.*\.([^.]+)\.[^.]+$', furl)
89 fid = fid_m.group(1) if fid_m else None
90 formats.append({
91 'quality': QUALITIES(fid),
92 'format_id': fid,
93 'url': furl,
94 })
95 else: # request JSON file
96 media_info = self._download_json(
97 'http://www.ardmediathek.de/play/media/%s' % video_id, video_id)
98 # The second element of the _mediaArray contains the standard http urls
99 streams = media_info['_mediaArray'][1]['_mediaStreamArray']
100 if not streams:
101 if '"fsk"' in webpage:
102 raise ExtractorError('This video is only available after 20:00')
103
104 formats = []
105 for s in streams:
106 if type(s['_stream']) == list:
107 for index, url in enumerate(s['_stream'][::-1]):
108 quality = s['_quality'] + index
109 formats.append({
110 'quality': quality,
111 'url': url,
112 'format_id': '%s-%s' % (determine_ext(url), quality)
113 })
114 continue
115
116 format = {
117 'quality': s['_quality'],
118 'url': s['_stream'],
119 }
120
121 format['format_id'] = '%s-%s' % (
122 determine_ext(format['url']), format['quality'])
123
124 formats.append(format)
125
126 self._sort_formats(formats)
127
128 return {
129 'id': video_id,
130 'title': title,
131 'description': description,
132 'formats': formats,
133 'thumbnail': thumbnail,
134 }
135
136
137 class ARDIE(InfoExtractor):
138 _VALID_URL = '(?P<mainurl>https?://(www\.)?daserste\.de/[^?#]+/videos/(?P<display_id>[^/?#]+)-(?P<id>[0-9]+))\.html'
139 _TEST = {
140 'url': 'http://www.daserste.de/information/reportage-dokumentation/dokus/videos/die-story-im-ersten-mission-unter-falscher-flagge-100.html',
141 'md5': 'd216c3a86493f9322545e045ddc3eb35',
142 'info_dict': {
143 'display_id': 'die-story-im-ersten-mission-unter-falscher-flagge',
144 'id': '100',
145 'ext': 'mp4',
146 'duration': 2600,
147 'title': 'Die Story im Ersten: Mission unter falscher Flagge',
148 'upload_date': '20140804',
149 'thumbnail': 're:^https?://.*\.jpg$',
150 }
151 }
152
153 def _real_extract(self, url):
154 mobj = re.match(self._VALID_URL, url)
155 display_id = mobj.group('display_id')
156
157 player_url = mobj.group('mainurl') + '~playerXml.xml'
158 doc = self._download_xml(player_url, display_id)
159 video_node = doc.find('./video')
160 upload_date = unified_strdate(xpath_text(
161 video_node, './broadcastDate'))
162 thumbnail = xpath_text(video_node, './/teaserImage//variant/url')
163
164 formats = []
165 for a in video_node.findall('.//asset'):
166 f = {
167 'format_id': a.attrib['type'],
168 'width': int_or_none(a.find('./frameWidth').text),
169 'height': int_or_none(a.find('./frameHeight').text),
170 'vbr': int_or_none(a.find('./bitrateVideo').text),
171 'abr': int_or_none(a.find('./bitrateAudio').text),
172 'vcodec': a.find('./codecVideo').text,
173 'tbr': int_or_none(a.find('./totalBitrate').text),
174 }
175 if a.find('./serverPrefix').text:
176 f['url'] = a.find('./serverPrefix').text
177 f['playpath'] = a.find('./fileName').text
178 else:
179 f['url'] = a.find('./fileName').text
180 formats.append(f)
181 self._sort_formats(formats)
182
183 return {
184 'id': mobj.group('id'),
185 'formats': formats,
186 'display_id': display_id,
187 'title': video_node.find('./title').text,
188 'duration': parse_duration(video_node.find('./duration').text),
189 'upload_date': upload_date,
190 'thumbnail': thumbnail,
191 }