[prosiebensat1] Fix broken tests
[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': '20140203',
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': '20141014',
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         r"'itemImageUrl'\s*:\s*'/dynamic/thumbnails/full/\d+/(\d+)",
172     ]
173     _TITLE_REGEXES = [
174         r'<h2 class="subtitle" itemprop="name">\s*(.+?)</h2>',
175         r'<header class="clearfix">\s*<h3>(.+?)</h3>',
176         r'<!-- start video -->\s*<h1>(.+?)</h1>',
177         r'<h1 class="att-name">\s*(.+?)</h1>',
178     ]
179     _DESCRIPTION_REGEXES = [
180         r'<p itemprop="description">\s*(.+?)</p>',
181         r'<div class="videoDecription">\s*<p><strong>Beschreibung</strong>: (.+?)</p>',
182         r'<div class="g-plusone" data-size="medium"></div>\s*</div>\s*</header>\s*(.+?)\s*<footer>',
183         r'<p class="att-description">\s*(.+?)\s*</p>',
184     ]
185     _UPLOAD_DATE_REGEXES = [
186         r'<meta property="og:published_time" content="(.+?)">',
187         r'<span>\s*(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}) \|\s*<span itemprop="duration"',
188         r'<footer>\s*(\d{2}\.\d{2}\.\d{4}) \d{2}:\d{2} Uhr',
189         r'<span style="padding-left: 4px;line-height:20px; color:#404040">(\d{2}\.\d{2}\.\d{4})</span>',
190         r'(\d{2}\.\d{2}\.\d{4}) \| \d{2}:\d{2} Min<br/>',
191     ]
192     _ITEM_TYPE_REGEXES = [
193         r"'itemType'\s*:\s*'([^']*)'",
194     ]
195     _ITEM_ID_REGEXES = [
196         r"'itemId'\s*:\s*'([^']*)'",
197     ]
198     _PLAYLIST_CLIPS_REGEXES = [
199         r'data-qvt=.+?<a href="([^"]+)"',
200     ]
201
202     def _real_extract(self, url):
203         video_id = self._match_id(url)
204         webpage = self._download_webpage(url, video_id)
205
206         item_type = self._html_search_regex(self._ITEM_TYPE_REGEXES, webpage, 'item type', default='CLIP')
207         if item_type == 'CLIP':
208             return self._clip_extract(url, webpage)
209         elif item_type == 'PLAYLIST':
210             playlist_id = self._html_search_regex(self._ITEM_ID_REGEXES, webpage, 'playlist id')
211
212             for regex in self._PLAYLIST_CLIPS_REGEXES:
213                 playlist_clips = re.findall(regex, webpage, re.DOTALL)
214                 if playlist_clips:
215                     title = self._html_search_regex(self._TITLE_REGEXES, webpage, 'title')
216                     description = self._html_search_regex(self._DESCRIPTION_REGEXES, webpage, 'description', fatal=False)
217                     root_url = re.match('(.+?//.+?)/', url).group(1)
218
219                     return {
220                         '_type': 'playlist',
221                         'id': playlist_id,
222                         'title': title,
223                         'description': description,
224                         'entries': [self._clip_extract(root_url + clip_path) for clip_path in playlist_clips]
225                     }
226         else:
227             raise ExtractorError('Unknown item type "%s"' % item_type)
228
229     def _clip_extract(self, url, webpage=None):
230         if webpage is None:
231             video_id = self._match_id(url)
232             webpage = self._download_webpage(url, video_id)
233
234         clip_id = self._html_search_regex(self._CLIPID_REGEXES, webpage, 'clip id')
235
236         access_token = 'testclient'
237         client_name = 'kolibri-1.2.5'
238         client_location = url
239
240         videos_api_url = 'http://vas.sim-technik.de/vas/live/v2/videos?%s' % compat_urllib_parse.urlencode({
241             'access_token': access_token,
242             'client_location': client_location,
243             'client_name': client_name,
244             'ids': clip_id,
245         })
246
247         videos = self._download_json(videos_api_url, clip_id, 'Downloading videos JSON')
248
249         duration = float(videos[0]['duration'])
250         source_ids = [source['id'] for source in videos[0]['sources']]
251         source_ids_str = ','.join(map(str, source_ids))
252
253         g = '01!8d8F_)r9]4s[qeuXfP%'
254
255         client_id = g[:2] + sha1(''.join([clip_id, g, access_token, client_location, g, client_name])
256                                  .encode('utf-8')).hexdigest()
257
258         sources_api_url = 'http://vas.sim-technik.de/vas/live/v2/videos/%s/sources?%s' % (clip_id, compat_urllib_parse.urlencode({
259             'access_token': access_token,
260             'client_id': client_id,
261             'client_location': client_location,
262             'client_name': client_name,
263         }))
264
265         sources = self._download_json(sources_api_url, clip_id, 'Downloading sources JSON')
266         server_id = sources['server_id']
267
268         client_id = g[:2] + sha1(''.join([g, clip_id, access_token, server_id,
269                                           client_location, source_ids_str, g, client_name])
270                                  .encode('utf-8')).hexdigest()
271
272         url_api_url = 'http://vas.sim-technik.de/vas/live/v2/videos/%s/sources/url?%s' % (clip_id, compat_urllib_parse.urlencode({
273             'access_token': access_token,
274             'client_id': client_id,
275             'client_location': client_location,
276             'client_name': client_name,
277             'server_id': server_id,
278             'source_ids': source_ids_str,
279         }))
280
281         urls = self._download_json(url_api_url, clip_id, 'Downloading urls JSON')
282
283         title = self._html_search_regex(self._TITLE_REGEXES, webpage, 'title')
284         description = self._html_search_regex(self._DESCRIPTION_REGEXES, webpage, 'description', fatal=False)
285         thumbnail = self._og_search_thumbnail(webpage)
286
287         upload_date = unified_strdate(self._html_search_regex(
288             self._UPLOAD_DATE_REGEXES, webpage, 'upload date', default=None))
289
290         formats = []
291
292         urls_sources = urls['sources']
293         if isinstance(urls_sources, dict):
294             urls_sources = urls_sources.values()
295
296         def fix_bitrate(bitrate):
297             return (bitrate // 1000) if bitrate % 1000 == 0 else bitrate
298
299         for source in urls_sources:
300             protocol = source['protocol']
301             if protocol == 'rtmp' or protocol == 'rtmpe':
302                 mobj = re.search(r'^(?P<url>rtmpe?://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', source['url'])
303                 if not mobj:
304                     continue
305                 formats.append({
306                     'url': mobj.group('url'),
307                     'app': mobj.group('app'),
308                     'play_path': mobj.group('playpath'),
309                     'player_url': 'http://livepassdl.conviva.com/hf/ver/2.79.0.17083/LivePassModuleMain.swf',
310                     'page_url': 'http://www.prosieben.de',
311                     'vbr': fix_bitrate(source['bitrate']),
312                     'ext': 'mp4',
313                     'format_id': '%s_%s' % (source['cdn'], source['bitrate']),
314                 })
315             else:
316                 formats.append({
317                     'url': source['url'],
318                     'vbr': fix_bitrate(source['bitrate']),
319                 })
320
321         self._sort_formats(formats)
322
323         return {
324             'id': clip_id,
325             'title': title,
326             'description': description,
327             'thumbnail': thumbnail,
328             'upload_date': upload_date,
329             'duration': duration,
330             'formats': formats,
331         }