[francetvinfo] Add support for france3-regions and strip title (Closes #7673)
[youtube-dl] / youtube_dl / extractor / francetv.py
1 # encoding: utf-8
2
3 from __future__ import unicode_literals
4
5 import re
6 import json
7
8 from .common import InfoExtractor
9 from ..compat import compat_urlparse
10 from ..utils import (
11     clean_html,
12     ExtractorError,
13     int_or_none,
14     parse_duration,
15     determine_ext,
16 )
17 from .dailymotion import DailymotionCloudIE
18
19
20 class FranceTVBaseInfoExtractor(InfoExtractor):
21     def _extract_video(self, video_id, catalogue):
22         info = self._download_json(
23             'http://webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=%s&catalogue=%s'
24             % (video_id, catalogue),
25             video_id, 'Downloading video JSON')
26
27         if info.get('status') == 'NOK':
28             raise ExtractorError(
29                 '%s returned error: %s' % (self.IE_NAME, info['message']), expected=True)
30         allowed_countries = info['videos'][0].get('geoblocage')
31         if allowed_countries:
32             georestricted = True
33             geo_info = self._download_json(
34                 'http://geo.francetv.fr/ws/edgescape.json', video_id,
35                 'Downloading geo restriction info')
36             country = geo_info['reponse']['geo_info']['country_code']
37             if country not in allowed_countries:
38                 raise ExtractorError(
39                     'The video is not available from your location',
40                     expected=True)
41         else:
42             georestricted = False
43
44         formats = []
45         for video in info['videos']:
46             if video['statut'] != 'ONLINE':
47                 continue
48             video_url = video['url']
49             if not video_url:
50                 continue
51             format_id = video['format']
52             ext = determine_ext(video_url)
53             if ext == 'f4m':
54                 if georestricted:
55                     # See https://github.com/rg3/youtube-dl/issues/3963
56                     # m3u8 urls work fine
57                     continue
58                 f4m_url = self._download_webpage(
59                     'http://hdfauth.francetv.fr/esi/TA?url=%s' % video_url,
60                     video_id, 'Downloading f4m manifest token', fatal=False)
61                 if f4m_url:
62                     formats.extend(self._extract_f4m_formats(
63                         f4m_url + '&hdcore=3.7.0&plugin=aasp-3.7.0.39.44', video_id, 1, format_id))
64             elif ext == 'm3u8':
65                 formats.extend(self._extract_m3u8_formats(video_url, video_id, 'mp4', m3u8_id=format_id))
66             elif video_url.startswith('rtmp'):
67                 formats.append({
68                     'url': video_url,
69                     'format_id': 'rtmp-%s' % format_id,
70                     'ext': 'flv',
71                     'preference': 1,
72                 })
73             else:
74                 formats.append({
75                     'url': video_url,
76                     'format_id': format_id,
77                     'preference': -1,
78                 })
79         self._sort_formats(formats)
80
81         title = info['titre']
82         subtitle = info.get('sous_titre')
83         if subtitle:
84             title += ' - %s' % subtitle
85         title = title.strip()
86
87         subtitles = {}
88         subtitles_list = [{
89             'url': subformat['url'],
90             'ext': subformat.get('format'),
91         } for subformat in info.get('subtitles', []) if subformat.get('url')]
92         if subtitles_list:
93             subtitles['fr'] = subtitles_list
94
95         return {
96             'id': video_id,
97             'title': title,
98             'description': clean_html(info['synopsis']),
99             'thumbnail': compat_urlparse.urljoin('http://pluzz.francetv.fr', info['image']),
100             'duration': int_or_none(info.get('real_duration')) or parse_duration(info['duree']),
101             'timestamp': int_or_none(info['diffusion']['timestamp']),
102             'formats': formats,
103             'subtitles': subtitles,
104         }
105
106
107 class PluzzIE(FranceTVBaseInfoExtractor):
108     IE_NAME = 'pluzz.francetv.fr'
109     _VALID_URL = r'https?://(?:m\.)?pluzz\.francetv\.fr/videos/(?P<id>.+?)\.html'
110
111     # Can't use tests, videos expire in 7 days
112
113     def _real_extract(self, url):
114         display_id = self._match_id(url)
115
116         webpage = self._download_webpage(url, display_id)
117
118         video_id = self._html_search_meta(
119             'id_video', webpage, 'video id', default=None)
120         if not video_id:
121             video_id = self._search_regex(
122                 r'data-diffusion=["\'](\d+)', webpage, 'video id')
123
124         return self._extract_video(video_id, 'Pluzz')
125
126
127 class FranceTvInfoIE(FranceTVBaseInfoExtractor):
128     IE_NAME = 'francetvinfo.fr'
129     _VALID_URL = r'https?://(?:www|mobile|france3-regions)\.francetvinfo\.fr/.*/(?P<title>.+)\.html'
130
131     _TESTS = [{
132         'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html',
133         'info_dict': {
134             'id': '84981923',
135             'ext': 'flv',
136             'title': 'Soir 3',
137             'upload_date': '20130826',
138             'timestamp': 1377548400,
139             'subtitles': {
140                 'fr': 'mincount:2',
141             },
142         },
143     }, {
144         'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
145         'info_dict': {
146             'id': 'EV_20019',
147             'ext': 'mp4',
148             'title': 'Débat des candidats à la Commission européenne',
149             'description': 'Débat des candidats à la Commission européenne',
150         },
151         'params': {
152             'skip_download': 'HLS (reqires ffmpeg)'
153         },
154         'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.',
155     }, {
156         'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html',
157         'md5': 'f485bda6e185e7d15dbc69b72bae993e',
158         'info_dict': {
159             'id': '556e03339473995ee145930c',
160             'ext': 'mp4',
161             'title': 'Les entreprises familiales : le secret de la réussite',
162             'thumbnail': 're:^https?://.*\.jpe?g$',
163         }
164     }, {
165         'url': 'http://france3-regions.francetvinfo.fr/bretagne/cotes-d-armor/thalassa-echappee-breizh-ce-venredi-dans-les-cotes-d-armor-954961.html',
166         'md5': 'f485bda6e185e7d15dbc69b72bae993e',
167         'info_dict': {
168             'id': 'NI_657393',
169             'ext': 'flv',
170             'title': 'Olivier Monthus, réalisateur de "Bretagne, le choix de l’Armor"',
171             'description': 'md5:a3264114c9d29aeca11ced113c37b16c',
172             'thumbnail': 're:^https?://.*\.jpe?g$',
173             'timestamp': 1458300695,
174             'upload_date': '20160318',
175         },
176         'params': {
177             'skip_download': True,
178         },
179     }]
180
181     def _real_extract(self, url):
182         mobj = re.match(self._VALID_URL, url)
183         page_title = mobj.group('title')
184         webpage = self._download_webpage(url, page_title)
185
186         dmcloud_url = DailymotionCloudIE._extract_dmcloud_url(webpage)
187         if dmcloud_url:
188             return self.url_result(dmcloud_url, 'DailymotionCloud')
189
190         video_id, catalogue = self._search_regex(
191             (r'id-video=([^@]+@[^"]+)',
192              r'<a[^>]+href="(?:https?:)?//videos\.francetv\.fr/video/([^@]+@[^"]+)"'),
193             webpage, 'video id').split('@')
194         return self._extract_video(video_id, catalogue)
195
196
197 class FranceTVIE(FranceTVBaseInfoExtractor):
198     IE_NAME = 'francetv'
199     IE_DESC = 'France 2, 3, 4, 5 and Ô'
200     _VALID_URL = r'''(?x)
201                     https?://
202                         (?:
203                             (?:www\.)?france[2345o]\.fr/
204                                 (?:
205                                     emissions/[^/]+/(?:videos|diffusions)|
206                                     emission/[^/]+|
207                                     videos|
208                                     jt
209                                 )
210                             /|
211                             embed\.francetv\.fr/\?ue=
212                         )
213                         (?P<id>[^/?]+)
214                     '''
215
216     _TESTS = [
217         # france2
218         {
219             'url': 'http://www.france2.fr/emissions/13h15-le-samedi-le-dimanche/videos/75540104',
220             'md5': 'c03fc87cb85429ffd55df32b9fc05523',
221             'info_dict': {
222                 'id': '109169362',
223                 'ext': 'flv',
224                 'title': '13h15, le dimanche...',
225                 'description': 'md5:9a0932bb465f22d377a449be9d1a0ff7',
226                 'upload_date': '20140914',
227                 'timestamp': 1410693600,
228             },
229         },
230         # france3
231         {
232             'url': 'http://www.france3.fr/emissions/pieces-a-conviction/diffusions/13-11-2013_145575',
233             'md5': '679bb8f8921f8623bd658fa2f8364da0',
234             'info_dict': {
235                 'id': '000702326_CAPP_PicesconvictionExtrait313022013_120220131722_Au',
236                 'ext': 'mp4',
237                 'title': 'Le scandale du prix des médicaments',
238                 'description': 'md5:1384089fbee2f04fc6c9de025ee2e9ce',
239                 'upload_date': '20131113',
240                 'timestamp': 1384380000,
241             },
242         },
243         # france4
244         {
245             'url': 'http://www.france4.fr/emissions/hero-corp/videos/rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
246             'md5': 'a182bf8d2c43d88d46ec48fbdd260c1c',
247             'info_dict': {
248                 'id': 'rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
249                 'ext': 'mp4',
250                 'title': 'Hero Corp Making of - Extrait 1',
251                 'description': 'md5:c87d54871b1790679aec1197e73d650a',
252                 'upload_date': '20131106',
253                 'timestamp': 1383766500,
254             },
255         },
256         # france5
257         {
258             'url': 'http://www.france5.fr/emissions/c-a-dire/videos/quels_sont_les_enjeux_de_cette_rentree_politique__31-08-2015_908948?onglet=tous&page=1',
259             'md5': 'f6c577df3806e26471b3d21631241fd0',
260             'info_dict': {
261                 'id': '123327454',
262                 'ext': 'flv',
263                 'title': 'C à dire ?! - Quels sont les enjeux de cette rentrée politique ?',
264                 'description': 'md5:4a0d5cb5dce89d353522a84462bae5a4',
265                 'upload_date': '20150831',
266                 'timestamp': 1441035120,
267             },
268         },
269         # franceo
270         {
271             'url': 'http://www.franceo.fr/jt/info-soir/18-07-2015',
272             'md5': '47d5816d3b24351cdce512ad7ab31da8',
273             'info_dict': {
274                 'id': '125377621',
275                 'ext': 'flv',
276                 'title': 'Infô soir',
277                 'description': 'md5:01b8c6915a3d93d8bbbd692651714309',
278                 'upload_date': '20150718',
279                 'timestamp': 1437241200,
280                 'duration': 414,
281             },
282         },
283         {
284             # francetv embed
285             'url': 'http://embed.francetv.fr/?ue=8d7d3da1e3047c42ade5a5d7dfd3fc87',
286             'info_dict': {
287                 'id': 'EV_30231',
288                 'ext': 'flv',
289                 'title': 'Alcaline, le concert avec Calogero',
290                 'description': 'md5:61f08036dcc8f47e9cfc33aed08ffaff',
291                 'upload_date': '20150226',
292                 'timestamp': 1424989860,
293                 'duration': 5400,
294             },
295         },
296         {
297             'url': 'http://www.france4.fr/emission/highlander/diffusion-du-17-07-2015-04h05',
298             'only_matching': True,
299         },
300         {
301             'url': 'http://www.franceo.fr/videos/125377617',
302             'only_matching': True,
303         }
304     ]
305
306     def _real_extract(self, url):
307         video_id = self._match_id(url)
308         webpage = self._download_webpage(url, video_id)
309         video_id, catalogue = self._html_search_regex(
310             r'(?:href=|player\.setVideo\(\s*)"http://videos?\.francetv\.fr/video/([^@]+@[^"]+)"',
311             webpage, 'video ID').split('@')
312         return self._extract_video(video_id, catalogue)
313
314
315 class GenerationQuoiIE(InfoExtractor):
316     IE_NAME = 'france2.fr:generation-quoi'
317     _VALID_URL = r'https?://generation-quoi\.france2\.fr/portrait/(?P<id>[^/?#]+)'
318
319     _TEST = {
320         'url': 'http://generation-quoi.france2.fr/portrait/garde-a-vous',
321         'info_dict': {
322             'id': 'k7FJX8VBcvvLmX4wA5Q',
323             'ext': 'mp4',
324             'title': 'Génération Quoi - Garde à Vous',
325             'uploader': 'Génération Quoi',
326         },
327         'params': {
328             # It uses Dailymotion
329             'skip_download': True,
330         },
331     }
332
333     def _real_extract(self, url):
334         display_id = self._match_id(url)
335         info_url = compat_urlparse.urljoin(url, '/medias/video/%s.json' % display_id)
336         info_json = self._download_webpage(info_url, display_id)
337         info = json.loads(info_json)
338         return self.url_result('http://www.dailymotion.com/video/%s' % info['id'],
339                                ie='Dailymotion')
340
341
342 class CultureboxIE(FranceTVBaseInfoExtractor):
343     IE_NAME = 'culturebox.francetvinfo.fr'
344     _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)'
345
346     _TEST = {
347         'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511',
348         'md5': '9b88dc156781c4dbebd4c3e066e0b1d6',
349         'info_dict': {
350             'id': 'EV_50111',
351             'ext': 'flv',
352             'title': "Le Livre Vermeil de Montserrat à la Cathédrale d'Elne",
353             'description': 'md5:f8a4ad202e8fe533e2c493cc12e739d9',
354             'upload_date': '20150320',
355             'timestamp': 1426892400,
356             'duration': 2760.9,
357         },
358     }
359
360     def _real_extract(self, url):
361         mobj = re.match(self._VALID_URL, url)
362         name = mobj.group('name')
363
364         webpage = self._download_webpage(url, name)
365
366         if ">Ce live n'est plus disponible en replay<" in webpage:
367             raise ExtractorError('Video %s is not available' % name, expected=True)
368
369         video_id, catalogue = self._search_regex(
370             r'"http://videos\.francetv\.fr/video/([^@]+@[^"]+)"', webpage, 'video id').split('@')
371
372         return self._extract_video(video_id, catalogue)