Merge branch 'akamai_pv' of https://github.com/remitamine/youtube-dl into remitamine...
[youtube-dl] / youtube_dl / extractor / wdr.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import itertools
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import (
9     compat_parse_qs,
10     compat_urlparse,
11 )
12 from ..utils import (
13     unified_strdate,
14     qualities,
15 )
16
17
18 class WDRIE(InfoExtractor):
19     _PLAYER_REGEX = '-(?:video|audio)player(?:_size-[LMS])?'
20     _VALID_URL = r'(?P<url>https?://www\d?\.(?:wdr\d?|funkhauseuropa)\.de/)(?P<id>.+?)(?P<player>%s)?\.html' % _PLAYER_REGEX
21
22     _TESTS = [
23         {
24             'url': 'http://www1.wdr.de/mediathek/video/sendungen/servicezeit/videoservicezeit560-videoplayer_size-L.html',
25             'info_dict': {
26                 'id': 'mdb-362427',
27                 'ext': 'flv',
28                 'title': 'Servicezeit',
29                 'description': 'md5:c8f43e5e815eeb54d0b96df2fba906cb',
30                 'upload_date': '20140310',
31                 'is_live': False
32             },
33             'params': {
34                 'skip_download': True,
35             },
36             'skip': 'Page Not Found',
37         },
38         {
39             'url': 'http://www1.wdr.de/themen/av/videomargaspiegelisttot101-videoplayer.html',
40             'info_dict': {
41                 'id': 'mdb-363194',
42                 'ext': 'flv',
43                 'title': 'Marga Spiegel ist tot',
44                 'description': 'md5:2309992a6716c347891c045be50992e4',
45                 'upload_date': '20140311',
46                 'is_live': False
47             },
48             'params': {
49                 'skip_download': True,
50             },
51             'skip': 'Page Not Found',
52         },
53         {
54             'url': 'http://www1.wdr.de/themen/kultur/audioerlebtegeschichtenmargaspiegel100-audioplayer.html',
55             'md5': '83e9e8fefad36f357278759870805898',
56             'info_dict': {
57                 'id': 'mdb-194332',
58                 'ext': 'mp3',
59                 'title': 'Erlebte Geschichten: Marga Spiegel (29.11.2009)',
60                 'description': 'md5:2309992a6716c347891c045be50992e4',
61                 'upload_date': '20091129',
62                 'is_live': False
63             },
64         },
65         {
66             'url': 'http://www.funkhauseuropa.de/av/audioflaviacoelhoamaramar100-audioplayer.html',
67             'md5': '99a1443ff29af19f6c52cf6f4dc1f4aa',
68             'info_dict': {
69                 'id': 'mdb-478135',
70                 'ext': 'mp3',
71                 'title': 'Flavia Coelho: Amar é Amar',
72                 'description': 'md5:7b29e97e10dfb6e265238b32fa35b23a',
73                 'upload_date': '20140717',
74                 'is_live': False
75             },
76             'skip': 'Page Not Found',
77         },
78         {
79             'url': 'http://www1.wdr.de/mediathek/video/sendungen/quarks_und_co/filterseite-quarks-und-co100.html',
80             'playlist_mincount': 146,
81             'info_dict': {
82                 'id': 'mediathek/video/sendungen/quarks_und_co/filterseite-quarks-und-co100',
83             }
84         },
85         {
86             'url': 'http://www1.wdr.de/mediathek/video/livestream/index.html',
87             'info_dict': {
88                 'id': 'mdb-103364',
89                 'title': 're:^WDR Fernsehen Live [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
90                 'description': 'md5:ae2ff888510623bf8d4b115f95a9b7c9',
91                 'ext': 'flv',
92                 'upload_date': '20150101',
93                 'is_live': True
94             },
95             'params': {
96                 'skip_download': True,
97             },
98         }
99     ]
100
101     def _real_extract(self, url):
102         mobj = re.match(self._VALID_URL, url)
103         page_url = mobj.group('url')
104         page_id = mobj.group('id')
105
106         webpage = self._download_webpage(url, page_id)
107
108         if mobj.group('player') is None:
109             entries = [
110                 self.url_result(page_url + href, 'WDR')
111                 for href in re.findall(
112                     r'<a href="/?(.+?%s\.html)" rel="nofollow"' % self._PLAYER_REGEX,
113                     webpage)
114             ]
115
116             if entries:  # Playlist page
117                 return self.playlist_result(entries, page_id)
118
119             # Overview page
120             entries = []
121             for page_num in itertools.count(2):
122                 hrefs = re.findall(
123                     r'<li class="mediathekvideo"\s*>\s*<img[^>]*>\s*<a href="(/mediathek/video/[^"]+)"',
124                     webpage)
125                 entries.extend(
126                     self.url_result(page_url + href, 'WDR')
127                     for href in hrefs)
128                 next_url_m = re.search(
129                     r'<li class="nextToLast">\s*<a href="([^"]+)"', webpage)
130                 if not next_url_m:
131                     break
132                 next_url = page_url + next_url_m.group(1)
133                 webpage = self._download_webpage(
134                     next_url, page_id,
135                     note='Downloading playlist page %d' % page_num)
136             return self.playlist_result(entries, page_id)
137
138         flashvars = compat_parse_qs(self._html_search_regex(
139             r'<param name="flashvars" value="([^"]+)"', webpage, 'flashvars'))
140
141         page_id = flashvars['trackerClipId'][0]
142         video_url = flashvars['dslSrc'][0]
143         title = flashvars['trackerClipTitle'][0]
144         thumbnail = flashvars['startPicture'][0] if 'startPicture' in flashvars else None
145         is_live = flashvars.get('isLive', ['0'])[0] == '1'
146
147         if is_live:
148             title = self._live_title(title)
149
150         if 'trackerClipAirTime' in flashvars:
151             upload_date = flashvars['trackerClipAirTime'][0]
152         else:
153             upload_date = self._html_search_meta(
154                 'DC.Date', webpage, 'upload date')
155
156         if upload_date:
157             upload_date = unified_strdate(upload_date)
158
159         formats = []
160         preference = qualities(['S', 'M', 'L', 'XL'])
161
162         if video_url.endswith('.f4m'):
163             formats.extend(self._extract_f4m_formats(
164                 video_url + '?hdcore=3.2.0&plugin=aasp-3.2.0.77.18', page_id,
165                 f4m_id='hds', fatal=False))
166         elif video_url.endswith('.smil'):
167             formats.extend(self._extract_smil_formats(
168                 video_url, page_id, False, {
169                     'hdcore': '3.3.0',
170                     'plugin': 'aasp-3.3.0.99.43',
171                 }))
172         else:
173             formats.append({
174                 'url': video_url,
175                 'http_headers': {
176                     'User-Agent': 'mobile',
177                 },
178             })
179
180         m3u8_url = self._search_regex(
181             r'rel="adaptiv"[^>]+href="([^"]+)"',
182             webpage, 'm3u8 url', default=None)
183         if m3u8_url:
184             formats.extend(self._extract_m3u8_formats(
185                 m3u8_url, page_id, 'mp4', 'm3u8_native',
186                 m3u8_id='hls', fatal=False))
187
188         direct_urls = re.findall(
189             r'rel="web(S|M|L|XL)"[^>]+href="([^"]+)"', webpage)
190         if direct_urls:
191             for quality, video_url in direct_urls:
192                 formats.append({
193                     'url': video_url,
194                     'preference': preference(quality),
195                     'http_headers': {
196                         'User-Agent': 'mobile',
197                     },
198                 })
199
200         self._sort_formats(formats)
201
202         description = self._html_search_meta('Description', webpage, 'description')
203
204         return {
205             'id': page_id,
206             'formats': formats,
207             'title': title,
208             'description': description,
209             'thumbnail': thumbnail,
210             'upload_date': upload_date,
211             'is_live': is_live
212         }
213
214
215 class WDRMobileIE(InfoExtractor):
216     _VALID_URL = r'''(?x)
217         https?://mobile-ondemand\.wdr\.de/
218         .*?/fsk(?P<age_limit>[0-9]+)
219         /[0-9]+/[0-9]+/
220         (?P<id>[0-9]+)_(?P<title>[0-9]+)'''
221     IE_NAME = 'wdr:mobile'
222     _TEST = {
223         'url': 'http://mobile-ondemand.wdr.de/CMS2010/mdb/ondemand/weltweit/fsk0/42/421735/421735_4283021.mp4',
224         'info_dict': {
225             'title': '4283021',
226             'id': '421735',
227             'ext': 'mp4',
228             'age_limit': 0,
229         },
230         'skip': 'Problems with loading data.'
231     }
232
233     def _real_extract(self, url):
234         mobj = re.match(self._VALID_URL, url)
235         return {
236             'id': mobj.group('id'),
237             'title': mobj.group('title'),
238             'age_limit': int(mobj.group('age_limit')),
239             'url': url,
240             'http_headers': {
241                 'User-Agent': 'mobile',
242             },
243         }
244
245
246 class WDRMausIE(InfoExtractor):
247     _VALID_URL = r'https?://(?:www\.)?wdrmaus\.de/(?:[^/]+/){,2}(?P<id>[^/?#]+)(?:/index\.php5|(?<!index)\.php5|/(?:$|[?#]))'
248     IE_DESC = 'Sendung mit der Maus'
249     _TESTS = [{
250         'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
251         'info_dict': {
252             'id': 'aktuelle-sendung',
253             'ext': 'mp4',
254             'thumbnail': 're:^http://.+\.jpg',
255             'upload_date': 're:^[0-9]{8}$',
256             'title': 're:^[0-9.]{10} - Aktuelle Sendung$',
257         }
258     }, {
259         'url': 'http://www.wdrmaus.de/sachgeschichten/sachgeschichten/40_jahre_maus.php5',
260         'md5': '3b1227ca3ed28d73ec5737c65743b2a3',
261         'info_dict': {
262             'id': '40_jahre_maus',
263             'ext': 'mp4',
264             'thumbnail': 're:^http://.+\.jpg',
265             'upload_date': '20131007',
266             'title': '12.03.2011 - 40 Jahre Maus',
267         }
268     }]
269
270     def _real_extract(self, url):
271         video_id = self._match_id(url)
272
273         webpage = self._download_webpage(url, video_id)
274         param_code = self._html_search_regex(
275             r'<a href="\?startVideo=1&amp;([^"]+)"', webpage, 'parameters')
276
277         title_date = self._search_regex(
278             r'<div class="sendedatum"><p>Sendedatum:\s*([0-9\.]+)</p>',
279             webpage, 'air date')
280         title_str = self._html_search_regex(
281             r'<h1>(.*?)</h1>', webpage, 'title')
282         title = '%s - %s' % (title_date, title_str)
283         upload_date = unified_strdate(
284             self._html_search_meta('dc.date', webpage))
285
286         fields = compat_parse_qs(param_code)
287         video_url = fields['firstVideo'][0]
288         thumbnail = compat_urlparse.urljoin(url, fields['startPicture'][0])
289
290         formats = [{
291             'format_id': 'rtmp',
292             'url': video_url,
293         }]
294
295         jscode = self._download_webpage(
296             'http://www.wdrmaus.de/codebase/js/extended-medien.min.js',
297             video_id, fatal=False,
298             note='Downloading URL translation table',
299             errnote='Could not download URL translation table')
300         if jscode:
301             for m in re.finditer(
302                     r"stream:\s*'dslSrc=(?P<stream>[^']+)',\s*download:\s*'(?P<dl>[^']+)'\s*\}",
303                     jscode):
304                 if video_url.startswith(m.group('stream')):
305                     http_url = video_url.replace(
306                         m.group('stream'), m.group('dl'))
307                     formats.append({
308                         'format_id': 'http',
309                         'url': http_url,
310                     })
311                     break
312
313         self._sort_formats(formats)
314
315         return {
316             'id': video_id,
317             'title': title,
318             'formats': formats,
319             'thumbnail': thumbnail,
320             'upload_date': upload_date,
321         }