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