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