Merge pull request #7225 from kennell/master
[youtube-dl] / youtube_dl / extractor / zdf.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import functools
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     int_or_none,
10     unified_strdate,
11     OnDemandPagedList,
12 )
13
14
15 def extract_from_xml_url(ie, video_id, xml_url):
16     doc = ie._download_xml(
17         xml_url, video_id,
18         note='Downloading video info',
19         errnote='Failed to download video info')
20
21     title = doc.find('.//information/title').text
22     description = doc.find('.//information/detail').text
23     duration = int(doc.find('.//details/lengthSec').text)
24     uploader_node = doc.find('.//details/originChannelTitle')
25     uploader = None if uploader_node is None else uploader_node.text
26     uploader_id_node = doc.find('.//details/originChannelId')
27     uploader_id = None if uploader_id_node is None else uploader_id_node.text
28     upload_date = unified_strdate(doc.find('.//details/airtime').text)
29
30     def xml_to_format(fnode):
31         video_url = fnode.find('url').text
32         is_available = 'http://www.metafilegenerator' not in video_url
33
34         format_id = fnode.attrib['basetype']
35         format_m = re.match(r'''(?x)
36             (?P<vcodec>[^_]+)_(?P<acodec>[^_]+)_(?P<container>[^_]+)_
37             (?P<proto>[^_]+)_(?P<index>[^_]+)_(?P<indexproto>[^_]+)
38         ''', format_id)
39
40         ext = format_m.group('container')
41         proto = format_m.group('proto').lower()
42
43         quality = fnode.find('./quality').text
44         abr = int(fnode.find('./audioBitrate').text) // 1000
45         vbr_node = fnode.find('./videoBitrate')
46         vbr = None if vbr_node is None else int(vbr_node.text) // 1000
47
48         width_node = fnode.find('./width')
49         width = None if width_node is None else int_or_none(width_node.text)
50         height_node = fnode.find('./height')
51         height = None if height_node is None else int_or_none(height_node.text)
52
53         format_note = ''
54         if not format_note:
55             format_note = None
56
57         return {
58             'format_id': format_id + '-' + quality,
59             'url': video_url,
60             'ext': ext,
61             'acodec': format_m.group('acodec'),
62             'vcodec': format_m.group('vcodec'),
63             'abr': abr,
64             'vbr': vbr,
65             'width': width,
66             'height': height,
67             'filesize': int_or_none(fnode.find('./filesize').text),
68             'format_note': format_note,
69             'protocol': proto,
70             '_available': is_available,
71         }
72
73     def xml_to_thumbnails(fnode):
74         thumbnails = list()
75         for node in fnode:
76             thumbnail = {'url': node.text}
77             if 'key' in node.attrib:
78                 m = re.match('^([0-9]+)x([0-9]+)$', node.attrib['key'])
79                 if m:
80                     thumbnail['width'] = int(m.group(1))
81                     thumbnail['height'] = int(m.group(2))
82             thumbnails.append(thumbnail)
83         return thumbnails
84
85
86     thumbnail_nodes = doc.findall('.//teaserimages/teaserimage')
87     thumbnails = xml_to_thumbnails(thumbnail_nodes)
88
89     format_nodes = doc.findall('.//formitaeten/formitaet')
90     formats = list(filter(
91         lambda f: f['_available'],
92         map(xml_to_format, format_nodes)))
93     ie._sort_formats(formats)
94
95     return {
96         'id': video_id,
97         'title': title,
98         'description': description,
99         'duration': duration,
100         'thumbnails': thumbnails,
101         'uploader': uploader,
102         'uploader_id': uploader_id,
103         'upload_date': upload_date,
104         'formats': formats,
105     }
106
107
108 class ZDFIE(InfoExtractor):
109     _VALID_URL = r'(?:zdf:|zdf:video:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/(.*beitrag/(?:video/)?))(?P<id>[0-9]+)(?:/[^/?]+)?(?:\?.*)?'
110
111     _TEST = {
112         'url': 'http://www.zdf.de/ZDFmediathek/beitrag/video/2037704/ZDFspezial---Ende-des-Machtpokers--?bc=sts;stt',
113         'info_dict': {
114             'id': '2037704',
115             'ext': 'webm',
116             'title': 'ZDFspezial - Ende des Machtpokers',
117             '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".',
118             'duration': 1022,
119             'uploader': 'spezial',
120             'uploader_id': '225948',
121             'upload_date': '20131127',
122         },
123         'skip': 'Videos on ZDF.de are depublicised in short order',
124     }
125
126     def _real_extract(self, url):
127         video_id = self._match_id(url)
128         xml_url = 'http://www.zdf.de/ZDFmediathek/xmlservice/web/beitragsDetails?ak=web&id=%s' % video_id
129         return extract_from_xml_url(self, video_id, xml_url)
130
131
132 class ZDFChannelIE(InfoExtractor):
133     _VALID_URL = r'(?:zdf:topic:|https?://www\.zdf\.de/ZDFmediathek(?:#)?/.*kanaluebersicht/)(?P<id>[0-9]+)'
134     _TEST = {
135         'url': 'http://www.zdf.de/ZDFmediathek#/kanaluebersicht/1586442/sendung/Titanic',
136         'info_dict': {
137             'id': '1586442',
138         },
139         'playlist_count': 3,
140     }
141     _PAGE_SIZE = 50
142
143     def _fetch_page(self, channel_id, page):
144         offset = page * self._PAGE_SIZE
145         xml_url = (
146             'http://www.zdf.de/ZDFmediathek/xmlservice/web/aktuellste?ak=web&offset=%d&maxLength=%d&id=%s'
147             % (offset, self._PAGE_SIZE, channel_id))
148         doc = self._download_xml(
149             xml_url, channel_id,
150             note='Downloading channel info',
151             errnote='Failed to download channel info')
152
153         title = doc.find('.//information/title').text
154         description = doc.find('.//information/detail').text
155         for asset in doc.findall('.//teasers/teaser'):
156             a_type = asset.find('./type').text
157             a_id = asset.find('./details/assetId').text
158             if a_type not in ('video', 'topic'):
159                 continue
160             yield {
161                 '_type': 'url',
162                 'playlist_title': title,
163                 'playlist_description': description,
164                 'url': 'zdf:%s:%s' % (a_type, a_id),
165             }
166
167     def _real_extract(self, url):
168         channel_id = self._match_id(url)
169         entries = OnDemandPagedList(
170             functools.partial(self._fetch_page, channel_id), self._PAGE_SIZE)
171
172         return {
173             '_type': 'playlist',
174             'id': channel_id,
175             'entries': entries,
176         }