[shahid] add default fallbacks for extracting api vars
[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'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]+))'
23
24     _TESTS = [
25         {
26             'url': 'http://www.rts.ch/archives/tv/divers/3449373-les-enfants-terribles.html',
27             'md5': '753b877968ad8afaeddccc374d4256a5',
28             'info_dict': {
29                 'id': '3449373',
30                 'display_id': 'les-enfants-terribles',
31                 'ext': 'mp4',
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,
38                 'thumbnail': 're:^https?://.*\.image',
39                 'view_count': int,
40             },
41         },
42         {
43             'url': 'http://www.rts.ch/emissions/passe-moi-les-jumelles/5624067-entre-ciel-et-mer.html',
44             'md5': 'c148457a27bdc9e5b1ffe081a7a8337b',
45             'info_dict': {
46                 'id': '5624067',
47                 'display_id': 'entre-ciel-et-mer',
48                 'ext': 'mp4',
49                 'duration': 3720,
50                 'title': 'Les yeux dans les cieux - Mon homard au Canada',
51                 'description': 'md5:d22ee46f5cc5bac0912e5a0c6d44a9f7',
52                 'uploader': 'Passe-moi les jumelles',
53                 'upload_date': '20140404',
54                 'timestamp': 1396635300,
55                 'thumbnail': 're:^https?://.*\.image',
56                 'view_count': int,
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',
64                 'display_id': '1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski',
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,
72                 'thumbnail': 're:^https?://.*\.image',
73                 'view_count': int,
74             },
75             'skip': 'Blocked outside Switzerland',
76         },
77         {
78             'url': 'http://www.rts.ch/video/info/journal-continu/5745356-londres-cachee-par-un-epais-smog.html',
79             'md5': '9bb06503773c07ce83d3cbd793cebb91',
80             'info_dict': {
81                 'id': '5745356',
82                 'display_id': 'londres-cachee-par-un-epais-smog',
83                 'ext': 'mp4',
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,
90                 'thumbnail': 're:^https?://.*\.image',
91                 'view_count': int,
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',
99                 'display_id': 'urban-hippie-de-damien-krisl-03-04-2014',
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         },
108         {
109             'url': 'http://www.rts.ch/play/tv/-/video/le-19h30?id=6348260',
110             'md5': '968777c8779e5aa2434be96c54e19743',
111             'info_dict': {
112                 'id': '6348260',
113                 'display_id': 'le-19h30',
114                 'ext': 'mp4',
115                 'duration': 1796,
116                 'title': 'Le 19h30',
117                 'description': '',
118                 'uploader': 'Le 19h30',
119                 'upload_date': '20141201',
120                 'timestamp': 1417458600,
121                 'thumbnail': 're:^https?://.*\.image',
122                 'view_count': int,
123             },
124         },
125         {
126             'url': 'http://www.rts.ch/play/tv/le-19h30/video/le-chantier-du-nouveau-parlement-vaudois-a-permis-une-trouvaille-historique?id=6348280',
127             'only_matching': True,
128         }
129     ]
130
131     def _real_extract(self, url):
132         m = re.match(self._VALID_URL, url)
133         video_id = m.group('id') or m.group('id_new')
134         display_id = m.group('display_id') or m.group('display_id_new')
135
136         def download_json(internal_id):
137             return self._download_json(
138                 'http://www.rts.ch/a/%s.html?f=json/article' % internal_id,
139                 display_id)
140
141         all_info = download_json(video_id)
142
143         # video_id extracted out of URL is not always a real id
144         if 'video' not in all_info and 'audio' not in all_info:
145             page = self._download_webpage(url, display_id)
146             internal_id = self._html_search_regex(
147                 r'<(?:video|audio) data-id="([0-9]+)"', page,
148                 'internal video id')
149             all_info = download_json(internal_id)
150
151         info = all_info['video']['JSONinfo'] if 'video' in all_info else all_info['audio']
152
153         upload_timestamp = parse_iso8601(info.get('broadcast_date'))
154         duration = info.get('duration') or info.get('cutout') or info.get('cutduration')
155         if isinstance(duration, compat_str):
156             duration = parse_duration(duration)
157         view_count = info.get('plays')
158         thumbnail = unescapeHTML(info.get('preview_image_url'))
159
160         def extract_bitrate(url):
161             return int_or_none(self._search_regex(
162                 r'-([0-9]+)k\.', url, 'bitrate', default=None))
163
164         formats = []
165         for format_id, format_url in info['streams'].items():
166             if format_url.endswith('.f4m'):
167                 token = self._download_xml(
168                     'http://tp.srgssr.ch/token/akahd.xml?stream=%s/*' % compat_urllib_parse_urlparse(format_url).path,
169                     video_id, 'Downloading %s token' % format_id)
170                 auth_params = xpath_text(token, './/authparams', 'auth params')
171                 if not auth_params:
172                     continue
173                 formats.extend(self._extract_f4m_formats(
174                     '%s?%s&hdcore=3.4.0&plugin=aasp-3.4.0.132.66' % (format_url, auth_params),
175                     video_id, f4m_id=format_id))
176             elif format_url.endswith('.m3u8'):
177                 formats.extend(self._extract_m3u8_formats(
178                     format_url, video_id, 'mp4', m3u8_id=format_id))
179             else:
180                 formats.append({
181                     'format_id': format_id,
182                     'url': format_url,
183                     'tbr': extract_bitrate(format_url),
184                 })
185
186         if 'media' in info:
187             formats.extend([{
188                 'format_id': '%s-%sk' % (media['ext'], media['rate']),
189                 'url': 'http://download-video.rts.ch/%s' % media['url'],
190                 'tbr': media['rate'] or extract_bitrate(media['url']),
191             } for media in info['media'] if media.get('rate')])
192
193         self._check_formats(formats, video_id)
194         self._sort_formats(formats)
195
196         return {
197             'id': video_id,
198             'display_id': display_id,
199             'formats': formats,
200             'title': info['title'],
201             'description': info.get('intro'),
202             'duration': duration,
203             'view_count': view_count,
204             'uploader': info.get('programName'),
205             'timestamp': upload_timestamp,
206             'thumbnail': thumbnail,
207         }