Merge branch 'polskie-radio-programme' of https://github.com/JakubAdamWieczorek/youtu...
[youtube-dl] / youtube_dl / extractor / prosiebensat1.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from hashlib import sha1
7 from .common import InfoExtractor
8 from ..compat import compat_str
9 from ..utils import (
10     ExtractorError,
11     determine_ext,
12     float_or_none,
13     int_or_none,
14     unified_strdate,
15 )
16
17
18 class ProSiebenSat1BaseIE(InfoExtractor):
19     def _extract_video_info(self, url, clip_id):
20         client_location = url
21
22         video = self._download_json(
23             'http://vas.sim-technik.de/vas/live/v2/videos',
24             clip_id, 'Downloading videos JSON', query={
25                 'access_token': self._TOKEN,
26                 'client_location': client_location,
27                 'client_name': self._CLIENT_NAME,
28                 'ids': clip_id,
29             })[0]
30
31         if video.get('is_protected') is True:
32             raise ExtractorError('This video is DRM protected.', expected=True)
33
34         duration = float_or_none(video.get('duration'))
35         source_ids = [compat_str(source['id']) for source in video['sources']]
36
37         client_id = self._SALT[:2] + sha1(''.join([clip_id, self._SALT, self._TOKEN, client_location, self._SALT, self._CLIENT_NAME]).encode('utf-8')).hexdigest()
38
39         sources = self._download_json(
40             'http://vas.sim-technik.de/vas/live/v2/videos/%s/sources' % clip_id,
41             clip_id, 'Downloading sources JSON', query={
42                 'access_token': self._TOKEN,
43                 'client_id': client_id,
44                 'client_location': client_location,
45                 'client_name': self._CLIENT_NAME,
46             })
47         server_id = sources['server_id']
48
49         def fix_bitrate(bitrate):
50             bitrate = int_or_none(bitrate)
51             if not bitrate:
52                 return None
53             return (bitrate // 1000) if bitrate % 1000 == 0 else bitrate
54
55         formats = []
56         for source_id in source_ids:
57             client_id = self._SALT[:2] + sha1(''.join([self._SALT, clip_id, self._TOKEN, server_id, client_location, source_id, self._SALT, self._CLIENT_NAME]).encode('utf-8')).hexdigest()
58             urls = self._download_json(
59                 'http://vas.sim-technik.de/vas/live/v2/videos/%s/sources/url' % clip_id,
60                 clip_id, 'Downloading urls JSON', fatal=False, query={
61                     'access_token': self._TOKEN,
62                     'client_id': client_id,
63                     'client_location': client_location,
64                     'client_name': self._CLIENT_NAME,
65                     'server_id': server_id,
66                     'source_ids': source_id,
67                 })
68             if not urls:
69                 continue
70             if urls.get('status_code') != 0:
71                 raise ExtractorError('This video is unavailable', expected=True)
72             urls_sources = urls['sources']
73             if isinstance(urls_sources, dict):
74                 urls_sources = urls_sources.values()
75             for source in urls_sources:
76                 source_url = source.get('url')
77                 if not source_url:
78                     continue
79                 protocol = source.get('protocol')
80                 mimetype = source.get('mimetype')
81                 if mimetype == 'application/f4m+xml' or 'f4mgenerator' in source_url or determine_ext(source_url) == 'f4m':
82                     formats.extend(self._extract_f4m_formats(
83                         source_url, clip_id, f4m_id='hds', fatal=False))
84                 elif mimetype == 'application/x-mpegURL':
85                     formats.extend(self._extract_m3u8_formats(
86                         source_url, clip_id, 'mp4', 'm3u8_native',
87                         m3u8_id='hls', fatal=False))
88                 else:
89                     tbr = fix_bitrate(source['bitrate'])
90                     if protocol in ('rtmp', 'rtmpe'):
91                         mobj = re.search(r'^(?P<url>rtmpe?://[^/]+)/(?P<path>.+)$', source_url)
92                         if not mobj:
93                             continue
94                         path = mobj.group('path')
95                         mp4colon_index = path.rfind('mp4:')
96                         app = path[:mp4colon_index]
97                         play_path = path[mp4colon_index:]
98                         formats.append({
99                             'url': '%s/%s' % (mobj.group('url'), app),
100                             'app': app,
101                             'play_path': play_path,
102                             'player_url': 'http://livepassdl.conviva.com/hf/ver/2.79.0.17083/LivePassModuleMain.swf',
103                             'page_url': 'http://www.prosieben.de',
104                             'tbr': tbr,
105                             'ext': 'flv',
106                             'format_id': 'rtmp%s' % ('-%d' % tbr if tbr else ''),
107                         })
108                     else:
109                         formats.append({
110                             'url': source_url,
111                             'tbr': tbr,
112                             'format_id': 'http%s' % ('-%d' % tbr if tbr else ''),
113                         })
114         self._sort_formats(formats)
115
116         return {
117             'duration': duration,
118             'formats': formats,
119         }
120
121
122 class ProSiebenSat1IE(ProSiebenSat1BaseIE):
123     IE_NAME = 'prosiebensat1'
124     IE_DESC = 'ProSiebenSat.1 Digital'
125     _VALID_URL = r'https?://(?:www\.)?(?:(?:prosieben|prosiebenmaxx|sixx|sat1|kabeleins|the-voice-of-germany|7tv)\.(?:de|at|ch)|ran\.de|fem\.com)/(?P<id>.+)'
126
127     _TESTS = [
128         {
129             # Tests changes introduced in https://github.com/rg3/youtube-dl/pull/6242
130             # in response to fixing https://github.com/rg3/youtube-dl/issues/6215:
131             # - malformed f4m manifest support
132             # - proper handling of URLs starting with `https?://` in 2.0 manifests
133             # - recursive child f4m manifests extraction
134             'url': 'http://www.prosieben.de/tv/circus-halligalli/videos/218-staffel-2-episode-18-jahresrueckblick-ganze-folge',
135             'info_dict': {
136                 'id': '2104602',
137                 'ext': 'flv',
138                 'title': 'Episode 18 - Staffel 2',
139                 'description': 'md5:8733c81b702ea472e069bc48bb658fc1',
140                 'upload_date': '20131231',
141                 'duration': 5845.04,
142             },
143             'params': {
144                 # rtmp download
145                 'skip_download': True,
146             },
147         },
148         {
149             'url': 'http://www.prosieben.de/videokatalog/Gesellschaft/Leben/Trends/video-Lady-Umstyling-f%C3%BCr-Audrina-Rebekka-Audrina-Fergen-billig-aussehen-Battal-Modica-700544.html',
150             'info_dict': {
151                 'id': '2570327',
152                 'ext': 'mp4',
153                 'title': 'Lady-Umstyling für Audrina',
154                 'description': 'md5:4c16d0c17a3461a0d43ea4084e96319d',
155                 'upload_date': '20131014',
156                 'duration': 606.76,
157             },
158             'params': {
159                 # rtmp download
160                 'skip_download': True,
161             },
162             'skip': 'Seems to be broken',
163         },
164         {
165             'url': 'http://www.prosiebenmaxx.de/tv/experience/video/144-countdown-fuer-die-autowerkstatt-ganze-folge',
166             'info_dict': {
167                 'id': '2429369',
168                 'ext': 'mp4',
169                 'title': 'Countdown für die Autowerkstatt',
170                 'description': 'md5:809fc051a457b5d8666013bc40698817',
171                 'upload_date': '20140223',
172                 'duration': 2595.04,
173             },
174             'params': {
175                 # rtmp download
176                 'skip_download': True,
177             },
178             'skip': 'This video is unavailable',
179         },
180         {
181             'url': 'http://www.sixx.de/stars-style/video/sexy-laufen-in-ugg-boots-clip',
182             'info_dict': {
183                 'id': '2904997',
184                 'ext': 'mp4',
185                 'title': 'Sexy laufen in Ugg Boots',
186                 'description': 'md5:edf42b8bd5bc4e5da4db4222c5acb7d6',
187                 'upload_date': '20140122',
188                 'duration': 245.32,
189             },
190             'params': {
191                 # rtmp download
192                 'skip_download': True,
193             },
194             'skip': 'This video is unavailable',
195         },
196         {
197             'url': 'http://www.sat1.de/film/der-ruecktritt/video/im-interview-kai-wiesinger-clip',
198             'info_dict': {
199                 'id': '2906572',
200                 'ext': 'mp4',
201                 'title': 'Im Interview: Kai Wiesinger',
202                 'description': 'md5:e4e5370652ec63b95023e914190b4eb9',
203                 'upload_date': '20140203',
204                 'duration': 522.56,
205             },
206             'params': {
207                 # rtmp download
208                 'skip_download': True,
209             },
210             'skip': 'This video is unavailable',
211         },
212         {
213             'url': 'http://www.kabeleins.de/tv/rosins-restaurants/videos/jagd-auf-fertigkost-im-elsthal-teil-2-ganze-folge',
214             'info_dict': {
215                 'id': '2992323',
216                 'ext': 'mp4',
217                 'title': 'Jagd auf Fertigkost im Elsthal - Teil 2',
218                 'description': 'md5:2669cde3febe9bce13904f701e774eb6',
219                 'upload_date': '20141014',
220                 'duration': 2410.44,
221             },
222             'params': {
223                 # rtmp download
224                 'skip_download': True,
225             },
226             'skip': 'This video is unavailable',
227         },
228         {
229             'url': 'http://www.ran.de/fussball/bundesliga/video/schalke-toennies-moechte-raul-zurueck-ganze-folge',
230             'info_dict': {
231                 'id': '3004256',
232                 'ext': 'mp4',
233                 'title': 'Schalke: Tönnies möchte Raul zurück',
234                 'description': 'md5:4b5b271d9bcde223b54390754c8ece3f',
235                 'upload_date': '20140226',
236                 'duration': 228.96,
237             },
238             'params': {
239                 # rtmp download
240                 'skip_download': True,
241             },
242             'skip': 'This video is unavailable',
243         },
244         {
245             'url': 'http://www.the-voice-of-germany.de/video/31-andreas-kuemmert-rocket-man-clip',
246             'info_dict': {
247                 'id': '2572814',
248                 'ext': 'flv',
249                 'title': 'Andreas Kümmert: Rocket Man',
250                 'description': 'md5:6ddb02b0781c6adf778afea606652e38',
251                 'upload_date': '20131017',
252                 'duration': 469.88,
253             },
254             'params': {
255                 'skip_download': True,
256             },
257         },
258         {
259             'url': 'http://www.fem.com/wellness/videos/wellness-video-clip-kurztripps-zum-valentinstag.html',
260             'info_dict': {
261                 'id': '2156342',
262                 'ext': 'flv',
263                 'title': 'Kurztrips zum Valentinstag',
264                 'description': 'Romantischer Kurztrip zum Valentinstag? Nina Heinemann verrät, was sich hier wirklich lohnt.',
265                 'duration': 307.24,
266             },
267             'params': {
268                 'skip_download': True,
269             },
270         },
271         {
272             'url': 'http://www.prosieben.de/tv/joko-gegen-klaas/videos/playlists/episode-8-ganze-folge-playlist',
273             'info_dict': {
274                 'id': '439664',
275                 'title': 'Episode 8 - Ganze Folge - Playlist',
276                 'description': 'md5:63b8963e71f481782aeea877658dec84',
277             },
278             'playlist_count': 2,
279         },
280         {
281             'url': 'http://www.7tv.de/circus-halligalli/615-best-of-circus-halligalli-ganze-folge',
282             'info_dict': {
283                 'id': '4187506',
284                 'ext': 'flv',
285                 'title': 'Best of Circus HalliGalli',
286                 'description': 'md5:8849752efd90b9772c9db6fdf87fb9e9',
287                 'upload_date': '20151229',
288             },
289             'params': {
290                 'skip_download': True,
291             },
292         },
293     ]
294
295     _TOKEN = 'prosieben'
296     _SALT = '01!8d8F_)r9]4s[qeuXfP%'
297     _CLIENT_NAME = 'kolibri-2.0.19-splec4'
298     _CLIPID_REGEXES = [
299         r'"clip_id"\s*:\s+"(\d+)"',
300         r'clipid: "(\d+)"',
301         r'clip[iI]d=(\d+)',
302         r'clip[iI]d\s*=\s*["\'](\d+)',
303         r"'itemImageUrl'\s*:\s*'/dynamic/thumbnails/full/\d+/(\d+)",
304     ]
305     _TITLE_REGEXES = [
306         r'<h2 class="subtitle" itemprop="name">\s*(.+?)</h2>',
307         r'<header class="clearfix">\s*<h3>(.+?)</h3>',
308         r'<!-- start video -->\s*<h1>(.+?)</h1>',
309         r'<h1 class="att-name">\s*(.+?)</h1>',
310         r'<header class="module_header">\s*<h2>([^<]+)</h2>\s*</header>',
311         r'<h2 class="video-title" itemprop="name">\s*(.+?)</h2>',
312         r'<div[^>]+id="veeseoTitle"[^>]*>(.+?)</div>',
313     ]
314     _DESCRIPTION_REGEXES = [
315         r'<p itemprop="description">\s*(.+?)</p>',
316         r'<div class="videoDecription">\s*<p><strong>Beschreibung</strong>: (.+?)</p>',
317         r'<div class="g-plusone" data-size="medium"></div>\s*</div>\s*</header>\s*(.+?)\s*<footer>',
318         r'<p class="att-description">\s*(.+?)\s*</p>',
319         r'<p class="video-description" itemprop="description">\s*(.+?)</p>',
320         r'<div[^>]+id="veeseoDescription"[^>]*>(.+?)</div>',
321     ]
322     _UPLOAD_DATE_REGEXES = [
323         r'<meta property="og:published_time" content="(.+?)">',
324         r'<span>\s*(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}) \|\s*<span itemprop="duration"',
325         r'<footer>\s*(\d{2}\.\d{2}\.\d{4}) \d{2}:\d{2} Uhr',
326         r'<span style="padding-left: 4px;line-height:20px; color:#404040">(\d{2}\.\d{2}\.\d{4})</span>',
327         r'(\d{2}\.\d{2}\.\d{4}) \| \d{2}:\d{2} Min<br/>',
328     ]
329     _PAGE_TYPE_REGEXES = [
330         r'<meta name="page_type" content="([^"]+)">',
331         r"'itemType'\s*:\s*'([^']*)'",
332     ]
333     _PLAYLIST_ID_REGEXES = [
334         r'content[iI]d=(\d+)',
335         r"'itemId'\s*:\s*'([^']*)'",
336     ]
337     _PLAYLIST_CLIP_REGEXES = [
338         r'(?s)data-qvt=.+?<a href="([^"]+)"',
339     ]
340
341     def _extract_clip(self, url, webpage):
342         clip_id = self._html_search_regex(
343             self._CLIPID_REGEXES, webpage, 'clip id')
344         title = self._html_search_regex(self._TITLE_REGEXES, webpage, 'title')
345         info = self._extract_video_info(url, clip_id)
346         description = self._html_search_regex(
347             self._DESCRIPTION_REGEXES, webpage, 'description', fatal=False)
348         thumbnail = self._og_search_thumbnail(webpage)
349         upload_date = unified_strdate(self._html_search_regex(
350             self._UPLOAD_DATE_REGEXES, webpage, 'upload date', default=None))
351
352         info.update({
353             'id': clip_id,
354             'title': title,
355             'description': description,
356             'thumbnail': thumbnail,
357             'upload_date': upload_date,
358         })
359         return info
360
361     def _extract_playlist(self, url, webpage):
362         playlist_id = self._html_search_regex(
363             self._PLAYLIST_ID_REGEXES, webpage, 'playlist id')
364         for regex in self._PLAYLIST_CLIP_REGEXES:
365             playlist_clips = re.findall(regex, webpage)
366             if playlist_clips:
367                 title = self._html_search_regex(
368                     self._TITLE_REGEXES, webpage, 'title')
369                 description = self._html_search_regex(
370                     self._DESCRIPTION_REGEXES, webpage, 'description', fatal=False)
371                 entries = [
372                     self.url_result(
373                         re.match('(.+?//.+?)/', url).group(1) + clip_path,
374                         'ProSiebenSat1')
375                     for clip_path in playlist_clips]
376                 return self.playlist_result(entries, playlist_id, title, description)
377
378     def _real_extract(self, url):
379         video_id = self._match_id(url)
380         webpage = self._download_webpage(url, video_id)
381         page_type = self._search_regex(
382             self._PAGE_TYPE_REGEXES, webpage,
383             'page type', default='clip').lower()
384         if page_type == 'clip':
385             return self._extract_clip(url, webpage)
386         elif page_type == 'playlist':
387             return self._extract_playlist(url, webpage)