]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/rts.py
[youtube] Force `hl=en` for comments (#594)
[yt-dlp.git] / yt_dlp / extractor / rts.py
CommitLineData
f70daac1
PH
1# coding: utf-8
2from __future__ import unicode_literals
3
4import re
5
7db2897d 6from .srgssr import SRGSSRIE
68601ef3 7from ..compat import compat_str
f70daac1 8from ..utils import (
ec5e77c5 9 determine_ext,
f70daac1
PH
10 int_or_none,
11 parse_duration,
12 parse_iso8601,
13 unescapeHTML,
ec5e77c5 14 urljoin,
f70daac1
PH
15)
16
17
7db2897d 18class RTSIE(SRGSSRIE):
f70daac1 19 IE_DESC = 'RTS.ch'
68601ef3 20 _VALID_URL = r'rts:(?P<rts_id>\d+)|https?://(?:.+?\.)?rts\.ch/(?:[^/]+/){2,}(?P<id>[0-9]+)-(?P<display_id>.+?)\.html'
6344fa04
S
21
22 _TESTS = [
23 {
24 'url': 'http://www.rts.ch/archives/tv/divers/3449373-les-enfants-terribles.html',
ec5e77c5 25 'md5': '753b877968ad8afaeddccc374d4256a5',
6344fa04
S
26 'info_dict': {
27 'id': '3449373',
b1399a14 28 'display_id': 'les-enfants-terribles',
422f7c11 29 'ext': 'mp4',
6344fa04
S
30 'duration': 1488,
31 'title': 'Les Enfants Terribles',
32 'description': 'France Pommier et sa soeur Luce Feral, les deux filles de ce groupe de 5.',
33 'uploader': 'Divers',
34 'upload_date': '19680921',
35 'timestamp': -40280400,
ec85ded8 36 'thumbnail': r're:^https?://.*\.image',
b1399a14 37 'view_count': int,
6344fa04 38 },
ec5e77c5 39 'expected_warnings': ['Unable to download f4m manifest', 'Failed to download m3u8 information'],
6344fa04
S
40 },
41 {
42 'url': 'http://www.rts.ch/emissions/passe-moi-les-jumelles/5624067-entre-ciel-et-mer.html',
6344fa04 43 'info_dict': {
68601ef3
RA
44 'id': '5624065',
45 'title': 'Passe-moi les jumelles',
6344fa04 46 },
68601ef3 47 'playlist_mincount': 4,
6344fa04
S
48 },
49 {
50 'url': 'http://www.rts.ch/video/sport/hockey/5745975-1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski.html',
6344fa04
S
51 'info_dict': {
52 'id': '5745975',
b1399a14 53 'display_id': '1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski',
6344fa04
S
54 'ext': 'mp4',
55 'duration': 48,
56 'title': '1/2, Kloten - Fribourg (5-2): second but pour Gottéron par Kwiatowski',
57 'description': 'Hockey - Playoff',
58 'uploader': 'Hockey',
59 'upload_date': '20140403',
60 'timestamp': 1396556882,
ec85ded8 61 'thumbnail': r're:^https?://.*\.image',
b1399a14 62 'view_count': int,
6344fa04 63 },
68601ef3
RA
64 'params': {
65 # m3u8 download
66 'skip_download': True,
67 },
ec5e77c5 68 'expected_warnings': ['Unable to download f4m manifest', 'Failed to download m3u8 information'],
6344fa04 69 'skip': 'Blocked outside Switzerland',
f70daac1 70 },
6344fa04
S
71 {
72 'url': 'http://www.rts.ch/video/info/journal-continu/5745356-londres-cachee-par-un-epais-smog.html',
ec5e77c5 73 'md5': '9bb06503773c07ce83d3cbd793cebb91',
6344fa04
S
74 'info_dict': {
75 'id': '5745356',
b1399a14 76 'display_id': 'londres-cachee-par-un-epais-smog',
422f7c11 77 'ext': 'mp4',
6344fa04
S
78 'duration': 33,
79 'title': 'Londres cachée par un épais smog',
80 'description': 'Un important voile de smog recouvre Londres depuis mercredi, provoqué par la pollution et du sable du Sahara.',
68601ef3 81 'uploader': 'L\'actu en vidéo',
6344fa04
S
82 'upload_date': '20140403',
83 'timestamp': 1396537322,
ec85ded8 84 'thumbnail': r're:^https?://.*\.image',
b1399a14 85 'view_count': int,
6344fa04 86 },
ec5e77c5 87 'expected_warnings': ['Unable to download f4m manifest', 'Failed to download m3u8 information'],
6344fa04
S
88 },
89 {
90 'url': 'http://www.rts.ch/audio/couleur3/programmes/la-belle-video-de-stephane-laurenceau/5706148-urban-hippie-de-damien-krisl-03-04-2014.html',
91 'md5': 'dd8ef6a22dff163d063e2a52bc8adcae',
92 'info_dict': {
93 'id': '5706148',
b1399a14 94 'display_id': 'urban-hippie-de-damien-krisl-03-04-2014',
6344fa04
S
95 'ext': 'mp3',
96 'duration': 123,
97 'title': '"Urban Hippie", de Damien Krisl',
98 'description': 'Des Hippies super glam.',
99 'upload_date': '20140403',
100 'timestamp': 1396551600,
101 },
102 },
ba911137
S
103 {
104 # article with videos on rhs
105 'url': 'http://www.rts.ch/sport/hockey/6693917-hockey-davos-decroche-son-31e-titre-de-champion-de-suisse.html',
106 'info_dict': {
107 'id': '6693917',
108 'title': 'Hockey: Davos décroche son 31e titre de champion de Suisse',
109 },
110 'playlist_mincount': 5,
68601ef3
RA
111 },
112 {
113 'url': 'http://pages.rts.ch/emissions/passe-moi-les-jumelles/5624065-entre-ciel-et-mer.html',
114 'only_matching': True,
b1399a14 115 }
6344fa04 116 ]
f70daac1
PH
117
118 def _real_extract(self, url):
119 m = re.match(self._VALID_URL, url)
7db2897d 120 media_id = m.group('rts_id') or m.group('id')
121 display_id = m.group('display_id') or media_id
f70daac1 122
f8514f61 123 def download_json(internal_id):
6344fa04 124 return self._download_json(
f8514f61 125 'http://www.rts.ch/a/%s.html?f=json/article' % internal_id,
b1399a14 126 display_id)
6344fa04 127
7db2897d 128 all_info = download_json(media_id)
6344fa04 129
7db2897d 130 # media_id extracted out of URL is not always a real id
6344fa04 131 if 'video' not in all_info and 'audio' not in all_info:
68601ef3 132 entries = []
ba911137 133
68601ef3
RA
134 for item in all_info.get('items', []):
135 item_url = item.get('url')
136 if not item_url:
137 continue
138 entries.append(self.url_result(item_url, 'RTS'))
139
140 if not entries:
141 page, urlh = self._download_webpage_handle(url, display_id)
142 if re.match(self._VALID_URL, urlh.geturl()).group('id') != media_id:
143 return self.url_result(urlh.geturl(), 'RTS')
144
145 # article with videos on rhs
7db2897d 146 videos = re.findall(
68601ef3 147 r'<article[^>]+class="content-item"[^>]*>\s*<a[^>]+data-video-urn="urn:([^"]+)"',
7db2897d 148 page)
68601ef3
RA
149 if not videos:
150 videos = re.findall(
151 r'(?s)<iframe[^>]+class="srg-player"[^>]+src="[^"]+urn:([^"]+)"',
152 page)
153 if videos:
154 entries = [self.url_result('srgssr:%s' % video_urn, 'SRGSSR') for video_urn in videos]
155
156 if entries:
157 return self.playlist_result(entries, media_id, all_info.get('title'))
ba911137 158
f8514f61
PH
159 internal_id = self._html_search_regex(
160 r'<(?:video|audio) data-id="([0-9]+)"', page,
161 'internal video id')
162 all_info = download_json(internal_id)
6344fa04 163
7db2897d 164 media_type = 'video' if 'video' in all_info else 'audio'
165
166 # check for errors
ec5e77c5 167 self._get_media_data('rts', media_type, media_id)
7db2897d 168
6344fa04 169 info = all_info['video']['JSONinfo'] if 'video' in all_info else all_info['audio']
f70daac1 170
68601ef3 171 title = info['title']
6344fa04
S
172
173 def extract_bitrate(url):
174 return int_or_none(self._search_regex(
175 r'-([0-9]+)k\.', url, 'bitrate', default=None))
176
f13b88c6 177 formats = []
68601ef3
RA
178 streams = info.get('streams', {})
179 for format_id, format_url in streams.items():
180 if format_id == 'hds_sd' and 'hds' in streams:
7db2897d 181 continue
68601ef3 182 if format_id == 'hls_sd' and 'hls' in streams:
7db2897d 183 continue
68601ef3
RA
184 ext = determine_ext(format_url)
185 if ext in ('m3u8', 'f4m'):
186 format_url = self._get_tokenized_src(format_url, media_id, format_id)
187 if ext == 'f4m':
188 formats.extend(self._extract_f4m_formats(
189 format_url + ('?' if '?' not in format_url else '&') + 'hdcore=3.4.0',
190 media_id, f4m_id=format_id, fatal=False))
191 else:
192 formats.extend(self._extract_m3u8_formats(
193 format_url, media_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
f13b88c6
S
194 else:
195 formats.append({
196 'format_id': format_id,
197 'url': format_url,
198 'tbr': extract_bitrate(format_url),
199 })
6344fa04 200
ec5e77c5 201 download_base = 'http://rtsww%s-d.rts.ch/' % ('-a' if media_type == 'audio' else '')
68601ef3
RA
202 for media in info.get('media', []):
203 media_url = media.get('url')
204 if not media_url or re.match(r'https?://', media_url):
205 continue
206 rate = media.get('rate')
207 ext = media.get('ext') or determine_ext(media_url, 'mp4')
208 format_id = ext
209 if rate:
210 format_id += '-%dk' % rate
211 formats.append({
212 'format_id': format_id,
ec5e77c5 213 'url': urljoin(download_base, media_url),
68601ef3
RA
214 'tbr': rate or extract_bitrate(media_url),
215 })
6344fa04 216
7db2897d 217 self._check_formats(formats, media_id)
f70daac1
PH
218 self._sort_formats(formats)
219
68601ef3
RA
220 duration = info.get('duration') or info.get('cutout') or info.get('cutduration')
221 if isinstance(duration, compat_str):
222 duration = parse_duration(duration)
223
f70daac1 224 return {
7db2897d 225 'id': media_id,
b1399a14 226 'display_id': display_id,
f70daac1 227 'formats': formats,
68601ef3 228 'title': title,
f70daac1
PH
229 'description': info.get('intro'),
230 'duration': duration,
68601ef3 231 'view_count': int_or_none(info.get('plays')),
f70daac1 232 'uploader': info.get('programName'),
68601ef3
RA
233 'timestamp': parse_iso8601(info.get('broadcast_date')),
234 'thumbnail': unescapeHTML(info.get('preview_image_url')),
f70daac1 235 }