Merge remote-tracking branch 'xavierbeynon/master'
[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 ..utils import (
8     ExtractorError,
9     HEADRequest,
10     unified_strdate,
11     url_basename,
12     qualities,
13 )
14
15
16 class CanalplusIE(InfoExtractor):
17     IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
18     _VALID_URL = r'https?://(?:www\.(?P<site>canalplus\.fr|piwiplus\.fr|d8\.tv)/.*?/(?P<path>.*)|player\.canalplus\.fr/#/(?P<id>[0-9]+))'
19     _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s'
20     _SITE_ID_MAP = {
21         'canalplus.fr': 'cplus',
22         'piwiplus.fr': 'teletoon',
23         'd8.tv': 'd8',
24     }
25
26     _TESTS = [{
27         'url': 'http://www.canalplus.fr/c-infos-documentaires/pid1830-c-zapping.html?vid=922470',
28         'md5': '3db39fb48b9685438ecf33a1078023e4',
29         'info_dict': {
30             'id': '922470',
31             'ext': 'flv',
32             'title': 'Zapping - 26/08/13',
33             'description': 'Le meilleur de toutes les chaînes, tous les jours.\nEmission du 26 août 2013',
34             'upload_date': '20130826',
35         },
36     }, {
37         'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
38         'info_dict': {
39             'id': '1108190',
40             'ext': 'flv',
41             'title': 'Le labyrinthe - Boing super ranger',
42             'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
43             'upload_date': '20140724',
44         },
45         'skip': 'Only works from France',
46     }, {
47         'url': 'http://www.d8.tv/d8-docs-mags/pid6589-d8-campagne-intime.html',
48         'info_dict': {
49             'id': '966289',
50             'ext': 'flv',
51             'title': 'Campagne intime - Documentaire exceptionnel',
52             'description': 'md5:d2643b799fb190846ae09c61e59a859f',
53             'upload_date': '20131108',
54         },
55         'skip': 'videos get deleted after a while',
56     }]
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60         video_id = mobj.groupdict().get('id')
61
62         site_id = self._SITE_ID_MAP[mobj.group('site') or 'canal']
63
64         # Beware, some subclasses do not define an id group
65         display_id = url_basename(mobj.group('path'))
66
67         if video_id is None:
68             webpage = self._download_webpage(url, display_id)
69             video_id = self._search_regex(
70                 r'<canal:player[^>]+?videoId="(\d+)"', webpage, 'video id')
71
72         info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
73         doc = self._download_xml(info_url, video_id, 'Downloading video XML')
74
75         video_info = [video for video in doc if video.find('ID').text == video_id][0]
76         media = video_info.find('MEDIA')
77         infos = video_info.find('INFOS')
78
79         preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD', 'HLS', 'HDS'])
80
81         fmt_url = next(iter(media.find('VIDEOS'))).text
82         if '/geo' in fmt_url.lower():
83             response = self._request_webpage(
84                 HEADRequest(fmt_url), video_id,
85                 'Checking if the video is georestricted')
86             if '/blocage' in response.geturl():
87                 raise ExtractorError(
88                     'The video is not available in your country',
89                     expected=True)
90
91         formats = []
92         for fmt in media.find('VIDEOS'):
93             format_url = fmt.text
94             if not format_url:
95                 continue
96             format_id = fmt.tag
97             if format_id == 'HLS':
98                 hls_formats = self._extract_m3u8_formats(format_url, video_id, 'flv')
99                 for fmt in hls_formats:
100                     fmt['preference'] = preference(format_id)
101                 formats.extend(hls_formats)
102             elif format_id == 'HDS':
103                 hds_formats = self._extract_f4m_formats(format_url + '?hdcore=2.11.3', video_id)
104                 for fmt in hds_formats:
105                     fmt['preference'] = preference(format_id)
106                 formats.extend(hds_formats)
107             else:
108                 formats.append({
109                     'url': format_url,
110                     'format_id': format_id,
111                     'preference': preference(format_id),
112                 })
113         self._sort_formats(formats)
114
115         return {
116             'id': video_id,
117             'display_id': display_id,
118             'title': '%s - %s' % (infos.find('TITRAGE/TITRE').text,
119                                   infos.find('TITRAGE/SOUS_TITRE').text),
120             'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
121             'thumbnail': media.find('IMAGES/GRAND').text,
122             'description': infos.find('DESCRIPTION').text,
123             'view_count': int(infos.find('NB_VUES').text),
124             'like_count': int(infos.find('NB_LIKES').text),
125             'comment_count': int(infos.find('NB_COMMENTS').text),
126             'formats': formats,
127         }