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