[canalplus] Add support for cstar.fr (#11990)
[youtube-dl] / youtube_dl / extractor / canalplus.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_parse_urlparse
8 from ..utils import (
9     dict_get,
10     ExtractorError,
11     HEADRequest,
12     int_or_none,
13     qualities,
14     remove_end,
15     unified_strdate,
16 )
17
18
19 class CanalplusIE(InfoExtractor):
20     IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
21     _VALID_URL = r'''(?x)
22                         https?://
23                             (?:
24                                 (?:
25                                     (?:(?:www|m)\.)?canalplus\.fr|
26                                     (?:www\.)?piwiplus\.fr|
27                                     (?:www\.)?d8\.tv|
28                                     (?:www\.)?c8\.fr|
29                                     (?:www\.)?d17\.tv|
30                                     (?:(?:football|www)\.)?cstar\.fr|
31                                     (?:www\.)?itele\.fr
32                                 )/(?:(?:[^/]+/)*(?P<display_id>[^/?#&]+))?(?:\?.*\bvid=(?P<vid>\d+))?|
33                                 player\.canalplus\.fr/#/(?P<id>\d+)
34                             )
35
36                     '''
37     _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
38     _SITE_ID_MAP = {
39         'canalplus': 'cplus',
40         'piwiplus': 'teletoon',
41         'd8': 'd8',
42         'c8': 'd8',
43         'd17': 'd17',
44         'cstar': 'd17',
45         'itele': 'itele',
46     }
47
48     _TESTS = [{
49         'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1192814',
50         'info_dict': {
51             'id': '1405510',
52             'display_id': 'pid1830-c-zapping',
53             'ext': 'mp4',
54             'title': 'Zapping - 02/07/2016',
55             'description': 'Le meilleur de toutes les chaînes, tous les jours',
56             'upload_date': '20160702',
57         },
58     }, {
59         'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
60         'info_dict': {
61             'id': '1108190',
62             'display_id': 'pid1405-le-labyrinthe-boing-super-ranger',
63             'ext': 'mp4',
64             'title': 'BOING SUPER RANGER - Ep : Le labyrinthe',
65             'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
66             'upload_date': '20140724',
67         },
68         'skip': 'Only works from France',
69     }, {
70         'url': 'http://www.c8.fr/c8-divertissement/ms-touche-pas-a-mon-poste/pid6318-videos-integrales.html',
71         'md5': '4b47b12b4ee43002626b97fad8fb1de5',
72         'info_dict': {
73             'id': '1420213',
74             'display_id': 'pid6318-videos-integrales',
75             'ext': 'mp4',
76             'title': 'TPMP ! Même le matin - Les 35H de Baba - 14/10/2016',
77             'description': 'md5:f96736c1b0ffaa96fd5b9e60ad871799',
78             'upload_date': '20161014',
79         },
80         'skip': 'Only works from France',
81     }, {
82         'url': 'http://www.itele.fr/chroniques/invite-michael-darmon/rachida-dati-nicolas-sarkozy-est-le-plus-en-phase-avec-les-inquietudes-des-francais-171510',
83         'info_dict': {
84             'id': '1420176',
85             'display_id': 'rachida-dati-nicolas-sarkozy-est-le-plus-en-phase-avec-les-inquietudes-des-francais-171510',
86             'ext': 'mp4',
87             'title': 'L\'invité de Michaël Darmon du 14/10/2016 - ',
88             'description': 'Chaque matin du lundi au vendredi, Michaël Darmon reçoit un invité politique à 8h25.',
89             'upload_date': '20161014',
90         },
91     }, {
92         'url': 'http://football.cstar.fr/cstar-minisite-foot/pid7566-feminines-videos.html?vid=1416769',
93         'info_dict': {
94             'id': '1416769',
95             'display_id': 'pid7566-feminines-videos',
96             'ext': 'mp4',
97             'title': 'France - Albanie : les temps forts de la soirée - 20/09/2016',
98             'description': 'md5:c3f30f2aaac294c1c969b3294de6904e',
99             'upload_date': '20160921',
100         },
101         'params': {
102             'skip_download': True,
103         },
104     }, {
105         'url': 'http://m.canalplus.fr/?vid=1398231',
106         'only_matching': True,
107     }, {
108         'url': 'http://www.d17.tv/emissions/pid8303-lolywood.html?vid=1397061',
109         'only_matching': True,
110     }]
111
112     def _real_extract(self, url):
113         mobj = re.match(self._VALID_URL, url)
114
115         site_id = self._SITE_ID_MAP[compat_urllib_parse_urlparse(url).netloc.rsplit('.', 2)[-2]]
116
117         # Beware, some subclasses do not define an id group
118         display_id = remove_end(dict_get(mobj.groupdict(), ('display_id', 'id', 'vid')), '.html')
119
120         webpage = self._download_webpage(url, display_id)
121         video_id = self._search_regex(
122             [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)',
123              r'id=["\']canal_video_player(?P<id>\d+)',
124              r'data-video=["\'](?P<id>\d+)'],
125             webpage, 'video id', default=mobj.group('vid'), group='id')
126
127         info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
128         video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
129
130         if isinstance(video_data, list):
131             video_data = [video for video in video_data if video.get('ID') == video_id][0]
132         media = video_data['MEDIA']
133         infos = video_data['INFOS']
134
135         preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
136
137         fmt_url = next(iter(media.get('VIDEOS')))
138         if '/geo' in fmt_url.lower():
139             response = self._request_webpage(
140                 HEADRequest(fmt_url), video_id,
141                 'Checking if the video is georestricted')
142             if '/blocage' in response.geturl():
143                 raise ExtractorError(
144                     'The video is not available in your country',
145                     expected=True)
146
147         formats = []
148         for format_id, format_url in media['VIDEOS'].items():
149             if not format_url:
150                 continue
151             if format_id == 'HLS':
152                 formats.extend(self._extract_m3u8_formats(
153                     format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
154             elif format_id == 'HDS':
155                 formats.extend(self._extract_f4m_formats(
156                     format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
157             else:
158                 formats.append({
159                     # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
160                     'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
161                     'format_id': format_id,
162                     'preference': preference(format_id),
163                 })
164         self._sort_formats(formats)
165
166         thumbnails = [{
167             'id': image_id,
168             'url': image_url,
169         } for image_id, image_url in media.get('images', {}).items()]
170
171         titrage = infos['TITRAGE']
172
173         return {
174             'id': video_id,
175             'display_id': display_id,
176             'title': '%s - %s' % (titrage['TITRE'],
177                                   titrage['SOUS_TITRE']),
178             'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
179             'thumbnails': thumbnails,
180             'description': infos.get('DESCRIPTION'),
181             'duration': int_or_none(infos.get('DURATION')),
182             'view_count': int_or_none(infos.get('NB_VUES')),
183             'like_count': int_or_none(infos.get('NB_LIKES')),
184             'comment_count': int_or_none(infos.get('NB_COMMENTS')),
185             'formats': formats,
186         }