30478f979f71be96a5419abb6a40cdda74af472b
[youtube-dl] / youtube_dl / extractor / prosiebensat1.py
1 # coding: 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                 elif mimetype == 'application/dash+xml':
89                     formats.extend(self._extract_mpd_formats(
90                         source_url, clip_id, mpd_id='dash', fatal=False))
91                 else:
92                     tbr = fix_bitrate(source['bitrate'])
93                     if protocol in ('rtmp', 'rtmpe'):
94                         mobj = re.search(r'^(?P<url>rtmpe?://[^/]+)/(?P<path>.+)$', source_url)
95                         if not mobj:
96                             continue
97                         path = mobj.group('path')
98                         mp4colon_index = path.rfind('mp4:')
99                         app = path[:mp4colon_index]
100                         play_path = path[mp4colon_index:]
101                         formats.append({
102                             'url': '%s/%s' % (mobj.group('url'), app),
103                             'app': app,
104                             'play_path': play_path,
105                             'player_url': 'http://livepassdl.conviva.com/hf/ver/2.79.0.17083/LivePassModuleMain.swf',
106                             'page_url': 'http://www.prosieben.de',
107                             'tbr': tbr,
108                             'ext': 'flv',
109                             'format_id': 'rtmp%s' % ('-%d' % tbr if tbr else ''),
110                         })
111                     else:
112                         formats.append({
113                             'url': source_url,
114                             'tbr': tbr,
115                             'format_id': 'http%s' % ('-%d' % tbr if tbr else ''),
116                         })
117         self._sort_formats(formats)
118
119         return {
120             'duration': duration,
121             'formats': formats,
122         }
123
124
125 class ProSiebenSat1IE(ProSiebenSat1BaseIE):
126     IE_NAME = 'prosiebensat1'
127     IE_DESC = 'ProSiebenSat.1 Digital'
128     _VALID_URL = r'''(?x)
129                     https?://
130                         (?:www\.)?
131                         (?:
132                             (?:
133                                 prosieben(?:maxx)?|sixx|sat1(?:gold)?|kabeleins(?:doku)?|the-voice-of-germany|7tv|advopedia
134                             )\.(?:de|at|ch)|
135                             ran\.de|fem\.com|advopedia\.de
136                         )
137                         /(?P<id>.+)
138                     '''
139
140     _TESTS = [
141         {
142             # Tests changes introduced in https://github.com/rg3/youtube-dl/pull/6242
143             # in response to fixing https://github.com/rg3/youtube-dl/issues/6215:
144             # - malformed f4m manifest support
145             # - proper handling of URLs starting with `https?://` in 2.0 manifests
146             # - recursive child f4m manifests extraction
147             'url': 'http://www.prosieben.de/tv/circus-halligalli/videos/218-staffel-2-episode-18-jahresrueckblick-ganze-folge',
148             'info_dict': {
149                 'id': '2104602',
150                 'ext': 'flv',
151                 'title': 'Episode 18 - Staffel 2',
152                 'description': 'md5:8733c81b702ea472e069bc48bb658fc1',
153                 'upload_date': '20131231',
154                 'duration': 5845.04,
155             },
156             'params': {
157                 # rtmp download
158                 'skip_download': True,
159             },
160         },
161         {
162             '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',
163             'info_dict': {
164                 'id': '2570327',
165                 'ext': 'mp4',
166                 'title': 'Lady-Umstyling für Audrina',
167                 'description': 'md5:4c16d0c17a3461a0d43ea4084e96319d',
168                 'upload_date': '20131014',
169                 'duration': 606.76,
170             },
171             'params': {
172                 # rtmp download
173                 'skip_download': True,
174             },
175             'skip': 'Seems to be broken',
176         },
177         {
178             'url': 'http://www.prosiebenmaxx.de/tv/experience/video/144-countdown-fuer-die-autowerkstatt-ganze-folge',
179             'info_dict': {
180                 'id': '2429369',
181                 'ext': 'mp4',
182                 'title': 'Countdown für die Autowerkstatt',
183                 'description': 'md5:809fc051a457b5d8666013bc40698817',
184                 'upload_date': '20140223',
185                 'duration': 2595.04,
186             },
187             'params': {
188                 # rtmp download
189                 'skip_download': True,
190             },
191             'skip': 'This video is unavailable',
192         },
193         {
194             'url': 'http://www.sixx.de/stars-style/video/sexy-laufen-in-ugg-boots-clip',
195             'info_dict': {
196                 'id': '2904997',
197                 'ext': 'mp4',
198                 'title': 'Sexy laufen in Ugg Boots',
199                 'description': 'md5:edf42b8bd5bc4e5da4db4222c5acb7d6',
200                 'upload_date': '20140122',
201                 'duration': 245.32,
202             },
203             'params': {
204                 # rtmp download
205                 'skip_download': True,
206             },
207             'skip': 'This video is unavailable',
208         },
209         {
210             'url': 'http://www.sat1.de/film/der-ruecktritt/video/im-interview-kai-wiesinger-clip',
211             'info_dict': {
212                 'id': '2906572',
213                 'ext': 'mp4',
214                 'title': 'Im Interview: Kai Wiesinger',
215                 'description': 'md5:e4e5370652ec63b95023e914190b4eb9',
216                 'upload_date': '20140203',
217                 'duration': 522.56,
218             },
219             'params': {
220                 # rtmp download
221                 'skip_download': True,
222             },
223             'skip': 'This video is unavailable',
224         },
225         {
226             'url': 'http://www.kabeleins.de/tv/rosins-restaurants/videos/jagd-auf-fertigkost-im-elsthal-teil-2-ganze-folge',
227             'info_dict': {
228                 'id': '2992323',
229                 'ext': 'mp4',
230                 'title': 'Jagd auf Fertigkost im Elsthal - Teil 2',
231                 'description': 'md5:2669cde3febe9bce13904f701e774eb6',
232                 'upload_date': '20141014',
233                 'duration': 2410.44,
234             },
235             'params': {
236                 # rtmp download
237                 'skip_download': True,
238             },
239             'skip': 'This video is unavailable',
240         },
241         {
242             'url': 'http://www.ran.de/fussball/bundesliga/video/schalke-toennies-moechte-raul-zurueck-ganze-folge',
243             'info_dict': {
244                 'id': '3004256',
245                 'ext': 'mp4',
246                 'title': 'Schalke: Tönnies möchte Raul zurück',
247                 'description': 'md5:4b5b271d9bcde223b54390754c8ece3f',
248                 'upload_date': '20140226',
249                 'duration': 228.96,
250             },
251             'params': {
252                 # rtmp download
253                 'skip_download': True,
254             },
255             'skip': 'This video is unavailable',
256         },
257         {
258             'url': 'http://www.the-voice-of-germany.de/video/31-andreas-kuemmert-rocket-man-clip',
259             'info_dict': {
260                 'id': '2572814',
261                 'ext': 'flv',
262                 'title': 'Andreas Kümmert: Rocket Man',
263                 'description': 'md5:6ddb02b0781c6adf778afea606652e38',
264                 'upload_date': '20131017',
265                 'duration': 469.88,
266             },
267             'params': {
268                 'skip_download': True,
269             },
270         },
271         {
272             'url': 'http://www.fem.com/wellness/videos/wellness-video-clip-kurztripps-zum-valentinstag.html',
273             'info_dict': {
274                 'id': '2156342',
275                 'ext': 'flv',
276                 'title': 'Kurztrips zum Valentinstag',
277                 'description': 'Romantischer Kurztrip zum Valentinstag? Nina Heinemann verrät, was sich hier wirklich lohnt.',
278                 'duration': 307.24,
279             },
280             'params': {
281                 'skip_download': True,
282             },
283         },
284         {
285             'url': 'http://www.prosieben.de/tv/joko-gegen-klaas/videos/playlists/episode-8-ganze-folge-playlist',
286             'info_dict': {
287                 'id': '439664',
288                 'title': 'Episode 8 - Ganze Folge - Playlist',
289                 'description': 'md5:63b8963e71f481782aeea877658dec84',
290             },
291             'playlist_count': 2,
292         },
293         {
294             'url': 'http://www.7tv.de/circus-halligalli/615-best-of-circus-halligalli-ganze-folge',
295             'info_dict': {
296                 'id': '4187506',
297                 'ext': 'flv',
298                 'title': 'Best of Circus HalliGalli',
299                 'description': 'md5:8849752efd90b9772c9db6fdf87fb9e9',
300                 'upload_date': '20151229',
301             },
302             'params': {
303                 'skip_download': True,
304             },
305         },
306         {
307             # geo restricted to Germany
308             'url': 'http://www.kabeleinsdoku.de/tv/mayday-alarm-im-cockpit/video/102-notlandung-im-hudson-river-ganze-folge',
309             'only_matching': True,
310         },
311         {
312             # geo restricted to Germany
313             'url': 'http://www.sat1gold.de/tv/edel-starck/video/11-staffel-1-episode-1-partner-wider-willen-ganze-folge',
314             'only_matching': True,
315         },
316         {
317             'url': 'http://www.sat1gold.de/tv/edel-starck/playlist/die-gesamte-1-staffel',
318             'only_matching': True,
319         },
320         {
321             'url': 'http://www.advopedia.de/videos/lenssen-klaert-auf/lenssen-klaert-auf-folge-8-staffel-3-feiertage-und-freie-tage',
322             'only_matching': True,
323         },
324     ]
325
326     _TOKEN = 'prosieben'
327     _SALT = '01!8d8F_)r9]4s[qeuXfP%'
328     _CLIENT_NAME = 'kolibri-2.0.19-splec4'
329     _CLIPID_REGEXES = [
330         r'"clip_id"\s*:\s+"(\d+)"',
331         r'clipid: "(\d+)"',
332         r'clip[iI]d=(\d+)',
333         r'clip[iI]d\s*=\s*["\'](\d+)',
334         r"'itemImageUrl'\s*:\s*'/dynamic/thumbnails/full/\d+/(\d+)",
335     ]
336     _TITLE_REGEXES = [
337         r'<h2 class="subtitle" itemprop="name">\s*(.+?)</h2>',
338         r'<header class="clearfix">\s*<h3>(.+?)</h3>',
339         r'<!-- start video -->\s*<h1>(.+?)</h1>',
340         r'<h1 class="att-name">\s*(.+?)</h1>',
341         r'<header class="module_header">\s*<h2>([^<]+)</h2>\s*</header>',
342         r'<h2 class="video-title" itemprop="name">\s*(.+?)</h2>',
343         r'<div[^>]+id="veeseoTitle"[^>]*>(.+?)</div>',
344     ]
345     _DESCRIPTION_REGEXES = [
346         r'<p itemprop="description">\s*(.+?)</p>',
347         r'<div class="videoDecription">\s*<p><strong>Beschreibung</strong>: (.+?)</p>',
348         r'<div class="g-plusone" data-size="medium"></div>\s*</div>\s*</header>\s*(.+?)\s*<footer>',
349         r'<p class="att-description">\s*(.+?)\s*</p>',
350         r'<p class="video-description" itemprop="description">\s*(.+?)</p>',
351         r'<div[^>]+id="veeseoDescription"[^>]*>(.+?)</div>',
352     ]
353     _UPLOAD_DATE_REGEXES = [
354         r'<meta property="og:published_time" content="(.+?)">',
355         r'<span>\s*(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}) \|\s*<span itemprop="duration"',
356         r'<footer>\s*(\d{2}\.\d{2}\.\d{4}) \d{2}:\d{2} Uhr',
357         r'<span style="padding-left: 4px;line-height:20px; color:#404040">(\d{2}\.\d{2}\.\d{4})</span>',
358         r'(\d{2}\.\d{2}\.\d{4}) \| \d{2}:\d{2} Min<br/>',
359     ]
360     _PAGE_TYPE_REGEXES = [
361         r'<meta name="page_type" content="([^"]+)">',
362         r"'itemType'\s*:\s*'([^']*)'",
363     ]
364     _PLAYLIST_ID_REGEXES = [
365         r'content[iI]d=(\d+)',
366         r"'itemId'\s*:\s*'([^']*)'",
367     ]
368     _PLAYLIST_CLIP_REGEXES = [
369         r'(?s)data-qvt=.+?<a href="([^"]+)"',
370     ]
371
372     def _extract_clip(self, url, webpage):
373         clip_id = self._html_search_regex(
374             self._CLIPID_REGEXES, webpage, 'clip id')
375         title = self._html_search_regex(self._TITLE_REGEXES, webpage, 'title')
376         info = self._extract_video_info(url, clip_id)
377         description = self._html_search_regex(
378             self._DESCRIPTION_REGEXES, webpage, 'description', fatal=False)
379         thumbnail = self._og_search_thumbnail(webpage)
380         upload_date = unified_strdate(self._html_search_regex(
381             self._UPLOAD_DATE_REGEXES, webpage, 'upload date', default=None))
382
383         info.update({
384             'id': clip_id,
385             'title': title,
386             'description': description,
387             'thumbnail': thumbnail,
388             'upload_date': upload_date,
389         })
390         return info
391
392     def _extract_playlist(self, url, webpage):
393         playlist_id = self._html_search_regex(
394             self._PLAYLIST_ID_REGEXES, webpage, 'playlist id')
395         playlist = self._parse_json(
396             self._search_regex(
397                 'var\s+contentResources\s*=\s*(\[.+?\]);\s*</script',
398                 webpage, 'playlist'),
399             playlist_id)
400         entries = []
401         for item in playlist:
402             clip_id = item.get('id') or item.get('upc')
403             if not clip_id:
404                 continue
405             info = self._extract_video_info(url, clip_id)
406             info.update({
407                 'id': clip_id,
408                 'title': item.get('title') or item.get('teaser', {}).get('headline'),
409                 'description': item.get('teaser', {}).get('description'),
410                 'thumbnail': item.get('poster'),
411                 'duration': float_or_none(item.get('duration')),
412                 'series': item.get('tvShowTitle'),
413                 'uploader': item.get('broadcastPublisher'),
414             })
415             entries.append(info)
416         return self.playlist_result(entries, playlist_id)
417
418     def _real_extract(self, url):
419         video_id = self._match_id(url)
420         webpage = self._download_webpage(url, video_id)
421         page_type = self._search_regex(
422             self._PAGE_TYPE_REGEXES, webpage,
423             'page type', default='clip').lower()
424         if page_type == 'clip':
425             return self._extract_clip(url, webpage)
426         elif page_type == 'playlist':
427             return self._extract_playlist(url, webpage)