Merge remote-tracking branch 'origin/master'
[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             'filesize': int(fe.find('./filesize').text),
56             'video_bitrate': int(fe.find('./videoBitrate').text),
57         } for fe in format_els
58             if not fe.find('./url').text.startswith('http://www.metafilegenerator.de/')]
59
60         self._sort_formats(formats)
61
62         return {
63             '_type': 'video',
64             'id': video_id,
65             'title': video_title,
66             'formats': formats,
67             'description': video_description,
68             'thumbnails': thumbnails,
69             'thumbnail': thumbnails[-1]['url'],
70             'uploader': video_uploader,
71             'upload_date': upload_date,
72         }