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