]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/francetv.py
Imported Upstream version 2015.06.04.1
[youtubedl] / 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 determine_ext,
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 video_url_parsed = compat_urllib_parse_urlparse(video_url)
62 f4m_url = self._download_webpage(
63 'http://hdfauth.francetv.fr/esi/urltokengen2.html?url=%s' % video_url_parsed.path,
64 video_id, 'Downloading f4m manifest token', fatal=False)
65 if f4m_url:
66 formats.extend(self._extract_f4m_formats(f4m_url, video_id, 1, format_id))
67 elif ext == 'm3u8':
68 formats.extend(self._extract_m3u8_formats(video_url, video_id, 'mp4', m3u8_id=format_id))
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': float_or_none(info.get('real_duration'), 1000) or 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<id>[^/?#]+)'
234
235 _TEST = {
236 'url': 'http://generation-quoi.france2.fr/portrait/garde-a-vous',
237 'info_dict': {
238 'id': 'k7FJX8VBcvvLmX4wA5Q',
239 'ext': 'mp4',
240 'title': 'Génération Quoi - Garde à Vous',
241 'uploader': 'Génération Quoi',
242 },
243 'params': {
244 # It uses Dailymotion
245 'skip_download': True,
246 },
247 }
248
249 def _real_extract(self, url):
250 display_id = self._match_id(url)
251 info_url = compat_urlparse.urljoin(url, '/medias/video/%s.json' % display_id)
252 info_json = self._download_webpage(info_url, display_id)
253 info = json.loads(info_json)
254 return self.url_result('http://www.dailymotion.com/video/%s' % info['id'],
255 ie='Dailymotion')
256
257
258 class CultureboxIE(FranceTVBaseInfoExtractor):
259 IE_NAME = 'culturebox.francetvinfo.fr'
260 _VALID_URL = r'https?://(?:m\.)?culturebox\.francetvinfo\.fr/(?P<name>.*?)(\?|$)'
261
262 _TEST = {
263 'url': 'http://culturebox.francetvinfo.fr/live/musique/musique-classique/le-livre-vermeil-de-montserrat-a-la-cathedrale-delne-214511',
264 'md5': '9b88dc156781c4dbebd4c3e066e0b1d6',
265 'info_dict': {
266 'id': 'EV_50111',
267 'ext': 'flv',
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 }
275
276 def _real_extract(self, url):
277 mobj = re.match(self._VALID_URL, url)
278 name = mobj.group('name')
279
280 webpage = self._download_webpage(url, name)
281
282 if ">Ce live n'est plus disponible en replay<" in webpage:
283 raise ExtractorError('Video %s is not available' % name, expected=True)
284
285 video_id, catalogue = self._search_regex(
286 r'"http://videos\.francetv\.fr/video/([^@]+@[^"]+)"', webpage, 'video id').split('@')
287
288 return self._extract_video(video_id, catalogue)