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