[zdf] Simplify
[youtube-dl] / youtube_dl / extractor / zdf.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     unified_strdate,
10 )
11
12
13 class ZDFIE(InfoExtractor):
14     _VALID_URL = r'^https?://www\.zdf\.de/ZDFmediathek(?P<hash>#)?/(.*beitrag/(?:video/)?)(?P<id>[0-9]+)(?:/[^/?]+)?(?:\?.*)?'
15
16     _TEST = {
17         'url': 'http://www.zdf.de/ZDFmediathek/beitrag/video/2037704/ZDFspezial---Ende-des-Machtpokers--?bc=sts;stt',
18         'info_dict': {
19             'id': '2037704',
20             'ext': 'webm',
21             'title': 'ZDFspezial - Ende des Machtpokers',
22             'description': 'Union und SPD haben sich auf einen Koalitionsvertrag geeinigt. Aber was bedeutet das für die Bürger? Sehen Sie hierzu das ZDFspezial "Ende des Machtpokers - Große Koalition für Deutschland".',
23             'duration': 1022,
24             'uploader': 'spezial',
25             'uploader_id': '225948',
26             'upload_date': '20131127',
27         },
28         'skip': 'Videos on ZDF.de are depublicised in short order',
29     }
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33
34         xml_url = 'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
35         doc = self._download_xml(
36             xml_url, video_id,
37             note='Downloading video info',
38             errnote='Failed to download video info')
39
40         title = doc.find('.//information/title').text
41         description = doc.find('.//information/detail').text
42         duration = int(doc.find('.//details/lengthSec').text)
43         uploader_node = doc.find('.//details/originChannelTitle')
44         uploader = None if uploader_node is None else uploader_node.text
45         uploader_id_node = doc.find('.//details/originChannelId')
46         uploader_id = None if uploader_id_node is None else uploader_id_node.text
47         upload_date = unified_strdate(doc.find('.//details/airtime').text)
48
49         def xml_to_format(fnode):
50             video_url = fnode.find('url').text
51             is_available = 'http://www.metafilegenerator' not in video_url
52
53             format_id = fnode.attrib['basetype']
54             format_m = re.match(r'''(?x)
55                 (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
56                 (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
57             ''', format_id)
58
59             ext = format_m.group('container')
60             proto = format_m.group('proto').lower()
61
62             quality = fnode.find('./quality').text
63             abr = int(fnode.find('./audioBitrate').text) // 1000
64             vbr_node = fnode.find('./videoBitrate')
65             vbr = None if vbr_node is None else int(vbr_node.text) // 1000
66
67             width_node = fnode.find('./width')
68             width = None if width_node is None else int_or_none(width_node.text)
69             height_node = fnode.find('./height')
70             height = None if height_node is None else int_or_none(height_node.text)
71
72             format_note = ''
73             if not format_note:
74                 format_note = None
75
76             return {
77                 'format_id': format_id + '-' + quality,
78                 'url': video_url,
79                 'ext': ext,
80                 'acodec': format_m.group('acodec'),
81                 'vcodec': format_m.group('vcodec'),
82                 'abr': abr,
83                 'vbr': vbr,
84                 'width': width,
85                 'height': height,
86                 'filesize': int_or_none(fnode.find('./filesize').text),
87                 'format_note': format_note,
88                 'protocol': proto,
89                 '_available': is_available,
90             }
91
92         format_nodes = doc.findall('.//formitaeten/formitaet')
93         formats = list(filter(
94             lambda f: f['_available'],
95             map(xml_to_format, format_nodes)))
96
97         self._sort_formats(formats)
98
99         return {
100             'id': video_id,
101             'title': title,
102             'description': description,
103             'duration': duration,
104             'uploader': uploader,
105             'uploader_id': uploader_id,
106             'upload_date': upload_date,
107             'formats': formats,
108         }