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