Merge pull request #6291 from atomicdryad/pr-fixvice
[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         return {
82             'id': video_id,
83             'title': info['titre'],
84             'description': clean_html(info['synopsis']),
85             'thumbnail': compat_urlparse.urljoin('http://pluzz.francetv.fr', info['image']),
86             'duration': int_or_none(info.get('real_duration')) or parse_duration(info['duree']),
87             'timestamp': int_or_none(info['diffusion']['timestamp']),
88             'formats': formats,
89         }
90
91
92 class PluzzIE(FranceTVBaseInfoExtractor):
93     IE_NAME = 'pluzz.francetv.fr'
94     _VALID_URL = r'https?://pluzz\.francetv\.fr/videos/(.*?)\.html'
95
96     # Can't use tests, videos expire in 7 days
97
98     def _real_extract(self, url):
99         title = re.match(self._VALID_URL, url).group(1)
100         webpage = self._download_webpage(url, title)
101         video_id = self._search_regex(
102             r'data-diffusion="(\d+)"', webpage, 'ID')
103         return self._extract_video(video_id, 'Pluzz')
104
105
106 class FranceTvInfoIE(FranceTVBaseInfoExtractor):
107     IE_NAME = 'francetvinfo.fr'
108     _VALID_URL = r'https?://(?:www|mobile)\.francetvinfo\.fr/.*/(?P<title>.+)\.html'
109
110     _TESTS = [{
111         'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html',
112         'info_dict': {
113             'id': '84981923',
114             'ext': 'flv',
115             'title': 'Soir 3',
116             'upload_date': '20130826',
117             'timestamp': 1377548400,
118         },
119     }, {
120         'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
121         'info_dict': {
122             'id': 'EV_20019',
123             'ext': 'mp4',
124             'title': 'Débat des candidats à la Commission européenne',
125             'description': 'Débat des candidats à la Commission européenne',
126         },
127         'params': {
128             'skip_download': 'HLS (reqires ffmpeg)'
129         },
130         'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.',
131     }, {
132         'url': 'http://www.francetvinfo.fr/economie/entreprises/les-entreprises-familiales-le-secret-de-la-reussite_933271.html',
133         'md5': 'f485bda6e185e7d15dbc69b72bae993e',
134         'info_dict': {
135             'id': '556e03339473995ee145930c',
136             'ext': 'mp4',
137             'title': 'Les entreprises familiales : le secret de la réussite',
138             'thumbnail': 're:^https?://.*\.jpe?g$',
139         }
140     }]
141
142     def _real_extract(self, url):
143         mobj = re.match(self._VALID_URL, url)
144         page_title = mobj.group('title')
145         webpage = self._download_webpage(url, page_title)
146
147         dmcloud_url = DailymotionCloudIE._extract_dmcloud_url(webpage)
148         if dmcloud_url:
149             return self.url_result(dmcloud_url, 'DailymotionCloud')
150
151         video_id, catalogue = self._search_regex(
152             r'id-video=([^@]+@[^"]+)', webpage, 'video id').split('@')
153         return self._extract_video(video_id, catalogue)
154
155
156 class FranceTVIE(FranceTVBaseInfoExtractor):
157     IE_NAME = 'francetv'
158     IE_DESC = 'France 2, 3, 4, 5 and Ô'
159     _VALID_URL = r'''(?x)
160                     https?://
161                         (?:
162                             (?:www\.)?france[2345o]\.fr/
163                                 (?:
164                                     emissions/[^/]+/(?:videos|diffusions)?|
165                                     videos|
166                                     jt
167                                 )
168                             /|
169                             embed\.francetv\.fr/\?ue=
170                         )
171                         (?P<id>[^/?]+)
172                     '''
173
174     _TESTS = [
175         # france2
176         {
177             'url': 'http://www.france2.fr/emissions/13h15-le-samedi-le-dimanche/videos/75540104',
178             'md5': 'c03fc87cb85429ffd55df32b9fc05523',
179             'info_dict': {
180                 'id': '109169362',
181                 'ext': 'flv',
182                 'title': '13h15, le dimanche...',
183                 'description': 'md5:9a0932bb465f22d377a449be9d1a0ff7',
184                 'upload_date': '20140914',
185                 'timestamp': 1410693600,
186             },
187         },
188         # france3
189         {
190             'url': 'http://www.france3.fr/emissions/pieces-a-conviction/diffusions/13-11-2013_145575',
191             'md5': '679bb8f8921f8623bd658fa2f8364da0',
192             'info_dict': {
193                 'id': '000702326_CAPP_PicesconvictionExtrait313022013_120220131722_Au',
194                 'ext': 'mp4',
195                 'title': 'Le scandale du prix des médicaments',
196                 'description': 'md5:1384089fbee2f04fc6c9de025ee2e9ce',
197                 'upload_date': '20131113',
198                 'timestamp': 1384380000,
199             },
200         },
201         # france4
202         {
203             'url': 'http://www.france4.fr/emissions/hero-corp/videos/rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
204             'md5': 'a182bf8d2c43d88d46ec48fbdd260c1c',
205             'info_dict': {
206                 'id': 'rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
207                 'ext': 'mp4',
208                 'title': 'Hero Corp Making of - Extrait 1',
209                 'description': 'md5:c87d54871b1790679aec1197e73d650a',
210                 'upload_date': '20131106',
211                 'timestamp': 1383766500,
212             },
213         },
214         # france5
215         {
216             'url': 'http://www.france5.fr/emissions/c-a-dire/videos/92837968',
217             'md5': '78f0f4064f9074438e660785bbf2c5d9',
218             'info_dict': {
219                 'id': '108961659',
220                 'ext': 'flv',
221                 'title': 'C à dire ?!',
222                 'description': 'md5:1a4aeab476eb657bf57c4ff122129f81',
223                 'upload_date': '20140915',
224                 'timestamp': 1410795000,
225             },
226         },
227         # franceo
228         {
229             'url': 'http://www.franceo.fr/jt/info-soir/18-07-2015',
230             'md5': '47d5816d3b24351cdce512ad7ab31da8',
231             'info_dict': {
232                 'id': '125377621',
233                 'ext': 'flv',
234                 'title': 'Infô soir',
235                 'description': 'md5:01b8c6915a3d93d8bbbd692651714309',
236                 'upload_date': '20150718',
237                 'timestamp': 1437241200,
238                 'duration': 414,
239             },
240         },
241         {
242             # francetv embed
243             'url': 'http://embed.francetv.fr/?ue=8d7d3da1e3047c42ade5a5d7dfd3fc87',
244             'info_dict': {
245                 'id': 'EV_30231',
246                 'ext': 'flv',
247                 'title': 'Alcaline, le concert avec Calogero',
248                 'description': 'md5:61f08036dcc8f47e9cfc33aed08ffaff',
249                 'upload_date': '20150226',
250                 'timestamp': 1424989860,
251                 'duration': 5400,
252             },
253         },
254         {
255             'url': 'http://www.france4.fr/emission/highlander/diffusion-du-17-07-2015-04h05',
256             'only_matching': True,
257         },
258         {
259             'url': 'http://www.franceo.fr/videos/125377617',
260             'only_matching': True,
261         }
262     ]
263
264     def _real_extract(self, url):
265         video_id = self._match_id(url)
266         webpage = self._download_webpage(url, video_id)
267         video_id, catalogue = self._html_search_regex(
268             r'href="http://videos?\.francetv\.fr/video/([^@]+@[^"]+)"',
269             webpage, 'video ID').split('@')
270         return self._extract_video(video_id, catalogue)
271
272
273 class GenerationQuoiIE(InfoExtractor):
274     IE_NAME = 'france2.fr:generation-quoi'
275     _VALID_URL = r'https?://generation-quoi\.france2\.fr/portrait/(?P<id>[^/?#]+)'
276
277     _TEST = {
278         'url': 'http://generation-quoi.france2.fr/portrait/garde-a-vous',
279         'info_dict': {
280             'id': 'k7FJX8VBcvvLmX4wA5Q',
281             'ext': 'mp4',
282             'title': 'Génération Quoi - Garde à Vous',
283             'uploader': 'Génération Quoi',
284         },
285         'params': {
286             # It uses Dailymotion
287             'skip_download': True,
288         },
289     }
290
291     def _real_extract(self, url):
292         display_id = self._match_id(url)
293         info_url = compat_urlparse.urljoin(url, '/medias/video/%s.json' % display_id)
294         info_json = self._download_webpage(info_url, display_id)
295         info = json.loads(info_json)
296         return self.url_result('http://www.dailymotion.com/video/%s' % info['id'],
297                                ie='Dailymotion')
298
299
300 class CultureboxIE(FranceTVBaseInfoExtractor):
301     IE_NAME = 'culturebox.francetvinfo.fr'
302     _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)'
303
304     _TEST = {
305         'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511',
306         'md5': '9b88dc156781c4dbebd4c3e066e0b1d6',
307         'info_dict': {
308             'id': 'EV_50111',
309             'ext': 'flv',
310             'title': "Le Livre Vermeil de Montserrat à la Cathédrale d'Elne",
311             'description': 'md5:f8a4ad202e8fe533e2c493cc12e739d9',
312             'upload_date': '20150320',
313             'timestamp': 1426892400,
314             'duration': 2760.9,
315         },
316     }
317
318     def _real_extract(self, url):
319         mobj = re.match(self._VALID_URL, url)
320         name = mobj.group('name')
321
322         webpage = self._download_webpage(url, name)
323
324         if ">Ce live n'est plus disponible en replay<" in webpage:
325             raise ExtractorError('Video %s is not available' % name, expected=True)
326
327         video_id, catalogue = self._search_regex(
328             r'"http://videos\.francetv\.fr/video/([^@]+@[^"]+)"', webpage, 'video id').split('@')
329
330         return self._extract_video(video_id, catalogue)