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