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