8e25ecf280769166a49d18cfd2508bd6d90caa74
[ytdl] / youtube_dl / extractor / wdr.py
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_parse_qs,
9     compat_urlparse,
10 )
11 from ..utils import (
12     determine_ext,
13     unified_strdate,
14 )
15
16
17 class WDRIE(InfoExtractor):
18     _PLAYER_REGEX = '-(?:video|audio)player(?:_size-[LMS])?'
19     _VALID_URL = r'(?P<url>https?://www\d?\.(?:wdr\d?|funkhauseuropa)\.de/)(?P<id>.+?)(?P<player>%s)?\.html' % _PLAYER_REGEX
20
21     _TESTS = [
22         {
23             'url': 'http://www1.wdr.de/mediathek/video/sendungen/servicezeit/videoservicezeit560-videoplayer_size-L.html',
24             'info_dict': {
25                 'id': 'mdb-362427',
26                 'ext': 'flv',
27                 'title': 'Servicezeit',
28                 'description': 'md5:c8f43e5e815eeb54d0b96df2fba906cb',
29                 'upload_date': '20140310',
30             },
31             'params': {
32                 'skip_download': True,
33             },
34         },
35         {
36             'url': 'http://www1.wdr.de/themen/av/videomargaspiegelisttot101-videoplayer.html',
37             'info_dict': {
38                 'id': 'mdb-363194',
39                 'ext': 'flv',
40                 'title': 'Marga Spiegel ist tot',
41                 'description': 'md5:2309992a6716c347891c045be50992e4',
42                 'upload_date': '20140311',
43             },
44             'params': {
45                 'skip_download': True,
46             },
47         },
48         {
49             'url': 'http://www1.wdr.de/themen/kultur/audioerlebtegeschichtenmargaspiegel100-audioplayer.html',
50             'md5': '83e9e8fefad36f357278759870805898',
51             'info_dict': {
52                 'id': 'mdb-194332',
53                 'ext': 'mp3',
54                 'title': 'Erlebte Geschichten: Marga Spiegel (29.11.2009)',
55                 'description': 'md5:2309992a6716c347891c045be50992e4',
56                 'upload_date': '20091129',
57             },
58         },
59         {
60             'url': 'http://www.funkhauseuropa.de/av/audioflaviacoelhoamaramar100-audioplayer.html',
61             'md5': '99a1443ff29af19f6c52cf6f4dc1f4aa',
62             'info_dict': {
63                 'id': 'mdb-478135',
64                 'ext': 'mp3',
65                 'title': 'Flavia Coelho: Amar é Amar',
66                 'description': 'md5:7b29e97e10dfb6e265238b32fa35b23a',
67                 'upload_date': '20140717',
68             },
69         },
70     ]
71
72     def _real_extract(self, url):
73         mobj = re.match(self._VALID_URL, url)
74         page_url = mobj.group('url')
75         page_id = mobj.group('id')
76
77         webpage = self._download_webpage(url, page_id)
78
79         if mobj.group('player') is None:
80             entries = [
81                 self.url_result(page_url + href, 'WDR')
82                 for href in re.findall(r'<a href="/?(.+?%s\.html)" rel="nofollow"' % self._PLAYER_REGEX, webpage)
83             ]
84             return self.playlist_result(entries, page_id)
85
86         flashvars = compat_parse_qs(
87             self._html_search_regex(r'<param name="flashvars" value="([^"]+)"', webpage, 'flashvars'))
88
89         page_id = flashvars['trackerClipId'][0]
90         video_url = flashvars['dslSrc'][0]
91         title = flashvars['trackerClipTitle'][0]
92         thumbnail = flashvars['startPicture'][0] if 'startPicture' in flashvars else None
93
94         if 'trackerClipAirTime' in flashvars:
95             upload_date = flashvars['trackerClipAirTime'][0]
96         else:
97             upload_date = self._html_search_meta('DC.Date', webpage, 'upload date')
98
99         if upload_date:
100             upload_date = unified_strdate(upload_date)
101
102         if video_url.endswith('.f4m'):
103             video_url += '?hdcore=3.2.0&plugin=aasp-3.2.0.77.18'
104             ext = 'flv'
105         else:
106             ext = determine_ext(video_url)
107
108         description = self._html_search_meta('Description', webpage, 'description')
109
110         return {
111             'id': page_id,
112             'url': video_url,
113             'ext': ext,
114             'title': title,
115             'description': description,
116             'thumbnail': thumbnail,
117             'upload_date': upload_date,
118         }
119
120
121 class WDRMobileIE(InfoExtractor):
122     _VALID_URL = r'''(?x)
123         https?://mobile-ondemand\.wdr\.de/
124         .*?/fsk(?P<age_limit>[0-9]+)
125         /[0-9]+/[0-9]+/
126         (?P<id>[0-9]+)_(?P<title>[0-9]+)'''
127     IE_NAME = 'wdr:mobile'
128     _TEST = {
129         'url': 'http://mobile-ondemand.wdr.de/CMS2010/mdb/ondemand/weltweit/fsk0/42/421735/421735_4283021.mp4',
130         'info_dict': {
131             'title': '4283021',
132             'id': '421735',
133             'ext': 'mp4',
134             'age_limit': 0,
135         },
136         'skip': 'Problems with loading data.'
137     }
138
139     def _real_extract(self, url):
140         mobj = re.match(self._VALID_URL, url)
141         return {
142             'id': mobj.group('id'),
143             'title': mobj.group('title'),
144             'age_limit': int(mobj.group('age_limit')),
145             'url': url,
146             'user_agent': 'mobile',
147         }
148
149
150 class WDRMausIE(InfoExtractor):
151     _VALID_URL = 'http://(?:www\.)?wdrmaus\.de/(?:[^/]+/){,2}(?P<id>[^/?#]+)(?:/index\.php5|(?<!index)\.php5|/(?:$|[?#]))'
152     IE_DESC = 'Sendung mit der Maus'
153     _TESTS = [{
154         'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
155         'info_dict': {
156             'id': 'aktuelle-sendung',
157             'ext': 'mp4',
158             'thumbnail': 're:^http://.+\.jpg',
159             'upload_date': 're:^[0-9]{8}$',
160             'title': 're:^[0-9.]{10} - Aktuelle Sendung$',
161         }
162     }, {
163         'url': 'http://www.wdrmaus.de/sachgeschichten/sachgeschichten/40_jahre_maus.php5',
164         'md5': '3b1227ca3ed28d73ec5737c65743b2a3',
165         'info_dict': {
166             'id': '40_jahre_maus',
167             'ext': 'mp4',
168             'thumbnail': 're:^http://.+\.jpg',
169             'upload_date': '20131007',
170             'title': '12.03.2011 - 40 Jahre Maus',
171         }
172     }]
173
174     def _real_extract(self, url):
175         mobj = re.match(self._VALID_URL, url)
176         video_id = mobj.group('id')
177
178         webpage = self._download_webpage(url, video_id)
179         param_code = self._html_search_regex(
180             r'<a href="\?startVideo=1&amp;([^"]+)"', webpage, 'parameters')
181
182         title_date = self._search_regex(
183             r'<div class="sendedatum"><p>Sendedatum:\s*([0-9\.]+)</p>',
184             webpage, 'air date')
185         title_str = self._html_search_regex(
186             r'<h1>(.*?)</h1>', webpage, 'title')
187         title = '%s - %s' % (title_date, title_str)
188         upload_date = unified_strdate(
189             self._html_search_meta('dc.date', webpage))
190
191         fields = compat_parse_qs(param_code)
192         video_url = fields['firstVideo'][0]
193         thumbnail = compat_urlparse.urljoin(url, fields['startPicture'][0])
194
195         formats = [{
196             'format_id': 'rtmp',
197             'url': video_url,
198         }]
199
200         jscode = self._download_webpage(
201             'http://www.wdrmaus.de/codebase/js/extended-medien.min.js',
202             video_id, fatal=False,
203             note='Downloading URL translation table',
204             errnote='Could not download URL translation table')
205         if jscode:
206             for m in re.finditer(
207                     r"stream:\s*'dslSrc=(?P<stream>[^']+)',\s*download:\s*'(?P<dl>[^']+)'\s*\}",
208                     jscode):
209                 if video_url.startswith(m.group('stream')):
210                     http_url = video_url.replace(
211                         m.group('stream'), m.group('dl'))
212                     formats.append({
213                         'format_id': 'http',
214                         'url': http_url,
215                     })
216                     break
217
218         self._sort_formats(formats)
219
220         return {
221             'id': video_id,
222             'title': title,
223             'formats': formats,
224             'thumbnail': thumbnail,
225             'upload_date': upload_date,
226         }
227
228 # TODO test _1