dc59a5e5c0da8c6e2001b411a03451604ca190f3
[youtube-dl] / youtube_dl / extractor / rts.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     parse_duration,
10     parse_iso8601,
11     unescapeHTML,
12     compat_str,
13 )
14
15
16 class RTSIE(InfoExtractor):
17     IE_DESC = 'RTS.ch'
18     _VALID_URL = r'https?://(?:www\.)?rts\.ch/(?:(?:[^/]+/){2,}(?P<id>[0-9]+)-(?P<display_id>.+?)\.html|play/tv/[^/]+/video/(?P<display_id_new>.+?)\?id=(?P<id_new>[0-9]+))'
19
20     _TESTS = [
21         {
22             'url': 'http://www.rts.ch/archives/tv/divers/3449373-les-enfants-terribles.html',
23             'md5': '753b877968ad8afaeddccc374d4256a5',
24             'info_dict': {
25                 'id': '3449373',
26                 'display_id': 'les-enfants-terribles',
27                 'ext': 'mp4',
28                 'duration': 1488,
29                 'title': 'Les Enfants Terribles',
30                 'description': 'France Pommier et sa soeur Luce Feral, les deux filles de ce groupe de 5.',
31                 'uploader': 'Divers',
32                 'upload_date': '19680921',
33                 'timestamp': -40280400,
34                 'thumbnail': 're:^https?://.*\.image',
35                 'view_count': int,
36             },
37         },
38         {
39             'url': 'http://www.rts.ch/emissions/passe-moi-les-jumelles/5624067-entre-ciel-et-mer.html',
40             'md5': 'c148457a27bdc9e5b1ffe081a7a8337b',
41             'info_dict': {
42                 'id': '5624067',
43                 'display_id': 'entre-ciel-et-mer',
44                 'ext': 'mp4',
45                 'duration': 3720,
46                 'title': 'Les yeux dans les cieux - Mon homard au Canada',
47                 'description': 'md5:d22ee46f5cc5bac0912e5a0c6d44a9f7',
48                 'uploader': 'Passe-moi les jumelles',
49                 'upload_date': '20140404',
50                 'timestamp': 1396635300,
51                 'thumbnail': 're:^https?://.*\.image',
52                 'view_count': int,
53             },
54         },
55         {
56             'url': 'http://www.rts.ch/video/sport/hockey/5745975-1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski.html',
57             'md5': 'b4326fecd3eb64a458ba73c73e91299d',
58             'info_dict': {
59                 'id': '5745975',
60                 'display_id': '1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski',
61                 'ext': 'mp4',
62                 'duration': 48,
63                 'title': '1/2, Kloten - Fribourg (5-2): second but pour Gottéron par Kwiatowski',
64                 'description': 'Hockey - Playoff',
65                 'uploader': 'Hockey',
66                 'upload_date': '20140403',
67                 'timestamp': 1396556882,
68                 'thumbnail': 're:^https?://.*\.image',
69                 'view_count': int,
70             },
71             'skip': 'Blocked outside Switzerland',
72         },
73         {
74             'url': 'http://www.rts.ch/video/info/journal-continu/5745356-londres-cachee-par-un-epais-smog.html',
75             'md5': '9bb06503773c07ce83d3cbd793cebb91',
76             'info_dict': {
77                 'id': '5745356',
78                 'display_id': 'londres-cachee-par-un-epais-smog',
79                 'ext': 'mp4',
80                 'duration': 33,
81                 'title': 'Londres cachée par un épais smog',
82                 'description': 'Un important voile de smog recouvre Londres depuis mercredi, provoqué par la pollution et du sable du Sahara.',
83                 'uploader': 'Le Journal en continu',
84                 'upload_date': '20140403',
85                 'timestamp': 1396537322,
86                 'thumbnail': 're:^https?://.*\.image',
87                 'view_count': int,
88             },
89         },
90         {
91             'url': 'http://www.rts.ch/audio/couleur3/programmes/la-belle-video-de-stephane-laurenceau/5706148-urban-hippie-de-damien-krisl-03-04-2014.html',
92             'md5': 'dd8ef6a22dff163d063e2a52bc8adcae',
93             'info_dict': {
94                 'id': '5706148',
95                 'display_id': 'urban-hippie-de-damien-krisl-03-04-2014',
96                 'ext': 'mp3',
97                 'duration': 123,
98                 'title': '"Urban Hippie", de Damien Krisl',
99                 'description': 'Des Hippies super glam.',
100                 'upload_date': '20140403',
101                 'timestamp': 1396551600,
102             },
103         },
104         {
105             'url': 'http://www.rts.ch/play/tv/-/video/le-19h30?id=6348260',
106             'md5': '968777c8779e5aa2434be96c54e19743',
107             'info_dict': {
108                 'id': '6348260',
109                 'display_id': 'le-19h30',
110                 'ext': 'mp4',
111                 'duration': 1796,
112                 'title': 'Le 19h30',
113                 'description': '',
114                 'uploader': 'Le 19h30',
115                 'upload_date': '20141201',
116                 'timestamp': 1417458600,
117                 'thumbnail': 're:^https?://.*\.image',
118                 'view_count': int,
119             },
120         },
121         {
122             'url': 'http://www.rts.ch/play/tv/le-19h30/video/le-chantier-du-nouveau-parlement-vaudois-a-permis-une-trouvaille-historique?id=6348280',
123             'only_matching': True,
124         }
125     ]
126
127     def _real_extract(self, url):
128         m = re.match(self._VALID_URL, url)
129         video_id = m.group('id') or m.group('id_new')
130         display_id = m.group('display_id') or m.group('display_id_new')
131
132         def download_json(internal_id):
133             return self._download_json(
134                 'http://www.rts.ch/a/%s.html?f=json/article' % internal_id,
135                 display_id)
136
137         all_info = download_json(video_id)
138
139         # video_id extracted out of URL is not always a real id
140         if 'video' not in all_info and 'audio' not in all_info:
141             page = self._download_webpage(url, display_id)
142             internal_id = self._html_search_regex(
143                 r'<(?:video|audio) data-id="([0-9]+)"', page,
144                 'internal video id')
145             all_info = download_json(internal_id)
146
147         info = all_info['video']['JSONinfo'] if 'video' in all_info else all_info['audio']
148
149         upload_timestamp = parse_iso8601(info.get('broadcast_date'))
150         duration = info.get('duration') or info.get('cutout') or info.get('cutduration')
151         if isinstance(duration, compat_str):
152             duration = parse_duration(duration)
153         view_count = info.get('plays')
154         thumbnail = unescapeHTML(info.get('preview_image_url'))
155
156         def extract_bitrate(url):
157             return int_or_none(self._search_regex(
158                 r'-([0-9]+)k\.', url, 'bitrate', default=None))
159
160         formats = [{
161             'format_id': fid,
162             'url': furl,
163             'tbr': extract_bitrate(furl),
164         } for fid, furl in info['streams'].items()]
165
166         if 'media' in info:
167             formats.extend([{
168                 'format_id': '%s-%sk' % (media['ext'], media['rate']),
169                 'url': 'http://download-video.rts.ch/%s' % media['url'],
170                 'tbr': media['rate'] or extract_bitrate(media['url']),
171             } for media in info['media'] if media.get('rate')])
172
173         self._sort_formats(formats)
174
175         return {
176             'id': video_id,
177             'display_id': display_id,
178             'formats': formats,
179             'title': info['title'],
180             'description': info.get('intro'),
181             'duration': duration,
182             'view_count': view_count,
183             'uploader': info.get('programName'),
184             'timestamp': upload_timestamp,
185             'thumbnail': thumbnail,
186         }