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