[Rte] Improve extractor
[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]+)-.*?\.html'
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                 'ext': 'mp4',
27                 'duration': 1488,
28                 'title': 'Les Enfants Terribles',
29                 'description': 'France Pommier et sa soeur Luce Feral, les deux filles de ce groupe de 5.',
30                 'uploader': 'Divers',
31                 'upload_date': '19680921',
32                 'timestamp': -40280400,
33                 'thumbnail': 're:^https?://.*\.image'
34             },
35         },
36         {
37             'url': 'http://www.rts.ch/emissions/passe-moi-les-jumelles/5624067-entre-ciel-et-mer.html',
38             'md5': 'c148457a27bdc9e5b1ffe081a7a8337b',
39             'info_dict': {
40                 'id': '5624067',
41                 'ext': 'mp4',
42                 'duration': 3720,
43                 'title': 'Les yeux dans les cieux - Mon homard au Canada',
44                 'description': 'md5:d22ee46f5cc5bac0912e5a0c6d44a9f7',
45                 'uploader': 'Passe-moi les jumelles',
46                 'upload_date': '20140404',
47                 'timestamp': 1396635300,
48                 'thumbnail': 're:^https?://.*\.image'
49             },
50         },
51         {
52             'url': 'http://www.rts.ch/video/sport/hockey/5745975-1-2-kloten-fribourg-5-2-second-but-pour-gotteron-par-kwiatowski.html',
53             'md5': 'b4326fecd3eb64a458ba73c73e91299d',
54             'info_dict': {
55                 'id': '5745975',
56                 'ext': 'mp4',
57                 'duration': 48,
58                 'title': '1/2, Kloten - Fribourg (5-2): second but pour Gottéron par Kwiatowski',
59                 'description': 'Hockey - Playoff',
60                 'uploader': 'Hockey',
61                 'upload_date': '20140403',
62                 'timestamp': 1396556882,
63                 'thumbnail': 're:^https?://.*\.image'
64             },
65             'skip': 'Blocked outside Switzerland',
66         },
67         {
68             'url': 'http://www.rts.ch/video/info/journal-continu/5745356-londres-cachee-par-un-epais-smog.html',
69             'md5': '9bb06503773c07ce83d3cbd793cebb91',
70             'info_dict': {
71                 'id': '5745356',
72                 'ext': 'mp4',
73                 'duration': 33,
74                 'title': 'Londres cachée par un épais smog',
75                 'description': 'Un important voile de smog recouvre Londres depuis mercredi, provoqué par la pollution et du sable du Sahara.',
76                 'uploader': 'Le Journal en continu',
77                 'upload_date': '20140403',
78                 'timestamp': 1396537322,
79                 'thumbnail': 're:^https?://.*\.image'
80             },
81         },
82         {
83             'url': 'http://www.rts.ch/audio/couleur3/programmes/la-belle-video-de-stephane-laurenceau/5706148-urban-hippie-de-damien-krisl-03-04-2014.html',
84             'md5': 'dd8ef6a22dff163d063e2a52bc8adcae',
85             'info_dict': {
86                 'id': '5706148',
87                 'ext': 'mp3',
88                 'duration': 123,
89                 'title': '"Urban Hippie", de Damien Krisl',
90                 'description': 'Des Hippies super glam.',
91                 'upload_date': '20140403',
92                 'timestamp': 1396551600,
93             },
94         },
95     ]
96
97     def _real_extract(self, url):
98         m = re.match(self._VALID_URL, url)
99         video_id = m.group('id')
100
101         def download_json(internal_id):
102             return self._download_json(
103                 'http://www.rts.ch/a/%s.html?f=json/article' % internal_id,
104                 video_id)
105
106         all_info = download_json(video_id)
107
108         # video_id extracted out of URL is not always a real id
109         if 'video' not in all_info and 'audio' not in all_info:
110             page = self._download_webpage(url, video_id)
111             internal_id = self._html_search_regex(
112                 r'<(?:video|audio) data-id="([0-9]+)"', page,
113                 'internal video id')
114             all_info = download_json(internal_id)
115
116         info = all_info['video']['JSONinfo'] if 'video' in all_info else all_info['audio']
117
118         upload_timestamp = parse_iso8601(info.get('broadcast_date'))
119         duration = info.get('duration') or info.get('cutout') or info.get('cutduration')
120         if isinstance(duration, compat_str):
121             duration = parse_duration(duration)
122         view_count = info.get('plays')
123         thumbnail = unescapeHTML(info.get('preview_image_url'))
124
125         def extract_bitrate(url):
126             return int_or_none(self._search_regex(
127                 r'-([0-9]+)k\.', url, 'bitrate', default=None))
128
129         formats = [{
130             'format_id': fid,
131             'url': furl,
132             'tbr': extract_bitrate(furl),
133         } for fid, furl in info['streams'].items()]
134
135         if 'media' in info:
136             formats.extend([{
137                 'format_id': '%s-%sk' % (media['ext'], media['rate']),
138                 'url': 'http://download-video.rts.ch/%s' % media['url'],
139                 'tbr': media['rate'] or extract_bitrate(media['url']),
140             } for media in info['media'] if media.get('rate')])
141
142         self._sort_formats(formats)
143
144         return {
145             'id': video_id,
146             'formats': formats,
147             'title': info['title'],
148             'description': info.get('intro'),
149             'duration': duration,
150             'view_count': view_count,
151             'uploader': info.get('programName'),
152             'timestamp': upload_timestamp,
153             'thumbnail': thumbnail,
154         }