]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/br.py
86f0c2861e35f296f594a4ac45bbfe74b799d9e0
[youtubedl] / youtube_dl / extractor / br.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 ExtractorError,
9 int_or_none,
10 parse_duration,
11 )
12
13
14 class BRIE(InfoExtractor):
15 IE_DESC = 'Bayerischer Rundfunk Mediathek'
16 _VALID_URL = r'https?://(?:www\.)?br\.de/(?:[a-z0-9\-_]+/)+(?P<id>[a-z0-9\-_]+)\.html'
17 _BASE_URL = 'http://www.br.de'
18
19 _TESTS = [
20 {
21 'url': 'http://www.br.de/mediathek/video/sendungen/heimatsound/heimatsound-festival-2014-trailer-100.html',
22 'md5': '93556dd2bcb2948d9259f8670c516d59',
23 'info_dict': {
24 'id': '25e279aa-1ffd-40fd-9955-5325bd48a53a',
25 'ext': 'mp4',
26 'title': 'Wenn das Traditions-Theater wackelt',
27 'description': 'Heimatsound-Festival 2014: Wenn das Traditions-Theater wackelt',
28 'duration': 34,
29 }
30 },
31 {
32 'url': 'http://www.br.de/mediathek/video/sendungen/unter-unserem-himmel/unter-unserem-himmel-alpen-ueber-den-pass-100.html',
33 'md5': 'ab451b09d861dbed7d7cc9ab0be19ebe',
34 'info_dict': {
35 'id': '2c060e69-3a27-4e13-b0f0-668fac17d812',
36 'ext': 'mp4',
37 'title': 'Über den Pass',
38 'description': 'Die Eroberung der Alpen: Über den Pass',
39 'duration': 2588,
40 }
41 },
42 {
43 'url': 'http://www.br.de/nachrichten/schaeuble-haushaltsentwurf-bundestag-100.html',
44 'md5': '3db0df1a9a9cd9fa0c70e6ea8aa8e820',
45 'info_dict': {
46 'id': 'c6aae3de-2cf9-43f2-957f-f17fef9afaab',
47 'ext': 'aac',
48 'title': '"Keine neuen Schulden im nächsten Jahr"',
49 'description': 'Haushaltsentwurf: "Keine neuen Schulden im nächsten Jahr"',
50 'duration': 64,
51 }
52 },
53 {
54 'url': 'http://www.br.de/radio/bayern1/service/team/videos/team-video-erdelt100.html',
55 'md5': 'dbab0aef2e047060ea7a21fc1ce1078a',
56 'info_dict': {
57 'id': '6ba73750-d405-45d3-861d-1ce8c524e059',
58 'ext': 'mp4',
59 'title': 'Umweltbewusster Häuslebauer',
60 'description': 'Uwe Erdelt: Umweltbewusster Häuslebauer',
61 'duration': 116,
62 }
63 },
64 {
65 'url': 'http://www.br.de/fernsehen/br-alpha/sendungen/kant-fuer-anfaenger/kritik-der-reinen-vernunft/kant-kritik-01-metaphysik100.html',
66 'md5': '23bca295f1650d698f94fc570977dae3',
67 'info_dict': {
68 'id': 'd982c9ce-8648-4753-b358-98abb8aec43d',
69 'ext': 'mp4',
70 'title': 'Folge 1 - Metaphysik',
71 'description': 'Kant für Anfänger: Folge 1 - Metaphysik',
72 'duration': 893,
73 'uploader': 'Eva Maria Steimle',
74 'upload_date': '20140117',
75 }
76 },
77 ]
78
79 def _real_extract(self, url):
80 mobj = re.match(self._VALID_URL, url)
81 display_id = mobj.group('id')
82 page = self._download_webpage(url, display_id)
83 xml_url = self._search_regex(
84 r"return BRavFramework\.register\(BRavFramework\('avPlayer_(?:[a-f0-9-]{36})'\)\.setup\({dataURL:'(/(?:[a-z0-9\-]+/)+[a-z0-9/~_.-]+)'}\)\);", page, 'XMLURL')
85 xml = self._download_xml(self._BASE_URL + xml_url, None)
86
87 medias = []
88
89 for xml_media in xml.findall('video') + xml.findall('audio'):
90 media = {
91 'id': xml_media.get('externalId'),
92 'title': xml_media.find('title').text,
93 'duration': parse_duration(xml_media.find('duration').text),
94 'formats': self._extract_formats(xml_media.find('assets')),
95 'thumbnails': self._extract_thumbnails(xml_media.find('teaserImage/variants')),
96 'description': ' '.join(xml_media.find('shareTitle').text.splitlines()),
97 'webpage_url': xml_media.find('permalink').text
98 }
99 if xml_media.find('author').text:
100 media['uploader'] = xml_media.find('author').text
101 if xml_media.find('broadcastDate').text:
102 media['upload_date'] = ''.join(reversed(xml_media.find('broadcastDate').text.split('.')))
103 medias.append(media)
104
105 if len(medias) > 1:
106 self._downloader.report_warning(
107 'found multiple medias; please '
108 'report this with the video URL to http://yt-dl.org/bug')
109 if not medias:
110 raise ExtractorError('No media entries found')
111 return medias[0]
112
113 def _extract_formats(self, assets):
114
115 def text_or_none(asset, tag):
116 elem = asset.find(tag)
117 return None if elem is None else elem.text
118
119 formats = [{
120 'url': text_or_none(asset, 'downloadUrl'),
121 'ext': text_or_none(asset, 'mediaType'),
122 'format_id': asset.get('type'),
123 'width': int_or_none(text_or_none(asset, 'frameWidth')),
124 'height': int_or_none(text_or_none(asset, 'frameHeight')),
125 'tbr': int_or_none(text_or_none(asset, 'bitrateVideo')),
126 'abr': int_or_none(text_or_none(asset, 'bitrateAudio')),
127 'vcodec': text_or_none(asset, 'codecVideo'),
128 'acodec': text_or_none(asset, 'codecAudio'),
129 'container': text_or_none(asset, 'mediaType'),
130 'filesize': int_or_none(text_or_none(asset, 'size')),
131 } for asset in assets.findall('asset')
132 if asset.find('downloadUrl') is not None]
133
134 self._sort_formats(formats)
135 return formats
136
137 def _extract_thumbnails(self, variants):
138 thumbnails = [{
139 'url': self._BASE_URL + variant.find('url').text,
140 'width': int_or_none(variant.find('width').text),
141 'height': int_or_none(variant.find('height').text),
142 } for variant in variants.findall('variant')]
143 thumbnails.sort(key=lambda x: x['width'] * x['height'], reverse=True)
144 return thumbnails