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