]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/br.py
Merge pull request #8827 from remitamine/safari
[youtube-dl] / 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     xpath_element,
12     xpath_text,
13 )
14
15
16 class BRIE(InfoExtractor):
17     IE_DESC = 'Bayerischer Rundfunk Mediathek'
18     _VALID_URL = r'(?P<base_url>https?://(?:www\.)?br(?:-klassik)?\.de)/(?:[a-z0-9\-_]+/)+(?P<id>[a-z0-9\-_]+)\.html'
19
20     _TESTS = [
21         {
22             'url': 'http://www.br.de/mediathek/video/sendungen/abendschau/betriebliche-altersvorsorge-104.html',
23             'md5': '83a0477cf0b8451027eb566d88b51106',
24             'info_dict': {
25                 'id': '48f656ef-287e-486f-be86-459122db22cc',
26                 'ext': 'mp4',
27                 'title': 'Die böse Überraschung',
28                 'description': 'md5:ce9ac81b466ce775b8018f6801b48ac9',
29                 'duration': 180,
30                 'uploader': 'Reinhard Weber',
31                 'upload_date': '20150422',
32             }
33         },
34         {
35             'url': 'http://www.br.de/nachrichten/oberbayern/inhalt/muenchner-polizeipraesident-schreiber-gestorben-100.html',
36             'md5': 'af3a3a4aa43ff0ce6a89504c67f427ef',
37             'info_dict': {
38                 'id': 'a4b83e34-123d-4b81-9f4e-c0d3121a4e05',
39                 'ext': 'flv',
40                 'title': 'Manfred Schreiber ist tot',
41                 'description': 'md5:b454d867f2a9fc524ebe88c3f5092d97',
42                 'duration': 26,
43             }
44         },
45         {
46             'url': 'https://www.br-klassik.de/audio/peeping-tom-premierenkritik-dance-festival-muenchen-100.html',
47             'md5': '8b5b27c0b090f3b35eac4ab3f7a73d3d',
48             'info_dict': {
49                 'id': '74c603c9-26d3-48bb-b85b-079aeed66e0b',
50                 'ext': 'aac',
51                 'title': 'Kurzweilig und sehr bewegend',
52                 'description': 'md5:0351996e3283d64adeb38ede91fac54e',
53                 'duration': 296,
54             }
55         },
56         {
57             'url': 'http://www.br.de/radio/bayern1/service/team/videos/team-video-erdelt100.html',
58             'md5': 'dbab0aef2e047060ea7a21fc1ce1078a',
59             'info_dict': {
60                 'id': '6ba73750-d405-45d3-861d-1ce8c524e059',
61                 'ext': 'mp4',
62                 'title': 'Umweltbewusster Häuslebauer',
63                 'description': 'md5:d52dae9792d00226348c1dbb13c9bae2',
64                 'duration': 116,
65             }
66         },
67         {
68             'url': 'http://www.br.de/fernsehen/br-alpha/sendungen/kant-fuer-anfaenger/kritik-der-reinen-vernunft/kant-kritik-01-metaphysik100.html',
69             'md5': '23bca295f1650d698f94fc570977dae3',
70             'info_dict': {
71                 'id': 'd982c9ce-8648-4753-b358-98abb8aec43d',
72                 'ext': 'mp4',
73                 'title': 'Folge 1 - Metaphysik',
74                 'description': 'md5:bb659990e9e59905c3d41e369db1fbe3',
75                 'duration': 893,
76                 'uploader': 'Eva Maria Steimle',
77                 'upload_date': '20140117',
78             }
79         },
80     ]
81
82     def _real_extract(self, url):
83         base_url, display_id = re.search(self._VALID_URL, url).groups()
84         page = self._download_webpage(url, display_id)
85         xml_url = self._search_regex(
86             r"return BRavFramework\.register\(BRavFramework\('avPlayer_(?:[a-f0-9-]{36})'\)\.setup\({dataURL:'(/(?:[a-z0-9\-]+/)+[a-z0-9/~_.-]+)'}\)\);", page, 'XMLURL')
87         xml = self._download_xml(base_url + xml_url, display_id)
88
89         medias = []
90
91         for xml_media in xml.findall('video') + xml.findall('audio'):
92             media_id = xml_media.get('externalId')
93             media = {
94                 'id': media_id,
95                 'title': xpath_text(xml_media, 'title', 'title', True),
96                 'duration': parse_duration(xpath_text(xml_media, 'duration')),
97                 'formats': self._extract_formats(xpath_element(
98                     xml_media, 'assets'), media_id),
99                 'thumbnails': self._extract_thumbnails(xpath_element(
100                     xml_media, 'teaserImage/variants'), base_url),
101                 'description': xpath_text(xml_media, 'desc'),
102                 'webpage_url': xpath_text(xml_media, 'permalink'),
103                 'uploader': xpath_text(xml_media, 'author'),
104             }
105             broadcast_date = xpath_text(xml_media, 'broadcastDate')
106             if broadcast_date:
107                 media['upload_date'] = ''.join(reversed(broadcast_date.split('.')))
108             medias.append(media)
109
110         if len(medias) > 1:
111             self._downloader.report_warning(
112                 'found multiple medias; please '
113                 'report this with the video URL to http://yt-dl.org/bug')
114         if not medias:
115             raise ExtractorError('No media entries found')
116         return medias[0]
117
118     def _extract_formats(self, assets, media_id):
119         formats = []
120         for asset in assets.findall('asset'):
121             format_url = xpath_text(asset, ['downloadUrl', 'url'])
122             asset_type = asset.get('type')
123             if asset_type == 'HDS':
124                 formats.extend(self._extract_f4m_formats(
125                     format_url + '?hdcore=3.2.0', media_id, f4m_id='hds', fatal=False))
126             elif asset_type == 'HLS':
127                 formats.extend(self._extract_m3u8_formats(
128                     format_url, media_id, 'mp4', 'm3u8_native', m3u8_id='hds', fatal=False))
129             else:
130                 format_info = {
131                     'ext': xpath_text(asset, 'mediaType'),
132                     'width': int_or_none(xpath_text(asset, 'frameWidth')),
133                     'height': int_or_none(xpath_text(asset, 'frameHeight')),
134                     'tbr': int_or_none(xpath_text(asset, 'bitrateVideo')),
135                     'abr': int_or_none(xpath_text(asset, 'bitrateAudio')),
136                     'vcodec': xpath_text(asset, 'codecVideo'),
137                     'acodec': xpath_text(asset, 'codecAudio'),
138                     'container': xpath_text(asset, 'mediaType'),
139                     'filesize': int_or_none(xpath_text(asset, 'size')),
140                 }
141                 format_url = self._proto_relative_url(format_url)
142                 if format_url:
143                     http_format_info = format_info.copy()
144                     http_format_info.update({
145                         'url': format_url,
146                         'format_id': 'http-%s' % asset_type,
147                     })
148                     formats.append(http_format_info)
149                 server_prefix = xpath_text(asset, 'serverPrefix')
150                 if server_prefix:
151                     rtmp_format_info = format_info.copy()
152                     rtmp_format_info.update({
153                         'url': server_prefix,
154                         'play_path': xpath_text(asset, 'fileName'),
155                         'format_id': 'rtmp-%s' % asset_type,
156                     })
157                     formats.append(rtmp_format_info)
158         self._sort_formats(formats)
159         return formats
160
161     def _extract_thumbnails(self, variants, base_url):
162         thumbnails = [{
163             'url': base_url + xpath_text(variant, 'url'),
164             'width': int_or_none(xpath_text(variant, 'width')),
165             'height': int_or_none(xpath_text(variant, 'height')),
166         } for variant in variants.findall('variant') if xpath_text(variant, 'url')]
167         thumbnails.sort(key=lambda x: x['width'] * x['height'], reverse=True)
168         return thumbnails