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