]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/mdr.py
[README.md] Make download URLs consistent
[youtube-dl] / youtube_dl / extractor / mdr.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urlparse
6 from ..utils import (
7     determine_ext,
8     int_or_none,
9     parse_duration,
10     parse_iso8601,
11     xpath_text,
12 )
13
14
15 class MDRIE(InfoExtractor):
16     IE_DESC = 'MDR.DE and KiKA'
17     _VALID_URL = r'https?://(?:www\.)?(?:mdr|kika)\.de/(?:.*)/[a-z]+-?(?P<id>\d+)(?:_.+?)?\.html'
18
19     _TESTS = [{
20         # MDR regularly deletes its videos
21         'url': 'http://www.mdr.de/fakt/video189002.html',
22         'only_matching': True,
23     }, {
24         # audio
25         'url': 'http://www.mdr.de/kultur/audio1312272_zc-15948bad_zs-86171fdd.html',
26         'md5': '64c4ee50f0a791deb9479cd7bbe9d2fa',
27         'info_dict': {
28             'id': '1312272',
29             'ext': 'mp3',
30             'title': 'Feuilleton vom 30. Oktober 2015',
31             'duration': 250,
32             'uploader': 'MITTELDEUTSCHER RUNDFUNK',
33         },
34     }, {
35         'url': 'http://www.kika.de/baumhaus/videos/video19636.html',
36         'md5': '4930515e36b06c111213e80d1e4aad0e',
37         'info_dict': {
38             'id': '19636',
39             'ext': 'mp4',
40             'title': 'Baumhaus vom 30. Oktober 2015',
41             'duration': 134,
42             'uploader': 'KIKA',
43         },
44     }, {
45         'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/videos/video8182.html',
46         'md5': '5fe9c4dd7d71e3b238f04b8fdd588357',
47         'info_dict': {
48             'id': '8182',
49             'ext': 'mp4',
50             'title': 'Beutolomäus und der geheime Weihnachtswunsch',
51             'description': 'md5:b69d32d7b2c55cbe86945ab309d39bbd',
52             'timestamp': 1450950000,
53             'upload_date': '20151224',
54             'duration': 4628,
55             'uploader': 'KIKA',
56         },
57     }, {
58         'url': 'http://www.kika.de/baumhaus/sendungen/video19636_zc-fea7f8a0_zs-4bf89c60.html',
59         'only_matching': True,
60     }, {
61         'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/einzelsendung2534.html',
62         'only_matching': True,
63     }, {
64         'url': 'http://www.mdr.de/mediathek/mdr-videos/a/video-1334.html',
65         'only_matching': True,
66     }]
67
68     def _real_extract(self, url):
69         video_id = self._match_id(url)
70
71         webpage = self._download_webpage(url, video_id)
72
73         data_url = self._search_regex(
74             r'(?:dataURL|playerXml(?:["\'])?)\s*:\s*(["\'])(?P<url>.+/(?:video|audio)-?[0-9]+-avCustom\.xml)\1',
75             webpage, 'data url', group='url').replace('\/', '/')
76
77         doc = self._download_xml(
78             compat_urlparse.urljoin(url, data_url), video_id)
79
80         title = xpath_text(doc, ['./title', './broadcast/broadcastName'], 'title', fatal=True)
81
82         formats = []
83         processed_urls = []
84         for asset in doc.findall('./assets/asset'):
85             for source in (
86                     'progressiveDownload',
87                     'dynamicHttpStreamingRedirector',
88                     'adaptiveHttpStreamingRedirector'):
89                 url_el = asset.find('./%sUrl' % source)
90                 if url_el is None:
91                     continue
92
93                 video_url = url_el.text
94                 if video_url in processed_urls:
95                     continue
96
97                 processed_urls.append(video_url)
98
99                 vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
100                 abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
101
102                 ext = determine_ext(url_el.text)
103                 if ext == 'm3u8':
104                     url_formats = self._extract_m3u8_formats(
105                         video_url, video_id, 'mp4', entry_protocol='m3u8_native',
106                         preference=0, m3u8_id='HLS', fatal=False)
107                 elif ext == 'f4m':
108                     url_formats = self._extract_f4m_formats(
109                         video_url + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id,
110                         preference=0, f4m_id='HDS', fatal=False)
111                 else:
112                     media_type = xpath_text(asset, './mediaType', 'media type', default='MP4')
113                     vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
114                     abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
115                     filesize = int_or_none(xpath_text(asset, './fileSize', 'file size'))
116
117                     f = {
118                         'url': video_url,
119                         'format_id': '%s-%d' % (media_type, vbr or abr),
120                         'filesize': filesize,
121                         'abr': abr,
122                         'preference': 1,
123                     }
124
125                     if vbr:
126                         width = int_or_none(xpath_text(asset, './frameWidth', 'width'))
127                         height = int_or_none(xpath_text(asset, './frameHeight', 'height'))
128                         f.update({
129                             'vbr': vbr,
130                             'width': width,
131                             'height': height,
132                         })
133
134                     url_formats = [f]
135
136                 if not url_formats:
137                     continue
138
139                 if not vbr:
140                     for f in url_formats:
141                         abr = f.get('tbr') or abr
142                         if 'tbr' in f:
143                             del f['tbr']
144                         f.update({
145                             'abr': abr,
146                             'vcodec': 'none',
147                         })
148
149                 formats.extend(url_formats)
150
151         self._sort_formats(formats)
152
153         description = xpath_text(doc, './broadcast/broadcastDescription', 'description')
154         timestamp = parse_iso8601(
155             xpath_text(
156                 doc, [
157                     './broadcast/broadcastDate',
158                     './broadcast/broadcastStartDate',
159                     './broadcast/broadcastEndDate'],
160                 'timestamp', default=None))
161         duration = parse_duration(xpath_text(doc, './duration', 'duration'))
162         uploader = xpath_text(doc, './rights', 'uploader')
163
164         return {
165             'id': video_id,
166             'title': title,
167             'description': description,
168             'timestamp': timestamp,
169             'duration': duration,
170             'uploader': uploader,
171             'formats': formats,
172         }