Merge pull request #9747 from TRox1972/lynda
[youtube-dl] / 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 ..utils import (
8     determine_ext,
9     ExtractorError,
10     js_to_json,
11     strip_jsonp,
12     unified_strdate,
13     update_url_query,
14     urlhandle_detect_ext,
15 )
16
17
18 class WDRIE(InfoExtractor):
19     _CURRENT_MAUS_URL = r'https?://(?:www\.)wdrmaus.de/(?:[^/]+/){1,2}[^/?#]+\.php5'
20     _PAGE_REGEX = r'/(?:mediathek/)?[^/]+/(?P<type>[^/]+)/(?P<display_id>.+)\.html'
21     _VALID_URL = r'(?P<page_url>https?://(?:www\d\.)?wdr\d?\.de)' + _PAGE_REGEX + '|' + _CURRENT_MAUS_URL
22
23     _TESTS = [
24         {
25             'url': 'http://www1.wdr.de/mediathek/video/sendungen/doku-am-freitag/video-geheimnis-aachener-dom-100.html',
26             # HDS download, MD5 is unstable
27             'info_dict': {
28                 'id': 'mdb-1058683',
29                 'ext': 'flv',
30                 'display_id': 'doku-am-freitag/video-geheimnis-aachener-dom-100',
31                 'title': 'Geheimnis Aachener Dom',
32                 'alt_title': 'Doku am Freitag',
33                 'upload_date': '20160304',
34                 'description': 'md5:87be8ff14d8dfd7a7ee46f0299b52318',
35                 'is_live': False,
36                 'subtitles': {'de': [{
37                     'url': 'http://ondemand-ww.wdr.de/medp/fsk0/105/1058683/1058683_12220974.xml',
38                     'ext': 'ttml',
39                 }]},
40             },
41         },
42         {
43             'url': 'http://www1.wdr.de/mediathek/audio/wdr3/wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100.html',
44             'md5': 'f4c1f96d01cf285240f53ea4309663d8',
45             'info_dict': {
46                 'id': 'mdb-1072000',
47                 'ext': 'mp3',
48                 'display_id': 'wdr3-gespraech-am-samstag/audio-schriftstellerin-juli-zeh-100',
49                 'title': 'Schriftstellerin Juli Zeh',
50                 'alt_title': 'WDR 3 Gespräch am Samstag',
51                 'upload_date': '20160312',
52                 'description': 'md5:e127d320bc2b1f149be697ce044a3dd7',
53                 'is_live': False,
54                 'subtitles': {}
55             },
56         },
57         {
58             'url': 'http://www1.wdr.de/mediathek/video/live/index.html',
59             'info_dict': {
60                 'id': 'mdb-103364',
61                 'ext': 'mp4',
62                 'display_id': 'index',
63                 'title': r're:^WDR Fernsehen im Livestream [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
64                 'alt_title': 'WDR Fernsehen Live',
65                 'upload_date': None,
66                 'description': 'md5:ae2ff888510623bf8d4b115f95a9b7c9',
67                 'is_live': True,
68                 'subtitles': {}
69             },
70             'params': {
71                 'skip_download': True,  # m3u8 download
72             },
73         },
74         {
75             'url': 'http://www1.wdr.de/mediathek/video/sendungen/aktuelle-stunde/aktuelle-stunde-120.html',
76             'playlist_mincount': 8,
77             'info_dict': {
78                 'id': 'aktuelle-stunde/aktuelle-stunde-120',
79             },
80         },
81         {
82             'url': 'http://www.wdrmaus.de/aktuelle-sendung/index.php5',
83             'info_dict': {
84                 'id': 'mdb-1096487',
85                 'ext': 'flv',
86                 'upload_date': 're:^[0-9]{8}$',
87                 'title': 're:^Die Sendung mit der Maus vom [0-9.]{10}$',
88                 'description': '- Die Sendung mit der Maus -',
89             },
90             'skip': 'The id changes from week to week because of the new episode'
91         },
92         {
93             'url': 'http://www.wdrmaus.de/sachgeschichten/sachgeschichten/achterbahn.php5',
94             # HDS download, MD5 is unstable
95             'info_dict': {
96                 'id': 'mdb-186083',
97                 'ext': 'flv',
98                 'upload_date': '20130919',
99                 'title': 'Sachgeschichte - Achterbahn ',
100                 'description': '- Die Sendung mit der Maus -',
101             },
102         },
103         {
104             'url': 'http://www1.wdr.de/radio/player/radioplayer116~_layout-popupVersion.html',
105             # Live stream, MD5 unstable
106             'info_dict': {
107                 'id': 'mdb-869971',
108                 'ext': 'flv',
109                 'title': 'Funkhaus Europa Livestream',
110                 'description': 'md5:2309992a6716c347891c045be50992e4',
111                 'upload_date': '20160101',
112             },
113         }
114     ]
115
116     def _real_extract(self, url):
117         mobj = re.match(self._VALID_URL, url)
118         url_type = mobj.group('type')
119         page_url = mobj.group('page_url')
120         display_id = mobj.group('display_id')
121         webpage = self._download_webpage(url, display_id)
122
123         # for wdr.de the data-extension is in a tag with the class "mediaLink"
124         # for wdr.de radio players, in a tag with the class "wdrrPlayerPlayBtn"
125         # for wdrmaus its in a link to the page in a multiline "videoLink"-tag
126         json_metadata = self._html_search_regex(
127             r'class=(?:"(?:mediaLink|wdrrPlayerPlayBtn)\b[^"]*"[^>]+|"videoLink\b[^"]*"[\s]*>\n[^\n]*)data-extension="([^"]+)"',
128             webpage, 'media link', default=None, flags=re.MULTILINE)
129
130         if not json_metadata:
131             entries = [
132                 self.url_result(page_url + href[0], 'WDR')
133                 for href in re.findall(
134                     r'<a href="(%s)"[^>]+data-extension=' % self._PAGE_REGEX,
135                     webpage)
136             ]
137
138             if entries:  # Playlist page
139                 return self.playlist_result(entries, playlist_id=display_id)
140
141             raise ExtractorError('No downloadable streams found', expected=True)
142
143         media_link_obj = self._parse_json(json_metadata, display_id,
144                                           transform_source=js_to_json)
145         jsonp_url = media_link_obj['mediaObj']['url']
146
147         metadata = self._download_json(
148             jsonp_url, 'metadata', transform_source=strip_jsonp)
149
150         metadata_tracker_data = metadata['trackerData']
151         metadata_media_resource = metadata['mediaResource']
152
153         formats = []
154
155         # check if the metadata contains a direct URL to a file
156         for kind, media_resource in metadata_media_resource.items():
157             if kind not in ('dflt', 'alt'):
158                 continue
159
160             for tag_name, medium_url in media_resource.items():
161                 if tag_name not in ('videoURL', 'audioURL'):
162                     continue
163
164                 ext = determine_ext(medium_url)
165                 if ext == 'm3u8':
166                     formats.extend(self._extract_m3u8_formats(
167                         medium_url, display_id, 'mp4', 'm3u8_native',
168                         m3u8_id='hls'))
169                 elif ext == 'f4m':
170                     manifest_url = update_url_query(
171                         medium_url, {'hdcore': '3.2.0', 'plugin': 'aasp-3.2.0.77.18'})
172                     formats.extend(self._extract_f4m_formats(
173                         manifest_url, display_id, f4m_id='hds', fatal=False))
174                 elif ext == 'smil':
175                     formats.extend(self._extract_smil_formats(
176                         medium_url, 'stream', fatal=False))
177                 else:
178                     a_format = {
179                         'url': medium_url
180                     }
181                     if ext == 'unknown_video':
182                         urlh = self._request_webpage(
183                             medium_url, display_id, note='Determining extension')
184                         ext = urlhandle_detect_ext(urlh)
185                         a_format['ext'] = ext
186                     formats.append(a_format)
187
188         self._sort_formats(formats)
189
190         subtitles = {}
191         caption_url = metadata_media_resource.get('captionURL')
192         if caption_url:
193             subtitles['de'] = [{
194                 'url': caption_url,
195                 'ext': 'ttml',
196             }]
197
198         title = metadata_tracker_data.get('trackerClipTitle')
199         is_live = url_type == 'live'
200
201         if is_live:
202             title = self._live_title(title)
203             upload_date = None
204         elif 'trackerClipAirTime' in metadata_tracker_data:
205             upload_date = metadata_tracker_data['trackerClipAirTime']
206         else:
207             upload_date = self._html_search_meta('DC.Date', webpage, 'upload date')
208
209         if upload_date:
210             upload_date = unified_strdate(upload_date)
211
212         return {
213             'id': metadata_tracker_data.get('trackerClipId', display_id),
214             'display_id': display_id,
215             'title': title,
216             'alt_title': metadata_tracker_data.get('trackerClipSubcategory'),
217             'formats': formats,
218             'upload_date': upload_date,
219             'description': self._html_search_meta('Description', webpage),
220             'is_live': is_live,
221             'subtitles': subtitles,
222         }
223
224
225 class WDRMobileIE(InfoExtractor):
226     _VALID_URL = r'''(?x)
227         https?://mobile-ondemand\.wdr\.de/
228         .*?/fsk(?P<age_limit>[0-9]+)
229         /[0-9]+/[0-9]+/
230         (?P<id>[0-9]+)_(?P<title>[0-9]+)'''
231     IE_NAME = 'wdr:mobile'
232     _TEST = {
233         'url': 'http://mobile-ondemand.wdr.de/CMS2010/mdb/ondemand/weltweit/fsk0/42/421735/421735_4283021.mp4',
234         'info_dict': {
235             'title': '4283021',
236             'id': '421735',
237             'ext': 'mp4',
238             'age_limit': 0,
239         },
240         'skip': 'Problems with loading data.'
241     }
242
243     def _real_extract(self, url):
244         mobj = re.match(self._VALID_URL, url)
245         return {
246             'id': mobj.group('id'),
247             'title': mobj.group('title'),
248             'age_limit': int(mobj.group('age_limit')),
249             'url': url,
250             'http_headers': {
251                 'User-Agent': 'mobile',
252             },
253         }