Merge remote-tracking branch 'dstftw/correct-valid-urls'
[youtube-dl] / youtube_dl / extractor / dreisat.py
1 # coding: utf-8
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     determine_ext,
8     unified_strdate,
9 )
10
11
12 class DreiSatIE(InfoExtractor):
13     IE_NAME = '3sat'
14     _VALID_URL = r'(?:http://)?(?:www\.)?3sat\.de/mediathek/index\.php\?(?:(?:mode|display)=[^&]+&)*obj=(?P<id>[0-9]+)$'
15     _TEST = {
16         u"url": u"http://www.3sat.de/mediathek/index.php?obj=36983",
17         u'file': u'36983.webm',
18         u'md5': u'57c97d0469d71cf874f6815aa2b7c944',
19         u'info_dict': {
20             u"title": u"Kaffeeland Schweiz",
21             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...", 
22             u"uploader": u"3sat",
23             u"upload_date": u"20130622"
24         }
25     }
26
27
28     def _real_extract(self, url):
29         mobj = re.match(self._VALID_URL, url)
30         video_id = mobj.group('id')
31         details_url = 'http://www.3sat.de/mediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
32         details_doc = self._download_xml(details_url, video_id, note=u'Downloading video details')
33
34         thumbnail_els = details_doc.findall('.//teaserimage')
35         thumbnails = [{
36             'width': te.attrib['key'].partition('x')[0],
37             'height': te.attrib['key'].partition('x')[2],
38             'url': te.text,
39         } for te in thumbnail_els]
40
41         information_el = details_doc.find('.//information')
42         video_title = information_el.find('./title').text
43         video_description = information_el.find('./detail').text
44
45         details_el = details_doc.find('.//details')
46         video_uploader = details_el.find('./channel').text
47         upload_date = unified_strdate(details_el.find('./airtime').text)
48
49         format_els = details_doc.findall('.//formitaet')
50         formats = [{
51             'format_id': fe.attrib['basetype'],
52             'width': int(fe.find('./width').text),
53             'height': int(fe.find('./height').text),
54             'url': fe.find('./url').text,
55             'ext': determine_ext(fe.find('./url').text),
56             'filesize': int(fe.find('./filesize').text),
57             'video_bitrate': int(fe.find('./videoBitrate').text),
58             '3sat_qualityname': fe.find('./quality').text,
59         } for fe in format_els
60             if not fe.find('./url').text.startswith('http://www.metafilegenerator.de/')]
61
62         def _sortkey(format):
63             qidx = ['low', 'med', 'high', 'veryhigh'].index(format['3sat_qualityname'])
64             prefer_http = 1 if 'rtmp' in format['url'] else 0
65             return (qidx, prefer_http, format['video_bitrate'])
66         formats.sort(key=_sortkey)
67
68         return {
69             '_type': 'video',
70             'id': video_id,
71             'title': video_title,
72             'formats': formats,
73             'description': video_description,
74             'thumbnails': thumbnails,
75             'thumbnail': thumbnails[-1]['url'],
76             'uploader': video_uploader,
77             'upload_date': upload_date,
78         }