Merge branch 'master' of github.com:rg3/youtube-dl
[youtube-dl] / youtube_dl / extractor / dreisat.py
1 # coding: utf-8
2
3 import re
4 import xml.etree.ElementTree
5
6 from .common import InfoExtractor
7 from ..utils import (
8     determine_ext,
9     ExtractorError,
10     unified_strdate,
11 )
12
13
14 class DreiSatIE(InfoExtractor):
15     IE_NAME = '3sat'
16     _VALID_URL = r'(?:http://)?(?:www\.)?3sat.de/mediathek/index.php\?(?:(?:mode|display)=[^&]+&)*obj=(?P<id>[0-9]+)$'
17     _TEST = {
18         u"url": u"http://www.3sat.de/mediathek/index.php?obj=36983",
19         u'file': u'36983.webm',
20         u'md5': u'57c97d0469d71cf874f6815aa2b7c944',
21         u'info_dict': {
22             u"title": u"Kaffeeland Schweiz",
23             u"description": u"Über 80 Kaffeeröstereien liefern in der Schweiz das Getränk, in das das Land so vernarrt ist: Mehr als 1000 Tassen trinkt ein Schweizer pro Jahr. SCHWEIZWEIT nimmt die Kaffeekultur unter die...", 
24             u"uploader": u"3sat",
25             u"upload_date": u"20130622"
26         }
27     }
28
29
30     def _real_extract(self, url):
31         mobj = re.match(self._VALID_URL, url)
32         video_id = mobj.group('id')
33         details_url = 'http://www.3sat.de/mediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
34         details_xml = self._download_webpage(details_url, video_id, note=u'Downloading video details')
35         details_doc = xml.etree.ElementTree.fromstring(details_xml.encode('utf-8'))
36
37         thumbnail_els = details_doc.findall('.//teaserimage')
38         thumbnails = [{
39             'width': te.attrib['key'].partition('x')[0],
40             'height': te.attrib['key'].partition('x')[2],
41             'url': te.text,
42         } for te in thumbnail_els]
43
44         information_el = details_doc.find('.//information')
45         video_title = information_el.find('./title').text
46         video_description = information_el.find('./detail').text
47
48         details_el = details_doc.find('.//details')
49         video_uploader = details_el.find('./channel').text
50         upload_date = unified_strdate(details_el.find('./airtime').text)
51
52         format_els = details_doc.findall('.//formitaet')
53         formats = [{
54             'format_id': fe.attrib['basetype'],
55             'width': int(fe.find('./width').text),
56             'height': int(fe.find('./height').text),
57             'url': fe.find('./url').text,
58             'filesize': int(fe.find('./filesize').text),
59             'video_bitrate': int(fe.find('./videoBitrate').text),
60             '3sat_qualityname': fe.find('./quality').text,
61         } for fe in format_els
62             if not fe.find('./url').text.startswith('http://www.metafilegenerator.de/')]
63
64         def _sortkey(format):
65             qidx = ['low', 'med', 'high', 'veryhigh'].index(format['3sat_qualityname'])
66             prefer_http = 1 if 'rtmp' in format['url'] else 0
67             return (qidx, prefer_http, format['video_bitrate'])
68         formats.sort(key=_sortkey)
69
70         info = {
71             'id': video_id,
72             'title': video_title,
73             'formats': formats,
74             'description': video_description,
75             'thumbnails': thumbnails,
76             'thumbnail': thumbnails[-1]['url'],
77             'uploader': video_uploader,
78             'upload_date': upload_date,
79         }
80
81         # TODO: Remove when #980 has been merged
82         info['url'] = formats[-1]['url']
83         info['ext'] = determine_ext(formats[-1]['url'])
84
85         return self.video_result(info)