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