]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/ndr.py
[ndr] Rework and cover with tests
[yt-dlp.git] / youtube_dl / extractor / ndr.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 int_or_none,
10 parse_iso8601,
11 qualities,
12 )
13
14
15 class NDRBaseIE(InfoExtractor):
16 def _real_extract(self, url):
17 display_id = self._match_id(url)
18 webpage = self._download_webpage(url, display_id)
19 return self._extract_embed(webpage, display_id)
20
21
22 class NDRIE(NDRBaseIE):
23 IE_NAME = 'ndr'
24 IE_DESC = 'NDR.de - Norddeutscher Rundfunk'
25 _VALID_URL = r'https?://www\.ndr\.de/(?:[^/]+/)+(?P<id>[^/?#]+),[\da-z]+\.html'
26 _TESTS = [{
27 # httpVideo, same content id
28 'url': 'http://www.ndr.de/fernsehen/Party-Poette-und-Parade,hafengeburtstag988.html',
29 'md5': '6515bc255dc5c5f8c85bbc38e035a659',
30 'info_dict': {
31 'id': 'hafengeburtstag988',
32 'display_id': 'Party-Poette-und-Parade',
33 'ext': 'mp4',
34 'title': 'Party, Pötte und Parade',
35 'description': 'md5:ad14f9d2f91d3040b6930c697e5f6b4c',
36 'uploader': 'ndrtv',
37 'timestamp': 1431108900,
38 'upload_date': '20150510',
39 'duration': 3498,
40 },
41 'params': {
42 'skip_download': True,
43 },
44 }, {
45 # httpVideo, different content id
46 'url': 'http://www.ndr.de/sport/fussball/40-Osnabrueck-spielt-sich-in-einen-Rausch,osna270.html',
47 'md5': '1043ff203eab307f0c51702ec49e9a71',
48 'info_dict': {
49 'id': 'osna272',
50 'display_id': '40-Osnabrueck-spielt-sich-in-einen-Rausch',
51 'ext': 'mp4',
52 'title': 'Osnabrück - Wehen Wiesbaden: Die Highlights',
53 'description': 'md5:32e9b800b3d2d4008103752682d5dc01',
54 'uploader': 'ndrtv',
55 'timestamp': 1442059200,
56 'upload_date': '20150912',
57 'duration': 510,
58 },
59 'params': {
60 'skip_download': True,
61 },
62 }, {
63 # httpAudio, same content id
64 'url': 'http://www.ndr.de/info/La-Valette-entgeht-der-Hinrichtung,audio51535.html',
65 'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
66 'info_dict': {
67 'id': 'audio51535',
68 'display_id': 'La-Valette-entgeht-der-Hinrichtung',
69 'ext': 'mp3',
70 'title': 'La Valette entgeht der Hinrichtung',
71 'description': 'md5:22f9541913a40fe50091d5cdd7c9f536',
72 'uploader': 'ndrinfo',
73 'timestamp': 1290626100,
74 'upload_date': '20140729',
75 'duration': 884,
76 },
77 'params': {
78 'skip_download': True,
79 },
80 }]
81
82 def _extract_embed(self, webpage, display_id):
83 embed_url = self._html_search_meta(
84 'embedURL', webpage, 'embed URL', fatal=True)
85 description = self._search_regex(
86 r'<p[^>]+itemprop="description">([^<]+)</p>',
87 webpage, 'description', fatal=False)
88 timestamp = parse_iso8601(
89 self._search_regex(
90 r'<span itemprop="datePublished" content="([^"]+)">',
91 webpage, 'upload date', fatal=False))
92 return {
93 '_type': 'url_transparent',
94 'url': embed_url,
95 'display_id': display_id,
96 'description': description,
97 'timestamp': timestamp,
98 }
99
100
101 class NJoyIE(NDRBaseIE):
102 IE_NAME = 'njoy'
103 IE_DESC = 'N-JOY'
104 _VALID_URL = r'https?://www\.n-joy\.de/(?:[^/]+/)+(?P<id>[^/?#]+),[\da-z]+\.html'
105 _TESTS = [{
106 # httpVideo, same content id
107 'url': 'http://www.n-joy.de/entertainment/comedy/comedy_contest/Benaissa-beim-NDR-Comedy-Contest,comedycontest2480.html',
108 'md5': 'cb63be60cd6f9dd75218803146d8dc67',
109 'info_dict': {
110 'id': 'comedycontest2480',
111 'display_id': 'Benaissa-beim-NDR-Comedy-Contest',
112 'ext': 'mp4',
113 'title': 'Benaissa beim NDR Comedy Contest',
114 'description': 'md5:f057a6c4e1c728b10d33b5ffd36ddc39',
115 'uploader': 'ndrtv',
116 'upload_date': '20141129',
117 'duration': 654,
118 },
119 'params': {
120 'skip_download': True,
121 },
122 }, {
123 # httpVideo, different content id
124 'url': 'http://www.n-joy.de/musik/Das-frueheste-DJ-Set-des-Nordens-live-mit-Felix-Jaehn-,felixjaehn168.html',
125 'md5': '417660fffa90e6df2fda19f1b40a64d8',
126 'info_dict': {
127 'id': 'dockville882',
128 'display_id': 'Das-frueheste-DJ-Set-des-Nordens-live-mit-Felix-Jaehn-',
129 'ext': 'mp4',
130 'title': '"Ich hab noch nie" mit Felix Jaehn',
131 'description': 'md5:85dd312d53be1b99e1f998a16452a2f3',
132 'uploader': 'njoy',
133 'upload_date': '20150822',
134 'duration': 211,
135 },
136 'params': {
137 'skip_download': True,
138 },
139 }]
140
141 def _extract_embed(self, webpage, display_id):
142 video_id = self._search_regex(
143 r'<iframe[^>]+id="pp_([\da-z]+)"', webpage, 'embed id')
144 description = self._search_regex(
145 r'<div[^>]+class="subline"[^>]*>[^<]+</div>\s*<p>([^<]+)</p>',
146 webpage, 'description', fatal=False)
147 return {
148 '_type': 'url_transparent',
149 'ie_key': 'NDREmbedBase',
150 'url': 'ndr:%s' % video_id,
151 'display_id': display_id,
152 'description': description,
153 }
154
155
156 class NDREmbedBaseIE(InfoExtractor):
157 IE_NAME = 'ndr:embed:base'
158 _VALID_URL = r'(?:ndr:(?P<id_s>[\da-z]+)|https?://www\.ndr\.de/(?P<id>[\da-z]+)-ppjson\.json)'
159 _TESTS = [{
160 'url': 'ndr:soundcheck3366',
161 'only_matching': True,
162 }, {
163 'url': 'http://www.ndr.de/soundcheck3366-ppjson.json',
164 'only_matching': True,
165 }]
166
167 def _real_extract(self, url):
168 mobj = re.match(self._VALID_URL, url)
169 video_id = mobj.group('id') or mobj.group('id_s')
170
171 ppjson = self._download_json(
172 'http://www.ndr.de/%s-ppjson.json' % video_id, video_id)
173
174 playlist = ppjson['playlist']
175
176 formats = []
177 quality_key = qualities(('xs', 's', 'm', 'l', 'xl'))
178
179 for format_id, f in playlist.items():
180 src = f.get('src')
181 if not src:
182 continue
183 ext = determine_ext(src, None)
184 if ext == 'f4m':
185 formats.extend(self._extract_f4m_formats(
186 src + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id, f4m_id='hds'))
187 elif ext == 'm3u8':
188 formats.extend(self._extract_m3u8_formats(
189 src, video_id, m3u8_id='hls', entry_protocol='m3u8_native'))
190 else:
191 quality = f.get('quality')
192 ff = {
193 'url': src,
194 'format_id': quality or format_id,
195 'quality': quality_key(quality),
196 }
197 type_ = f.get('type')
198 if type_ and type_.split('/')[0] == 'audio':
199 ff['vcodec'] = 'none'
200 ff['ext'] = ext or 'mp3'
201 formats.append(ff)
202 self._sort_formats(formats)
203
204 config = playlist['config']
205
206 live = playlist.get('config', {}).get('streamType') in ['httpVideoLive', 'httpAudioLive']
207 title = config['title']
208 if live:
209 title = self._live_title(title)
210 uploader = ppjson.get('config', {}).get('branding')
211 upload_date = ppjson.get('config', {}).get('publicationDate')
212 duration = int_or_none(config.get('duration'))
213
214 thumbnails = [{
215 'id': thumbnail.get('quality') or thumbnail_id,
216 'url': thumbnail['src'],
217 'preference': quality_key(thumbnail.get('quality')),
218 } for thumbnail_id, thumbnail in config.get('poster', {}).items() if thumbnail.get('src')]
219
220 return {
221 'id': video_id,
222 'title': title,
223 'is_live': live,
224 'uploader': uploader if uploader != '-' else None,
225 'upload_date': upload_date[0:8] if upload_date else None,
226 'duration': duration,
227 'thumbnails': thumbnails,
228 'formats': formats,
229 }
230
231
232 class NDREmbedIE(NDREmbedBaseIE):
233 IE_NAME = 'ndr:embed'
234 _VALID_URL = r'https?://www\.ndr\.de/(?:[^/]+/)+(?P<id>[\da-z]+)-(?:player|externalPlayer)\.html'
235 _TESTS = [{
236 'url': 'http://www.ndr.de/fernsehen/sendungen/ndr_aktuell/ndraktuell28488-player.html',
237 'md5': '8b9306142fe65bbdefb5ce24edb6b0a9',
238 'info_dict': {
239 'id': 'ndraktuell28488',
240 'ext': 'mp4',
241 'title': 'Norddeutschland begrüßt Flüchtlinge',
242 'is_live': False,
243 'uploader': 'ndrtv',
244 'upload_date': '20150907',
245 'duration': 132,
246 },
247 }, {
248 'url': 'http://www.ndr.de/ndr2/events/soundcheck/soundcheck3366-player.html',
249 'md5': '002085c44bae38802d94ae5802a36e78',
250 'info_dict': {
251 'id': 'soundcheck3366',
252 'ext': 'mp4',
253 'title': 'Ella Henderson braucht Vergleiche nicht zu scheuen',
254 'is_live': False,
255 'uploader': 'ndr2',
256 'upload_date': '20150912',
257 'duration': 3554,
258 },
259 'params': {
260 'skip_download': True,
261 },
262 }, {
263 'url': 'http://www.ndr.de/info/audio51535-player.html',
264 'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
265 'info_dict': {
266 'id': 'audio51535',
267 'ext': 'mp3',
268 'title': 'La Valette entgeht der Hinrichtung',
269 'is_live': False,
270 'uploader': 'ndrinfo',
271 'upload_date': '20140729',
272 'duration': 884,
273 },
274 'params': {
275 'skip_download': True,
276 },
277 }, {
278 'url': 'http://www.ndr.de/fernsehen/sendungen/visite/visite11010-externalPlayer.html',
279 'md5': 'ae57f80511c1e1f2fd0d0d3d31aeae7c',
280 'info_dict': {
281 'id': 'visite11010',
282 'ext': 'mp4',
283 'title': 'Visite - die ganze Sendung',
284 'is_live': False,
285 'uploader': 'ndrtv',
286 'upload_date': '20150902',
287 'duration': 3525,
288 },
289 'params': {
290 'skip_download': True,
291 },
292 }, {
293 # httpVideoLive
294 'url': 'http://www.ndr.de/fernsehen/livestream/livestream217-externalPlayer.html',
295 'info_dict': {
296 'id': 'livestream217',
297 'ext': 'flv',
298 'title': 're:^NDR Fernsehen Niedersachsen \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
299 'is_live': True,
300 'upload_date': '20150910',
301 },
302 'params': {
303 'skip_download': True,
304 },
305 }, {
306 'url': 'http://www.ndr.de/ndrkultur/audio255020-player.html',
307 'only_matching': True,
308 }, {
309 'url': 'http://www.ndr.de/fernsehen/sendungen/nordtour/nordtour7124-player.html',
310 'only_matching': True,
311 }, {
312 'url': 'http://www.ndr.de/kultur/film/videos/videoimport10424-player.html',
313 'only_matching': True,
314 }, {
315 'url': 'http://www.ndr.de/fernsehen/sendungen/hamburg_journal/hamj43006-player.html',
316 'only_matching': True,
317 }, {
318 'url': 'http://www.ndr.de/fernsehen/sendungen/weltbilder/weltbilder4518-player.html',
319 'only_matching': True,
320 }, {
321 'url': 'http://www.ndr.de/fernsehen/doku952-player.html',
322 'only_matching': True,
323 }]
324
325
326 class NJoyEmbedIE(NDREmbedBaseIE):
327 IE_NAME = 'njoy:embed'
328 _VALID_URL = r'https?://www\.n-joy\.de/(?:[^/]+/)+(?P<id>[\da-z]+)-(?:player|externalPlayer)_[^/]+\.html'
329 _TESTS = [{
330 # httpVideo
331 'url': 'http://www.n-joy.de/events/reeperbahnfestival/doku948-player_image-bc168e87-5263-4d6d-bd27-bb643005a6de_theme-n-joy.html',
332 'md5': '8483cbfe2320bd4d28a349d62d88bd74',
333 'info_dict': {
334 'id': 'doku948',
335 'ext': 'mp4',
336 'title': 'Zehn Jahre Reeperbahn Festival - die Doku',
337 'is_live': False,
338 'upload_date': '20150807',
339 'duration': 1011,
340 },
341 }, {
342 # httpAudio
343 'url': 'http://www.n-joy.de/news_wissen/stefanrichter100-player_image-d5e938b1-f21a-4b9a-86b8-aaba8bca3a13_theme-n-joy.html',
344 'md5': 'd989f80f28ac954430f7b8a48197188a',
345 'info_dict': {
346 'id': 'stefanrichter100',
347 'ext': 'mp3',
348 'title': 'Interview mit einem Augenzeugen',
349 'is_live': False,
350 'uploader': 'njoy',
351 'upload_date': '20150909',
352 'duration': 140,
353 },
354 'params': {
355 'skip_download': True,
356 },
357 }, {
358 # httpAudioLive, no explicit ext
359 'url': 'http://www.n-joy.de/news_wissen/webradioweltweit100-player_image-3fec0484-2244-4565-8fb8-ed25fd28b173_theme-n-joy.html',
360 'info_dict': {
361 'id': 'webradioweltweit100',
362 'ext': 'mp3',
363 'title': 're:^N-JOY Weltweit \d{4}-\d{2}-\d{2} \d{2}:\d{2}$',
364 'is_live': True,
365 'uploader': 'njoy',
366 'upload_date': '20150810',
367 },
368 'params': {
369 'skip_download': True,
370 },
371 }, {
372 'url': 'http://www.n-joy.de/musik/dockville882-player_image-3905259e-0803-4764-ac72-8b7de077d80a_theme-n-joy.html',
373 'only_matching': True,
374 }, {
375 'url': 'http://www.n-joy.de/radio/sendungen/morningshow/urlaubsfotos190-player_image-066a5df1-5c95-49ec-a323-941d848718db_theme-n-joy.html',
376 'only_matching': True,
377 }, {
378 'url': 'http://www.n-joy.de/entertainment/comedy/krudetv290-player_image-ab261bfe-51bf-4bf3-87ba-c5122ee35b3d_theme-n-joy.html',
379 'only_matching': True,
380 }]