[ndr] use utils.qualites
[youtube-dl] / youtube_dl / extractor / ndr.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     int_or_none,
8     qualities,
9 )
10
11
12 preference = qualities(['xs', 's', 'm','l', 'xl'])
13
14
15 class NDRBaseIE(InfoExtractor):
16
17     def extract_video_info(self, playlist, video_id):
18         formats = []
19         streamType = playlist.get('config').get('streamType')
20         if streamType == 'httpVideo':
21             for key, f in playlist.items():
22                 if key != 'config':
23                     src = f['src']
24                     if '.f4m' in src:
25                         formats.extend(self._extract_f4m_formats(src, video_id))
26                     elif '.m3u8' in src:
27                         formats.extend(self._extract_m3u8_formats(src, video_id, fatal=False))
28                     else:
29                         quality = f.get('quality')
30                         formats.append({
31                             'url': src,
32                             'format_id': quality,
33                             'preference': preference(quality),
34                         })
35         elif streamType == 'httpAudio':
36             for key, f in playlist.items():
37                 if key != 'config':
38                     formats.append({
39                         'url': f['src'],
40                         'format_id': 'mp3',
41                         'vcodec': 'none',
42                     })
43         else:
44             raise ExtractorError('No media links available for %s' % video_id)
45
46         self._sort_formats(formats)
47
48         config = playlist.get('config')
49
50         title = config['title']
51         duration = int_or_none(config.get('duration'))
52         thumbnails = [{
53             'id': thumbnail.get('quality'),
54             'url': thumbnail.get('src'),
55             'preference': preference(thumbnail.get('quality'))
56         } for thumbnail in config.get('poster').values()]
57
58         return {
59             'id': video_id,
60             'title': title,
61             'thumbnails': thumbnails,
62             'duration': duration,
63             'formats': formats,
64         }
65
66     def _real_extract(self, url):
67         video_id = self._match_id(url)
68
69         json_data = self._download_json('http://www.ndr.de/%s-ppjson.json' % video_id, video_id, fatal=False)
70
71         if not json_data:
72             webpage = self._download_webpage(url, video_id)
73             embed_url = self._html_search_regex(r'<iframe[^>]+id="pp_\w+"[^>]+src="(/.*)"', webpage, 'embed url', None, False)
74             if not embed_url:
75                 embed_url = self._html_search_meta('embedURL', webpage, fatal=False)
76             if embed_url:
77                 if embed_url.startswith('/'):
78                     return self.url_result('http://www.ndr.de%s' % embed_url, 'NDREmbed')
79                 else:
80                     return self.url_result(embed_url, 'NDREmbed')
81             raise ExtractorError('No media links available for %s' % video_id)
82
83         return self.extract_video_info(json_data['playlist'], video_id)
84
85
86 class NDRIE(NDRBaseIE):
87     IE_NAME = 'ndr'
88     IE_DESC = 'NDR.de - Mediathek'
89     _VALID_URL = r'https?://www\.ndr\.de/.+?,(?P<id>\w+)\.html'
90
91     _TESTS = [
92         {
93             'url': 'http://www.ndr.de/fernsehen/sendungen/nordmagazin/Kartoffeltage-in-der-Lewitz,nordmagazin25866.html',
94             'md5': '5bc5f5b92c82c0f8b26cddca34f8bb2c',
95             'note': 'Video file',
96             'info_dict': {
97                 'id': 'nordmagazin25866',
98                 'ext': 'mp4',
99                 'title': 'Kartoffeltage in der Lewitz',
100                 'duration': 166,
101             },
102             'skip': '404 Not found',
103         },
104         {
105             'url': 'http://www.ndr.de/fernsehen/Party-Poette-und-Parade,hafengeburtstag988.html',
106             'md5': 'dadc003c55ae12a5d2f6bd436cd73f59',
107             'info_dict': {
108                 'id': 'hafengeburtstag988',
109                 'ext': 'mp4',
110                 'title': 'Party, Pötte und Parade',
111                 'duration': 3498,
112             },
113         },
114         {
115             'url': 'http://www.ndr.de/info/La-Valette-entgeht-der-Hinrichtung,audio51535.html',
116             'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
117             'note': 'Audio file',
118             'info_dict': {
119                 'id': 'audio51535',
120                 'ext': 'mp3',
121                 'title': 'La Valette entgeht der Hinrichtung',
122                 'duration': 884,
123             }
124         }
125     ]
126
127
128 class NJoyIE(NDRBaseIE):
129     IE_NAME = 'N-JOY'
130     _VALID_URL = r'https?://www\.n-joy\.de/.+?,(?P<id>\w+)\.html'
131
132     _TEST = {
133         'url': 'http://www.n-joy.de/entertainment/comedy/comedy_contest/Benaissa-beim-NDR-Comedy-Contest,comedycontest2480.html',
134         'md5': 'cb63be60cd6f9dd75218803146d8dc67',
135         'info_dict': {
136             'id': 'comedycontest2480',
137             'ext': 'mp4',
138             'title': 'Benaissa beim NDR Comedy Contest',
139             'duration': 654,
140         }
141     }
142
143
144 class NDREmbedBaseIE(NDRBaseIE):
145
146     def _real_extract(self, url):
147         video_id = self._match_id(url)
148         json_data = self._download_json('http://www.ndr.de/%s-ppjson.json' % video_id, video_id, fatal=False)
149         if not json_data:
150             raise ExtractorError('No media links available for %s' % video_id)
151         return self.extract_video_info(json_data['playlist'], video_id)
152
153
154 class NDREmbedIE(NDREmbedBaseIE):
155     IE_NAME = 'ndr:embed'
156     _VALID_URL = r'https?://www\.ndr\.de/(?:[^/]+/)+(?P<id>[a-z0-9]+)-(?:player|externalPlayer)\.html'
157
158     _TEST = {
159         'url': 'http://www.ndr.de/fernsehen/sendungen/ndr_aktuell/ndraktuell28488-player.html',
160         'md5': 'cb63be60cd6f9dd75218803146d8dc67',
161         'info_dict': {
162             'id': 'ndraktuell28488',
163             'ext': 'mp4',
164             'title': 'Norddeutschland begrüßt Flüchtlinge',
165             'duration': 132,
166         }
167     }
168
169
170 class NJoyEmbedIE(NDREmbedBaseIE):
171     IE_NAME = 'N-JOY:embed'
172     _VALID_URL = r'https?://www\.n-joy\.de/(?:[^/]+/)+(?P<id>[a-z0-9]+)-(?:player|externalPlayer)\.html'
173
174     _TEST = {
175         'url': 'http://www.n-joy.de/entertainment/film/portraet374-player_image-832d9b79-fa8a-4026-92e2-e0fd99deb2f9_theme-n-joy.html',
176         'md5': 'cb63be60cd6f9dd75218803146d8dc67',
177         'info_dict': {
178             'id': 'portraet374',
179             'ext': 'mp4',
180             'title': 'Viviane Andereggen - "Schuld um Schuld"',
181             'duration': 129,
182         }
183     }