aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/extractor/mdr.py
blob: d29cf2c07f205c6a79b9de0a981400b83d4cdcaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import re

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
)


class MDRIE(InfoExtractor):
    _VALID_URL = r'^(?P<domain>(?:https?://)?(?:www\.)?mdr\.de)/mediathek/(?:.*)/(?P<type>video|audio)(?P<video_id>[^/_]+)_.*'

    _TESTS = [{
        u'url': u'http://www.mdr.de/mediathek/themen/nachrichten/video165624_zc-c5c7de76_zs-3795826d.html',
        u'file': u'165624.mp4',
        u'md5': u'ae785f36ecbf2f19b42edf1bc9c85815',
        u'info_dict': {
            u"title": u"MDR aktuell Eins30 09.12.2013, 22:48 Uhr"
        },
    },
    {
        u'url': u'http://www.mdr.de/mediathek/radio/mdr1-radio-sachsen/audio718370_zc-67b21197_zs-1b9b2483.html',
        u'file': u'718370.mp3',
        u'md5': u'a9d21345a234c7b45dee612f290fd8d7',
        u'info_dict': {
            u"title": u"MDR 1 RADIO SACHSEN 10.12.2013, 05:00 Uhr"
        },
    }]

    def _real_extract(self, url):
        m = re.match(self._VALID_URL, url)
        video_id = m.group('video_id')
        domain = m.group('domain')

        # determine title and media streams from webpage
        html = self._download_webpage(url, video_id)

        title = self._html_search_regex(r'<h2>(.*?)</h2>', html, u'title')
        xmlurl = self._search_regex(
            r'(/mediathek/(?:.+)/(?:video|audio)[0-9]+-avCustom.xml)', html, u'XML URL')

        doc = self._download_xml(domain + xmlurl, video_id)
        formats = []
        for a in doc.findall('./assets/asset'):
            url_el = a.find('.//progressiveDownloadUrl')
            if url_el is None:
                continue
            abr = int(a.find('bitrateAudio').text) // 1000
            media_type = a.find('mediaType').text
            format = {
                'abr': abr,
                'filesize': int(a.find('fileSize').text),
                'url': url_el.text,
            }

            vbr_el = a.find('bitrateVideo')
            if vbr_el is None:
                format.update({
                    'vcodec': 'none',
                    'format_id': u'%s-%d' % (media_type, abr),
                })
            else:
                vbr = int(vbr_el.text) // 1000
                format.update({
                    'vbr': vbr,
                    'width': int(a.find('frameWidth').text),
                    'height': int(a.find('frameHeight').text),
                    'format_id': u'%s-%d' % (media_type, vbr),
                })
            formats.append(format)
        formats.sort(key=lambda f: (f.get('vbr'), f['abr']))
        if not formats:
            raise ExtractorError(u'Could not find any valid formats')

        return {
            'id': video_id,
            'title': title,
            'formats': formats,
        }