]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/mdr.py
[brightcove:new] Add ability to smuggle geo_countries into URL
[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         'skip': '404 not found',
35     }, {
36         'url': 'http://www.kika.de/baumhaus/videos/video19636.html',
37         'md5': '4930515e36b06c111213e80d1e4aad0e',
38         'info_dict': {
39             'id': '19636',
40             'ext': 'mp4',
41             'title': 'Baumhaus vom 30. Oktober 2015',
42             'duration': 134,
43             'uploader': 'KIKA',
44         },
45         'skip': '404 not found',
46     }, {
47         'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/videos/video8182.html',
48         'md5': '5fe9c4dd7d71e3b238f04b8fdd588357',
49         'info_dict': {
50             'id': '8182',
51             'ext': 'mp4',
52             'title': 'Beutolomäus und der geheime Weihnachtswunsch',
53             'description': 'md5:b69d32d7b2c55cbe86945ab309d39bbd',
54             'timestamp': 1482541200,
55             'upload_date': '20161224',
56             'duration': 4628,
57             'uploader': 'KIKA',
58         },
59     }, {
60         # audio with alternative playerURL pattern
61         'url': 'http://www.mdr.de/kultur/videos-und-audios/audio-radio/operation-mindfuck-robert-wilson100.html',
62         'info_dict': {
63             'id': '100',
64             'ext': 'mp4',
65             'title': 'Feature: Operation Mindfuck - Robert Anton Wilson',
66             'duration': 3239,
67             'uploader': 'MITTELDEUTSCHER RUNDFUNK',
68         },
69     }, {
70         'url': 'http://www.kika.de/baumhaus/sendungen/video19636_zc-fea7f8a0_zs-4bf89c60.html',
71         'only_matching': True,
72     }, {
73         'url': 'http://www.kika.de/sendungen/einzelsendungen/weihnachtsprogramm/einzelsendung2534.html',
74         'only_matching': True,
75     }, {
76         'url': 'http://www.mdr.de/mediathek/mdr-videos/a/video-1334.html',
77         'only_matching': True,
78     }]
79
80     def _real_extract(self, url):
81         video_id = self._match_id(url)
82
83         webpage = self._download_webpage(url, video_id)
84
85         data_url = self._search_regex(
86             r'(?:dataURL|playerXml(?:["\'])?)\s*:\s*(["\'])(?P<url>.+?-avCustom\.xml)\1',
87             webpage, 'data url', group='url').replace(r'\/', '/')
88
89         doc = self._download_xml(
90             compat_urlparse.urljoin(url, data_url), video_id)
91
92         title = xpath_text(doc, ['./title', './broadcast/broadcastName'], 'title', fatal=True)
93
94         formats = []
95         processed_urls = []
96         for asset in doc.findall('./assets/asset'):
97             for source in (
98                     'progressiveDownload',
99                     'dynamicHttpStreamingRedirector',
100                     'adaptiveHttpStreamingRedirector'):
101                 url_el = asset.find('./%sUrl' % source)
102                 if url_el is None:
103                     continue
104
105                 video_url = url_el.text
106                 if video_url in processed_urls:
107                     continue
108
109                 processed_urls.append(video_url)
110
111                 vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
112                 abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
113
114                 ext = determine_ext(url_el.text)
115                 if ext == 'm3u8':
116                     url_formats = self._extract_m3u8_formats(
117                         video_url, video_id, 'mp4', entry_protocol='m3u8_native',
118                         preference=0, m3u8_id='HLS', fatal=False)
119                 elif ext == 'f4m':
120                     url_formats = self._extract_f4m_formats(
121                         video_url + '?hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id,
122                         preference=0, f4m_id='HDS', fatal=False)
123                 else:
124                     media_type = xpath_text(asset, './mediaType', 'media type', default='MP4')
125                     vbr = int_or_none(xpath_text(asset, './bitrateVideo', 'vbr'), 1000)
126                     abr = int_or_none(xpath_text(asset, './bitrateAudio', 'abr'), 1000)
127                     filesize = int_or_none(xpath_text(asset, './fileSize', 'file size'))
128
129                     f = {
130                         'url': video_url,
131                         'format_id': '%s-%d' % (media_type, vbr or abr),
132                         'filesize': filesize,
133                         'abr': abr,
134                         'preference': 1,
135                     }
136
137                     if vbr:
138                         width = int_or_none(xpath_text(asset, './frameWidth', 'width'))
139                         height = int_or_none(xpath_text(asset, './frameHeight', 'height'))
140                         f.update({
141                             'vbr': vbr,
142                             'width': width,
143                             'height': height,
144                         })
145
146                     url_formats = [f]
147
148                 if not url_formats:
149                     continue
150
151                 if not vbr:
152                     for f in url_formats:
153                         abr = f.get('tbr') or abr
154                         if 'tbr' in f:
155                             del f['tbr']
156                         f.update({
157                             'abr': abr,
158                             'vcodec': 'none',
159                         })
160
161                 formats.extend(url_formats)
162
163         self._sort_formats(formats)
164
165         description = xpath_text(doc, './broadcast/broadcastDescription', 'description')
166         timestamp = parse_iso8601(
167             xpath_text(
168                 doc, [
169                     './broadcast/broadcastDate',
170                     './broadcast/broadcastStartDate',
171                     './broadcast/broadcastEndDate'],
172                 'timestamp', default=None))
173         duration = parse_duration(xpath_text(doc, './duration', 'duration'))
174         uploader = xpath_text(doc, './rights', 'uploader')
175
176         return {
177             'id': video_id,
178             'title': title,
179             'description': description,
180             'timestamp': timestamp,
181             'duration': duration,
182             'uploader': uploader,
183             'formats': formats,
184         }