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