[telequebec] Add support for brightcove videos (closes #25833)
[youtube-dl] / youtube_dl / extractor / telequebec.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     int_or_none,
8     smuggle_url,
9     try_get,
10     unified_timestamp,
11 )
12
13
14 class TeleQuebecBaseIE(InfoExtractor):
15     BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/6150020952001/default_default/index.html?videoId=%s'
16
17     @staticmethod
18     def _limelight_result(media_id):
19         return {
20             '_type': 'url_transparent',
21             'url': smuggle_url(
22                 'limelight:media:' + media_id, {'geo_countries': ['CA']}),
23             'ie_key': 'LimelightMedia',
24         }
25
26     def _brightcove_result(self, brightcove_id):
27         return self.url_result(
28             smuggle_url(
29                 self.BRIGHTCOVE_URL_TEMPLATE % brightcove_id,
30                 {'geo_countries': ['CA']}),
31             'BrightcoveNew', brightcove_id)
32
33
34 class TeleQuebecIE(TeleQuebecBaseIE):
35     _VALID_URL = r'''(?x)
36                     https?://
37                         (?:
38                             zonevideo\.telequebec\.tv/media|
39                             coucou\.telequebec\.tv/videos
40                         )/(?P<id>\d+)
41                     '''
42     _TESTS = [{
43         # available till 01.01.2023
44         'url': 'http://zonevideo.telequebec.tv/media/37578/un-petit-choc-et-puis-repart/un-chef-a-la-cabane',
45         'info_dict': {
46             'id': '577116881b4b439084e6b1cf4ef8b1b3',
47             'ext': 'mp4',
48             'title': 'Un petit choc et puis repart!',
49             'description': 'md5:067bc84bd6afecad85e69d1000730907',
50         },
51         'params': {
52             'skip_download': True,
53         },
54     }, {
55         # no description
56         'url': 'http://zonevideo.telequebec.tv/media/30261',
57         'only_matching': True,
58     }, {
59         'url': 'https://coucou.telequebec.tv/videos/41788/idee-de-genie/l-heure-du-bain',
60         'only_matching': True,
61     }]
62
63     def _real_extract(self, url):
64         media_id = self._match_id(url)
65
66         media_data = self._download_json(
67             'https://mnmedias.api.telequebec.tv/api/v2/media/' + media_id,
68             media_id)['media']
69
70         if media_data['streamInfo']['source'] == 'Brightcove':
71             info = self._brightcove_result(media_data['streamInfo']['sourceId'])
72         elif media_data['streamInfo']['source'] == 'Limelight':
73             info = self._limelight_result(media_data['streamInfo']['sourceId'])
74         info.update({
75             'title': media_data.get('title'),
76             'description': try_get(
77                 media_data, lambda x: x['descriptions'][0]['text'], compat_str),
78             'duration': int_or_none(
79                 media_data.get('durationInMilliseconds'), 1000),
80         })
81         return info
82
83
84 class TeleQuebecSquatIE(InfoExtractor):
85     _VALID_URL = r'https://squat\.telequebec\.tv/videos/(?P<id>\d+)'
86     _TESTS = [{
87         'url': 'https://squat.telequebec.tv/videos/9314',
88         'info_dict': {
89             'id': 'd59ae78112d542e793d83cc9d3a5b530',
90             'ext': 'mp4',
91             'title': 'Poupeflekta',
92             'description': 'md5:2f0718f8d2f8fece1646ee25fb7bce75',
93             'duration': 1351,
94             'timestamp': 1569057600,
95             'upload_date': '20190921',
96             'series': 'Miraculous : Les Aventures de Ladybug et Chat Noir',
97             'season': 'Saison 3',
98             'season_number': 3,
99             'episode_number': 57,
100         },
101         'params': {
102             'skip_download': True,
103         },
104     }]
105
106     def _real_extract(self, url):
107         video_id = self._match_id(url)
108
109         video = self._download_json(
110             'https://squat.api.telequebec.tv/v1/videos/%s' % video_id,
111             video_id)
112
113         media_id = video['sourceId']
114
115         return {
116             '_type': 'url_transparent',
117             'url': 'http://zonevideo.telequebec.tv/media/%s' % media_id,
118             'ie_key': TeleQuebecIE.ie_key(),
119             'id': media_id,
120             'title': video.get('titre'),
121             'description': video.get('description'),
122             'timestamp': unified_timestamp(video.get('datePublication')),
123             'series': video.get('container'),
124             'season': video.get('saison'),
125             'season_number': int_or_none(video.get('noSaison')),
126             'episode_number': int_or_none(video.get('episode')),
127         }
128
129
130 class TeleQuebecEmissionIE(TeleQuebecBaseIE):
131     _VALID_URL = r'''(?x)
132                     https?://
133                         (?:
134                             [^/]+\.telequebec\.tv/emissions/|
135                             (?:www\.)?telequebec\.tv/
136                         )
137                         (?P<id>[^?#&]+)
138                     '''
139     _TESTS = [{
140         'url': 'http://lindicemcsween.telequebec.tv/emissions/100430013/des-soins-esthetiques-a-377-d-interets-annuels-ca-vous-tente',
141         'info_dict': {
142             'id': '66648a6aef914fe3badda25e81a4d50a',
143             'ext': 'mp4',
144             'title': "Des soins esthétiques à 377 % d'intérêts annuels, ça vous tente?",
145             'description': 'md5:369e0d55d0083f1fc9b71ffb640ea014',
146             'upload_date': '20171024',
147             'timestamp': 1508862118,
148         },
149         'params': {
150             'skip_download': True,
151         },
152     }, {
153         'url': 'http://bancpublic.telequebec.tv/emissions/emission-49/31986/jeunes-meres-sous-pression',
154         'only_matching': True,
155     }, {
156         'url': 'http://www.telequebec.tv/masha-et-michka/epi059masha-et-michka-3-053-078',
157         'only_matching': True,
158     }, {
159         'url': 'http://www.telequebec.tv/documentaire/bebes-sur-mesure/',
160         'only_matching': True,
161     }]
162
163     def _real_extract(self, url):
164         display_id = self._match_id(url)
165
166         webpage = self._download_webpage(url, display_id)
167
168         media_id = self._search_regex(
169             r'mediaUID\s*:\s*["\'][Ll]imelight_(?P<id>[a-z0-9]{32})', webpage,
170             'limelight id')
171
172         info = self._limelight_result(media_id)
173         info.update({
174             'title': self._og_search_title(webpage, default=None),
175             'description': self._og_search_description(webpage, default=None),
176         })
177         return info
178
179
180 class TeleQuebecLiveIE(InfoExtractor):
181     _VALID_URL = r'https?://zonevideo\.telequebec\.tv/(?P<id>endirect)'
182     _TEST = {
183         'url': 'http://zonevideo.telequebec.tv/endirect/',
184         'info_dict': {
185             'id': 'endirect',
186             'ext': 'mp4',
187             'title': 're:^Télé-Québec - En direct [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
188             'is_live': True,
189         },
190         'params': {
191             'skip_download': True,
192         },
193     }
194
195     def _real_extract(self, url):
196         video_id = self._match_id(url)
197
198         m3u8_url = None
199         webpage = self._download_webpage(
200             'https://player.telequebec.tv/Tq_VideoPlayer.js', video_id,
201             fatal=False)
202         if webpage:
203             m3u8_url = self._search_regex(
204                 r'm3U8Url\s*:\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
205                 'm3u8 url', default=None, group='url')
206         if not m3u8_url:
207             m3u8_url = 'https://teleqmmd.mmdlive.lldns.net/teleqmmd/f386e3b206814e1f8c8c1c71c0f8e748/manifest.m3u8'
208         formats = self._extract_m3u8_formats(
209             m3u8_url, video_id, 'mp4', m3u8_id='hls')
210         self._sort_formats(formats)
211
212         return {
213             'id': video_id,
214             'title': self._live_title('Télé-Québec - En direct'),
215             'is_live': True,
216             'formats': formats,
217         }