]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/rtbf.py
[cleanup] Upgrade syntax
[yt-dlp.git] / yt_dlp / extractor / rtbf.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 float_or_none,
7 int_or_none,
8 strip_or_none,
9 )
10
11
12 class RTBFIE(InfoExtractor):
13 _VALID_URL = r'''(?x)
14 https?://(?:www\.)?rtbf\.be/
15 (?:
16 video/[^?]+\?.*\bid=|
17 ouftivi/(?:[^/]+/)*[^?]+\?.*\bvideoId=|
18 auvio/[^/]+\?.*\b(?P<live>l)?id=
19 )(?P<id>\d+)'''
20 _TESTS = [{
21 'url': 'https://www.rtbf.be/video/detail_les-diables-au-coeur-episode-2?id=1921274',
22 'md5': '8c876a1cceeb6cf31b476461ade72384',
23 'info_dict': {
24 'id': '1921274',
25 'ext': 'mp4',
26 'title': 'Les Diables au coeur (épisode 2)',
27 'description': '(du 25/04/2014)',
28 'duration': 3099.54,
29 'upload_date': '20140425',
30 'timestamp': 1398456300,
31 }
32 }, {
33 # geo restricted
34 'url': 'http://www.rtbf.be/ouftivi/heros/detail_scooby-doo-mysteres-associes?id=1097&videoId=2057442',
35 'only_matching': True,
36 }, {
37 'url': 'http://www.rtbf.be/ouftivi/niouzz?videoId=2055858',
38 'only_matching': True,
39 }, {
40 'url': 'http://www.rtbf.be/auvio/detail_jeudi-en-prime-siegfried-bracke?id=2102996',
41 'only_matching': True,
42 }, {
43 # Live
44 'url': 'https://www.rtbf.be/auvio/direct_pure-fm?lid=134775',
45 'only_matching': True,
46 }, {
47 # Audio
48 'url': 'https://www.rtbf.be/auvio/detail_cinq-heures-cinema?id=2360811',
49 'only_matching': True,
50 }, {
51 # With Subtitle
52 'url': 'https://www.rtbf.be/auvio/detail_les-carnets-du-bourlingueur?id=2361588',
53 'only_matching': True,
54 }]
55 _IMAGE_HOST = 'http://ds1.ds.static.rtbf.be'
56 _PROVIDERS = {
57 'YOUTUBE': 'Youtube',
58 'DAILYMOTION': 'Dailymotion',
59 'VIMEO': 'Vimeo',
60 }
61 _QUALITIES = [
62 ('mobile', 'SD'),
63 ('web', 'MD'),
64 ('high', 'HD'),
65 ]
66
67 def _real_extract(self, url):
68 live, media_id = self._match_valid_url(url).groups()
69 embed_page = self._download_webpage(
70 'https://www.rtbf.be/auvio/embed/' + ('direct' if live else 'media'),
71 media_id, query={'id': media_id})
72 data = self._parse_json(self._html_search_regex(
73 r'data-media="([^"]+)"', embed_page, 'media data'), media_id)
74
75 error = data.get('error')
76 if error:
77 raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
78
79 provider = data.get('provider')
80 if provider in self._PROVIDERS:
81 return self.url_result(data['url'], self._PROVIDERS[provider])
82
83 title = data['title']
84 is_live = data.get('isLive')
85 height_re = r'-(\d+)p\.'
86 formats = []
87
88 m3u8_url = data.get('urlHlsAes128') or data.get('urlHls')
89 if m3u8_url:
90 formats.extend(self._extract_m3u8_formats(
91 m3u8_url, media_id, 'mp4', m3u8_id='hls', fatal=False))
92
93 fix_url = lambda x: x.replace('//rtbf-vod.', '//rtbf.') if '/geo/drm/' in x else x
94 http_url = data.get('url')
95 if formats and http_url and re.search(height_re, http_url):
96 http_url = fix_url(http_url)
97 for m3u8_f in formats[:]:
98 height = m3u8_f.get('height')
99 if not height:
100 continue
101 f = m3u8_f.copy()
102 del f['protocol']
103 f.update({
104 'format_id': m3u8_f['format_id'].replace('hls-', 'http-'),
105 'url': re.sub(height_re, '-%dp.' % height, http_url),
106 })
107 formats.append(f)
108 else:
109 sources = data.get('sources') or {}
110 for key, format_id in self._QUALITIES:
111 format_url = sources.get(key)
112 if not format_url:
113 continue
114 height = int_or_none(self._search_regex(
115 height_re, format_url, 'height', default=None))
116 formats.append({
117 'format_id': format_id,
118 'url': fix_url(format_url),
119 'height': height,
120 })
121
122 mpd_url = data.get('urlDash')
123 if mpd_url and (self.get_param('allow_unplayable_formats') or not data.get('drm')):
124 formats.extend(self._extract_mpd_formats(
125 mpd_url, media_id, mpd_id='dash', fatal=False))
126
127 audio_url = data.get('urlAudio')
128 if audio_url:
129 formats.append({
130 'format_id': 'audio',
131 'url': audio_url,
132 'vcodec': 'none',
133 })
134 self._sort_formats(formats)
135
136 subtitles = {}
137 for track in (data.get('tracks') or {}).values():
138 sub_url = track.get('url')
139 if not sub_url:
140 continue
141 subtitles.setdefault(track.get('lang') or 'fr', []).append({
142 'url': sub_url,
143 })
144
145 return {
146 'id': media_id,
147 'formats': formats,
148 'title': title,
149 'description': strip_or_none(data.get('description')),
150 'thumbnail': data.get('thumbnail'),
151 'duration': float_or_none(data.get('realDuration')),
152 'timestamp': int_or_none(data.get('liveFrom')),
153 'series': data.get('programLabel'),
154 'subtitles': subtitles,
155 'is_live': is_live,
156 }