[mdr] Simplify
[youtube-dl] / youtube_dl / extractor / mdr.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5     ExtractorError,
6 )
7
8
9 class MDRIE(InfoExtractor):
10     _VALID_URL = r'^(?P<domain>(?:https?://)?(?:www\.)?mdr\.de)/mediathek/(?:.*)/(?P<type>video|audio)(?P<video_id>[^/_]+)_.*'
11
12     _TESTS = [{
13         u'url': u'http://www.mdr.de/mediathek/themen/nachrichten/video165624_zc-c5c7de76_zs-3795826d.html',
14         u'file': u'165624.mp4',
15         u'md5': u'ae785f36ecbf2f19b42edf1bc9c85815',
16         u'info_dict': {
17             u"title": u"MDR aktuell Eins30 09.12.2013, 22:48 Uhr"
18         },
19     },
20     {
21         u'url': u'http://www.mdr.de/mediathek/radio/mdr1-radio-sachsen/audio718370_zc-67b21197_zs-1b9b2483.html',
22         u'file': u'718370.mp3',
23         u'md5': u'a9d21345a234c7b45dee612f290fd8d7',
24         u'info_dict': {
25             u"title": u"MDR 1 RADIO SACHSEN 10.12.2013, 05:00 Uhr"
26         },
27     }]
28
29     def _real_extract(self, url):
30         m = re.match(self._VALID_URL, url)
31         video_id = m.group('video_id')
32         domain = m.group('domain')
33         mediatype = m.group('type')
34
35         # determine title and media streams from webpage
36         html = self._download_webpage(url, video_id)
37
38         title = self._html_search_regex(r'<h2>(.*?)</h2>', html, u'title')
39         xmlurl = self._search_regex(
40             r'(/mediathek/(?:.+)/(?:video|audio)[0-9]+-avCustom.xml)', html, u'XML URL')
41
42         doc = self._download_xml(domain + xmlurl, video_id)
43         formats = []
44         for a in doc.findall('./assets/asset'):
45             url_el = a.find('.//progressiveDownloadUrl')
46             if url_el is None:
47                 continue
48             abr = int(a.find('bitrateAudio').text) // 1000
49             media_type = a.find('mediaType').text
50             format = {
51                 'abr': abr,
52                 'filesize': int(a.find('fileSize').text),
53                 'url': url_el.text,
54             }
55
56             vbr_el = a.find('bitrateVideo')
57             if vbr_el is None:
58                 format.update({
59                     'vcodec': 'none',
60                     'format_id': u'%s-%d' % (media_type, abr),
61                 })
62             else:
63                 vbr = int(vbr_el.text) // 1000
64                 format.update({
65                     'vbr': vbr,
66                     'width': int(a.find('frameWidth').text),
67                     'height': int(a.find('frameHeight').text),
68                     'format_id': u'%s-%d' % (media_type, vbr),
69                 })
70             formats.append(format)
71         formats.sort(key=lambda f: (f.get('vbr'), f['abr']))
72         if not formats:
73             raise ValueError('Could not find any valid formats')
74
75         return {
76             'id': video_id,
77             'title': title,
78             'formats': formats,
79         }