[francetv] Fix duration
[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 (
10     compat_urllib_parse_urlparse,
11     compat_urlparse,
12 )
13 from ..utils import (
14     clean_html,
15     ExtractorError,
16     int_or_none,
17     float_or_none,
18     parse_duration,
19 )
20
21
22 class FranceTVBaseInfoExtractor(InfoExtractor):
23     def _extract_video(self, video_id, catalogue):
24         info = self._download_json(
25             'http://webservices.francetelevisions.fr/tools/getInfosOeuvre/v2/?idDiffusion=%s&catalogue=%s'
26             % (video_id, catalogue),
27             video_id, 'Downloading video JSON')
28
29         if info.get('status') == 'NOK':
30             raise ExtractorError(
31                 '%s returned error: %s' % (self.IE_NAME, info['message']), expected=True)
32         allowed_countries = info['videos'][0].get('geoblocage')
33         if allowed_countries:
34             georestricted = True
35             geo_info = self._download_json(
36                 'http://geo.francetv.fr/ws/edgescape.json', video_id,
37                 'Downloading geo restriction info')
38             country = geo_info['reponse']['geo_info']['country_code']
39             if country not in allowed_countries:
40                 raise ExtractorError(
41                     'The video is not available from your location',
42                     expected=True)
43         else:
44             georestricted = False
45
46         formats = []
47         for video in info['videos']:
48             if video['statut'] != 'ONLINE':
49                 continue
50             video_url = video['url']
51             if not video_url:
52                 continue
53             format_id = video['format']
54             if video_url.endswith('.f4m'):
55                 if georestricted:
56                     # See https://github.com/rg3/youtube-dl/issues/3963
57                     # m3u8 urls work fine
58                     continue
59                 video_url_parsed = compat_urllib_parse_urlparse(video_url)
60                 f4m_url = self._download_webpage(
61                     'http://hdfauth.francetv.fr/esi/urltokengen2.html?url=%s' % video_url_parsed.path,
62                     video_id, 'Downloading f4m manifest token', fatal=False)
63                 if f4m_url:
64                     f4m_formats = self._extract_f4m_formats(f4m_url, video_id)
65                     for f4m_format in f4m_formats:
66                         f4m_format['preference'] = 1
67                     formats.extend(f4m_formats)
68             elif video_url.endswith('.m3u8'):
69                 formats.extend(self._extract_m3u8_formats(video_url, video_id, 'mp4'))
70             elif video_url.startswith('rtmp'):
71                 formats.append({
72                     'url': video_url,
73                     'format_id': 'rtmp-%s' % format_id,
74                     'ext': 'flv',
75                     'preference': 1,
76                 })
77             else:
78                 formats.append({
79                     'url': video_url,
80                     'format_id': format_id,
81                     'preference': -1,
82                 })
83         self._sort_formats(formats)
84
85         return {
86             'id': video_id,
87             'title': info['titre'],
88             'description': clean_html(info['synopsis']),
89             'thumbnail': compat_urlparse.urljoin('http://pluzz.francetv.fr', info['image']),
90             'duration': float_or_none(info.get('real_duration'), 1000) or parse_duration(info['duree']),
91             'timestamp': int_or_none(info['diffusion']['timestamp']),
92             'formats': formats,
93         }
94
95
96 class PluzzIE(FranceTVBaseInfoExtractor):
97     IE_NAME = 'pluzz.francetv.fr'
98     _VALID_URL = r'https?://pluzz\.francetv\.fr/videos/(.*?)\.html'
99
100     # Can't use tests, videos expire in 7 days
101
102     def _real_extract(self, url):
103         title = re.match(self._VALID_URL, url).group(1)
104         webpage = self._download_webpage(url, title)
105         video_id = self._search_regex(
106             r'data-diffusion="(\d+)"', webpage, 'ID')
107         return self._extract_video(video_id, 'Pluzz')
108
109
110 class FranceTvInfoIE(FranceTVBaseInfoExtractor):
111     IE_NAME = 'francetvinfo.fr'
112     _VALID_URL = r'https?://(?:www|mobile)\.francetvinfo\.fr/.*/(?P<title>.+)\.html'
113
114     _TESTS = [{
115         'url': 'http://www.francetvinfo.fr/replay-jt/france-3/soir-3/jt-grand-soir-3-lundi-26-aout-2013_393427.html',
116         'info_dict': {
117             'id': '84981923',
118             'ext': 'flv',
119             'title': 'Soir 3',
120             'upload_date': '20130826',
121             'timestamp': 1377548400,
122         },
123     }, {
124         'url': 'http://www.francetvinfo.fr/elections/europeennes/direct-europeennes-regardez-le-debat-entre-les-candidats-a-la-presidence-de-la-commission_600639.html',
125         'info_dict': {
126             'id': 'EV_20019',
127             'ext': 'mp4',
128             'title': 'Débat des candidats à la Commission européenne',
129             'description': 'Débat des candidats à la Commission européenne',
130         },
131         'params': {
132             'skip_download': 'HLS (reqires ffmpeg)'
133         },
134         'skip': 'Ce direct est terminé et sera disponible en rattrapage dans quelques minutes.',
135     }]
136
137     def _real_extract(self, url):
138         mobj = re.match(self._VALID_URL, url)
139         page_title = mobj.group('title')
140         webpage = self._download_webpage(url, page_title)
141         video_id, catalogue = self._search_regex(
142             r'id-video=([^@]+@[^"]+)', webpage, 'video id').split('@')
143         return self._extract_video(video_id, catalogue)
144
145
146 class FranceTVIE(FranceTVBaseInfoExtractor):
147     IE_NAME = 'francetv'
148     IE_DESC = 'France 2, 3, 4, 5 and Ô'
149     _VALID_URL = r'''(?x)https?://www\.france[2345o]\.fr/
150         (?:
151             emissions/.*?/(videos|emissions)/(?P<id>[^/?]+)
152         |   (emissions?|jt)/(?P<key>[^/?]+)
153         )'''
154
155     _TESTS = [
156         # france2
157         {
158             'url': 'http://www.france2.fr/emissions/13h15-le-samedi-le-dimanche/videos/75540104',
159             'md5': 'c03fc87cb85429ffd55df32b9fc05523',
160             'info_dict': {
161                 'id': '109169362',
162                 'ext': 'flv',
163                 'title': '13h15, le dimanche...',
164                 'description': 'md5:9a0932bb465f22d377a449be9d1a0ff7',
165                 'upload_date': '20140914',
166                 'timestamp': 1410693600,
167             },
168         },
169         # france3
170         {
171             'url': 'http://www.france3.fr/emissions/pieces-a-conviction/diffusions/13-11-2013_145575',
172             'md5': '679bb8f8921f8623bd658fa2f8364da0',
173             'info_dict': {
174                 'id': '000702326_CAPP_PicesconvictionExtrait313022013_120220131722_Au',
175                 'ext': 'mp4',
176                 'title': 'Le scandale du prix des médicaments',
177                 'description': 'md5:1384089fbee2f04fc6c9de025ee2e9ce',
178                 'upload_date': '20131113',
179                 'timestamp': 1384380000,
180             },
181         },
182         # france4
183         {
184             'url': 'http://www.france4.fr/emissions/hero-corp/videos/rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
185             'md5': 'a182bf8d2c43d88d46ec48fbdd260c1c',
186             'info_dict': {
187                 'id': 'rhozet_herocorp_bonus_1_20131106_1923_06112013172108_F4',
188                 'ext': 'mp4',
189                 'title': 'Hero Corp Making of - Extrait 1',
190                 'description': 'md5:c87d54871b1790679aec1197e73d650a',
191                 'upload_date': '20131106',
192                 'timestamp': 1383766500,
193             },
194         },
195         # france5
196         {
197             'url': 'http://www.france5.fr/emissions/c-a-dire/videos/92837968',
198             'md5': '78f0f4064f9074438e660785bbf2c5d9',
199             'info_dict': {
200                 'id': '108961659',
201                 'ext': 'flv',
202                 'title': 'C à dire ?!',
203                 'description': 'md5:1a4aeab476eb657bf57c4ff122129f81',
204                 'upload_date': '20140915',
205                 'timestamp': 1410795000,
206             },
207         },
208         # franceo
209         {
210             'url': 'http://www.franceo.fr/jt/info-afrique/04-12-2013',
211             'md5': '52f0bfe202848b15915a2f39aaa8981b',
212             'info_dict': {
213                 'id': '108634970',
214                 'ext': 'flv',
215                 'title': 'Infô Afrique',
216                 'description': 'md5:ebf346da789428841bee0fd2a935ea55',
217                 'upload_date': '20140915',
218                 'timestamp': 1410822000,
219             },
220         },
221     ]
222
223     def _real_extract(self, url):
224         mobj = re.match(self._VALID_URL, url)
225         webpage = self._download_webpage(url, mobj.group('key') or mobj.group('id'))
226         video_id, catalogue = self._html_search_regex(
227             r'href="http://videos\.francetv\.fr/video/([^@]+@[^"]+)"',
228             webpage, 'video ID').split('@')
229         return self._extract_video(video_id, catalogue)
230
231
232 class GenerationQuoiIE(InfoExtractor):
233     IE_NAME = 'france2.fr:generation-quoi'
234     _VALID_URL = r'https?://generation-quoi\.france2\.fr/portrait/(?P<id>[^/?#]+)'
235
236     _TEST = {
237         'url': 'http://generation-quoi.france2.fr/portrait/garde-a-vous',
238         'info_dict': {
239             'id': 'k7FJX8VBcvvLmX4wA5Q',
240             'ext': 'mp4',
241             'title': 'Génération Quoi - Garde à Vous',
242             'uploader': 'Génération Quoi',
243         },
244         'params': {
245             # It uses Dailymotion
246             'skip_download': True,
247         },
248     }
249
250     def _real_extract(self, url):
251         display_id = self._match_id(url)
252         info_url = compat_urlparse.urljoin(url, '/medias/video/%s.json' % display_id)
253         info_json = self._download_webpage(info_url, display_id)
254         info = json.loads(info_json)
255         return self.url_result('http://www.dailymotion.com/video/%s' % info['id'],
256                                ie='Dailymotion')
257
258
259 class CultureboxIE(FranceTVBaseInfoExtractor):
260     IE_NAME = 'culturebox.francetvinfo.fr'
261     _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)'
262
263     _TEST = {
264         'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511',
265         'info_dict': {
266             'id': 'EV_50111',
267             'ext': 'mp4',
268             'title': "Le Livre Vermeil de Montserrat à la Cathédrale d'Elne",
269             'description': 'md5:f8a4ad202e8fe533e2c493cc12e739d9',
270             'upload_date': '20150320',
271             'timestamp': 1426892400,
272             'duration': 2760.9,
273         },
274         'params': {
275             'skip_download': True,
276         },
277     }
278
279     def _real_extract(self, url):
280         mobj = re.match(self._VALID_URL, url)
281         name = mobj.group('name')
282
283         webpage = self._download_webpage(url, name)
284
285         if ">Ce live n'est plus disponible en replay<" in webpage:
286             raise ExtractorError('Video %s is not available' % name, expected=True)
287
288         video_id, catalogue = self._search_regex(
289             r'"http://videos\.francetv\.fr/video/([^@]+@[^"]+)"', webpage, 'video id').split('@')
290
291         return self._extract_video(video_id, catalogue)