]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/zdf.py
[zdf/common] Use API in ZDF extractor.
[yt-dlp.git] / youtube_dl / extractor / zdf.py
1 import operator
2 import re
3
4 from .common import InfoExtractor
5 from ..utils import (
6 parse_xml_doc,
7 unified_strdate,
8 )
9
10
11 class ZDFIE(InfoExtractor):
12 _VALID_URL = r'^http://www\.zdf\.de\/ZDFmediathek(?P<hash>#)?\/(.*beitrag\/video\/)(?P<video_id>[^/\?]+)(?:\?.*)?'
13
14 def _real_extract(self, url):
15 mobj = re.match(self._VALID_URL, url)
16 video_id = mobj.group('video_id')
17
18 xml_url = u'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
19 info_xml = self._download_webpage(
20 xml_url, video_id, note=u'Downloading video info')
21 doc = parse_xml_doc(info_xml)
22
23 title = doc.find('.//information/title').text
24 description = doc.find('.//information/detail').text
25 uploader_node = doc.find('.//details/originChannelTitle')
26 uploader = None if uploader_node is None else uploader_node.text
27 duration_str = doc.find('.//details/length').text
28 duration_m = re.match(r'''(?x)^
29 (?P<hours>[0-9]{2})
30 :(?P<minutes>[0-9]{2})
31 :(?P<seconds>[0-9]{2})
32 (?:\.(?P<ms>[0-9]+)?)
33 ''', duration_str)
34 duration = (
35 (
36 (int(duration_m.group('hours')) * 60 * 60) +
37 (int(duration_m.group('minutes')) * 60) +
38 int(duration_m.group('seconds'))
39 )
40 if duration_m
41 else None
42 )
43 upload_date = unified_strdate(doc.find('.//details/airtime').text)
44
45 def xml_to_format(fnode):
46 video_url = fnode.find('url').text
47 is_available = u'http://www.metafilegenerator' not in video_url
48
49 format_id = fnode.attrib['basetype']
50 format_m = re.match(r'''(?x)
51 (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
52 (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
53 ''', format_id)
54
55 PROTO_ORDER = ['http', 'rtmp', 'rtsp']
56 try:
57 proto_pref = -PROTO_ORDER.index(format_m.group('proto'))
58 except ValueError:
59 proto_pref = 999
60
61 quality = fnode.find('./quality').text
62 QUALITY_ORDER = ['veryhigh', '300', 'high', 'med', 'low']
63 try:
64 quality_pref = -QUALITY_ORDER.index(quality)
65 except ValueError:
66 quality_pref = 999
67
68 abr = int(fnode.find('./audioBitrate').text) // 1000
69 vbr = int(fnode.find('./videoBitrate').text) // 1000
70 pref = (is_available, proto_pref, quality_pref, vbr, abr)
71
72 return {
73 'format_id': format_id,
74 'url': video_url,
75 'ext': format_m.group('container'),
76 'acodec': format_m.group('acodec'),
77 'vcodec': format_m.group('vcodec'),
78 'abr': abr,
79 'vbr': vbr,
80 'width': int(fnode.find('./width').text),
81 'height': int(fnode.find('./height').text),
82 'quality_name': quality,
83 'filesize': int(fnode.find('./filesize').text),
84 'format_note': None if is_available else u'(unavailable)',
85 '_pref': pref,
86 }
87
88 format_nodes = doc.findall('.//formitaeten/formitaet')
89 formats = sorted(map(xml_to_format, format_nodes),
90 key=operator.itemgetter('_pref'))
91
92 return {
93 'id': video_id,
94 'title': title,
95 'formats': formats,
96 'description': description,
97 'uploader': uploader,
98 'duration': duration,
99 'upload_date': upload_date,
100 }