Unify coding cookie
[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     ExtractorError,
10     HEADRequest,
11     unified_strdate,
12     qualities,
13     int_or_none,
14 )
15
16
17 class CanalplusIE(InfoExtractor):
18     IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
19     _VALID_URL = r'''(?x)
20                         https?://
21                             (?:
22                                 (?:
23                                     (?:(?:www|m)\.)?canalplus\.fr|
24                                     (?:www\.)?piwiplus\.fr|
25                                     (?:www\.)?d8\.tv|
26                                     (?:www\.)?c8\.fr|
27                                     (?:www\.)?d17\.tv|
28                                     (?:www\.)?itele\.fr
29                                 )/(?:(?:[^/]+/)*(?P<display_id>[^/?#&]+))?(?:\?.*\bvid=(?P<vid>\d+))?|
30                                 player\.canalplus\.fr/#/(?P<id>\d+)
31                             )
32
33                     '''
34     _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
35     _SITE_ID_MAP = {
36         'canalplus': 'cplus',
37         'piwiplus': 'teletoon',
38         'd8': 'd8',
39         'c8': 'd8',
40         'd17': 'd17',
41         'itele': 'itele',
42     }
43
44     _TESTS = [{
45         'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1192814',
46         'md5': '41f438a4904f7664b91b4ed0dec969dc',
47         'info_dict': {
48             'id': '1192814',
49             'ext': 'mp4',
50             'title': "L'Année du Zapping 2014 - L'Année du Zapping 2014",
51             'description': "Toute l'année 2014 dans un Zapping exceptionnel !",
52             'upload_date': '20150105',
53         },
54     }, {
55         'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
56         'info_dict': {
57             'id': '1108190',
58             'ext': 'flv',
59             'title': 'Le labyrinthe - Boing super ranger',
60             'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
61             'upload_date': '20140724',
62         },
63         'skip': 'Only works from France',
64     }, {
65         'url': 'http://www.d8.tv/d8-docs-mags/pid5198-d8-en-quete-d-actualite.html?vid=1390231',
66         'info_dict': {
67             'id': '1390231',
68             'ext': 'mp4',
69             'title': "Vacances pas chères : prix discount ou grosses dépenses ? - En quête d'actualité",
70             'description': 'md5:edb6cf1cb4a1e807b5dd089e1ac8bfc6',
71             'upload_date': '20160512',
72         },
73         'params': {
74             'skip_download': True,
75         },
76     }, {
77         'url': 'http://www.itele.fr/chroniques/invite-bruce-toussaint/thierry-solere-nicolas-sarkozy-officialisera-sa-candidature-a-la-primaire-quand-il-le-voudra-167224',
78         'info_dict': {
79             'id': '1398334',
80             'ext': 'mp4',
81             'title': "L'invité de Bruce Toussaint du 07/06/2016 - ",
82             'description': 'md5:40ac7c9ad0feaeb6f605bad986f61324',
83             'upload_date': '20160607',
84         },
85         'params': {
86             'skip_download': True,
87         },
88     }, {
89         'url': 'http://m.canalplus.fr/?vid=1398231',
90         'only_matching': True,
91     }, {
92         'url': 'http://www.d17.tv/emissions/pid8303-lolywood.html?vid=1397061',
93         'only_matching': True,
94     }]
95
96     def _real_extract(self, url):
97         mobj = re.match(self._VALID_URL, url)
98         video_id = mobj.groupdict().get('id') or mobj.groupdict().get('vid')
99
100         site_id = self._SITE_ID_MAP[compat_urllib_parse_urlparse(url).netloc.rsplit('.', 2)[-2]]
101
102         # Beware, some subclasses do not define an id group
103         display_id = mobj.group('display_id') or video_id
104
105         if video_id is None:
106             webpage = self._download_webpage(url, display_id)
107             video_id = self._search_regex(
108                 [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)', r'id=["\']canal_video_player(?P<id>\d+)'],
109                 webpage, 'video id', group='id')
110
111         info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
112         video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
113
114         if isinstance(video_data, list):
115             video_data = [video for video in video_data if video.get('ID') == video_id][0]
116         media = video_data['MEDIA']
117         infos = video_data['INFOS']
118
119         preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
120
121         fmt_url = next(iter(media.get('VIDEOS')))
122         if '/geo' in fmt_url.lower():
123             response = self._request_webpage(
124                 HEADRequest(fmt_url), video_id,
125                 'Checking if the video is georestricted')
126             if '/blocage' in response.geturl():
127                 raise ExtractorError(
128                     'The video is not available in your country',
129                     expected=True)
130
131         formats = []
132         for format_id, format_url in media['VIDEOS'].items():
133             if not format_url:
134                 continue
135             if format_id == 'HLS':
136                 formats.extend(self._extract_m3u8_formats(
137                     format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
138             elif format_id == 'HDS':
139                 formats.extend(self._extract_f4m_formats(
140                     format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
141             else:
142                 formats.append({
143                     # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
144                     'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
145                     'format_id': format_id,
146                     'preference': preference(format_id),
147                 })
148         self._sort_formats(formats)
149
150         thumbnails = [{
151             'id': image_id,
152             'url': image_url,
153         } for image_id, image_url in media.get('images', {}).items()]
154
155         titrage = infos['TITRAGE']
156
157         return {
158             'id': video_id,
159             'display_id': display_id,
160             'title': '%s - %s' % (titrage['TITRE'],
161                                   titrage['SOUS_TITRE']),
162             'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
163             'thumbnails': thumbnails,
164             'description': infos.get('DESCRIPTION'),
165             'duration': int_or_none(infos.get('DURATION')),
166             'view_count': int_or_none(infos.get('NB_VUES')),
167             'like_count': int_or_none(infos.get('NB_LIKES')),
168             'comment_count': int_or_none(infos.get('NB_COMMENTS')),
169             'formats': formats,
170         }