]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/rts.py
Start new release.
[youtubedl] / youtube_dl / extractor / rts.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .srgssr import SRGSSRIE
7 from ..compat import compat_str
8 from ..utils import (
9 int_or_none,
10 parse_duration,
11 parse_iso8601,
12 unescapeHTML,
13 determine_ext,
14 )
15
16
17 class RTSIE(SRGSSRIE):
18 IE_DESC = 'RTS.ch'
19 _VALID_URL = r'rts:(?P<rts_id>\d+)|https?://(?:.+?\.)?rts\.ch/(?:[^/]+/){2,}(?P<id>[0-9]+)-(?P<display_id>.+?)\.html'
20
21 _TESTS = [
22 {
23 'url': 'http://www.rts.ch/archives/tv/divers/3449373-les-enfants-terribles.html',
24 'md5': 'ff7f8450a90cf58dacb64e29707b4a8e',
25 'info_dict': {
26 'id': '3449373',
27 'display_id': 'les-enfants-terribles',
28 'ext': 'mp4',
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,
35 'thumbnail': r're:^https?://.*\.image',
36 'view_count': int,
37 },
38 },
39 {
40 'url': 'http://www.rts.ch/emissions/passe-moi-les-jumelles/5624067-entre-ciel-et-mer.html',
41 'info_dict': {
42 'id': '5624065',
43 'title': 'Passe-moi les jumelles',
44 },
45 'playlist_mincount': 4,
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',
49 'info_dict': {
50 'id': '5745975',
51 'display_id': '1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski',
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,
59 'thumbnail': r're:^https?://.*\.image',
60 'view_count': int,
61 },
62 'params': {
63 # m3u8 download
64 'skip_download': True,
65 },
66 'skip': 'Blocked outside Switzerland',
67 },
68 {
69 'url': 'http://www.rts.ch/video/info/journal-continu/5745356-londres-cachee-par-un-epais-smog.html',
70 'md5': '1bae984fe7b1f78e94abc74e802ed99f',
71 'info_dict': {
72 'id': '5745356',
73 'display_id': 'londres-cachee-par-un-epais-smog',
74 'ext': 'mp4',
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.',
78 'uploader': 'L\'actu en vidéo',
79 'upload_date': '20140403',
80 'timestamp': 1396537322,
81 'thumbnail': r're:^https?://.*\.image',
82 'view_count': int,
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',
90 'display_id': 'urban-hippie-de-damien-krisl-03-04-2014',
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 },
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,
107 },
108 {
109 'url': 'http://pages.rts.ch/emissions/passe-moi-les-jumelles/5624065-entre-ciel-et-mer.html',
110 'only_matching': True,
111 }
112 ]
113
114 def _real_extract(self, url):
115 m = re.match(self._VALID_URL, url)
116 media_id = m.group('rts_id') or m.group('id')
117 display_id = m.group('display_id') or media_id
118
119 def download_json(internal_id):
120 return self._download_json(
121 'http://www.rts.ch/a/%s.html?f=json/article' % internal_id,
122 display_id)
123
124 all_info = download_json(media_id)
125
126 # media_id extracted out of URL is not always a real id
127 if 'video' not in all_info and 'audio' not in all_info:
128 entries = []
129
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
142 videos = re.findall(
143 r'<article[^>]+class="content-item"[^>]*>\s*<a[^>]+data-video-urn="urn:([^"]+)"',
144 page)
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'))
154
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)
159
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
165 info = all_info['video']['JSONinfo'] if 'video' in all_info else all_info['audio']
166
167 title = info['title']
168
169 def extract_bitrate(url):
170 return int_or_none(self._search_regex(
171 r'-([0-9]+)k\.', url, 'bitrate', default=None))
172
173 formats = []
174 streams = info.get('streams', {})
175 for format_id, format_url in streams.items():
176 if format_id == 'hds_sd' and 'hds' in streams:
177 continue
178 if format_id == 'hls_sd' and 'hls' in streams:
179 continue
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))
190 else:
191 formats.append({
192 'format_id': format_id,
193 'url': format_url,
194 'tbr': extract_bitrate(format_url),
195 })
196
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 })
211
212 self._check_formats(formats, media_id)
213 self._sort_formats(formats)
214
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
219 return {
220 'id': media_id,
221 'display_id': display_id,
222 'formats': formats,
223 'title': title,
224 'description': info.get('intro'),
225 'duration': duration,
226 'view_count': int_or_none(info.get('plays')),
227 'uploader': info.get('programName'),
228 'timestamp': parse_iso8601(info.get('broadcast_date')),
229 'thumbnail': unescapeHTML(info.get('preview_image_url')),
230 }