Merge pull request #5695 from blissland/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|itele\.fr)/.*?/(?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         'itele.fr': 'itele',
25     }
26
27     _TESTS = [{
28         'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1263092',
29         'info_dict': {
30             'id': '1263092',
31             'ext': 'flv',
32             'title': 'Le Zapping - 13/05/15',
33             'description': 'md5:09738c0d06be4b5d06a0940edb0da73f',
34             'upload_date': '20150513',
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         'url': 'http://www.itele.fr/france/video/aubervilliers-un-lycee-en-colere-111559',
58         'info_dict': {
59             'id': '1213714',
60             'ext': 'flv',
61             'title': 'Aubervilliers : un lycée en colère - Le 11/02/2015 à 06h45',
62             'description': 'md5:8216206ec53426ea6321321f3b3c16db',
63             'upload_date': '20150211',
64         },
65     }]
66
67     def _real_extract(self, url):
68         mobj = re.match(self._VALID_URL, url)
69         video_id = mobj.groupdict().get('id')
70
71         site_id = self._SITE_ID_MAP[mobj.group('site') or 'canal']
72
73         # Beware, some subclasses do not define an id group
74         display_id = url_basename(mobj.group('path'))
75
76         if video_id is None:
77             webpage = self._download_webpage(url, display_id)
78             video_id = self._search_regex(
79                 r'<canal:player[^>]+?videoId="(\d+)"', webpage, 'video id')
80
81         info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
82         doc = self._download_xml(info_url, video_id, 'Downloading video XML')
83
84         video_info = [video for video in doc if video.find('ID').text == video_id][0]
85         media = video_info.find('MEDIA')
86         infos = video_info.find('INFOS')
87
88         preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD', 'HLS', 'HDS'])
89
90         fmt_url = next(iter(media.find('VIDEOS'))).text
91         if '/geo' in fmt_url.lower():
92             response = self._request_webpage(
93                 HEADRequest(fmt_url), video_id,
94                 'Checking if the video is georestricted')
95             if '/blocage' in response.geturl():
96                 raise ExtractorError(
97                     'The video is not available in your country',
98                     expected=True)
99
100         formats = []
101         for fmt in media.find('VIDEOS'):
102             format_url = fmt.text
103             if not format_url:
104                 continue
105             format_id = fmt.tag
106             if format_id == 'HLS':
107                 hls_formats = self._extract_m3u8_formats(format_url, video_id, 'flv')
108                 for fmt in hls_formats:
109                     fmt['preference'] = preference(format_id)
110                 formats.extend(hls_formats)
111             elif format_id == 'HDS':
112                 hds_formats = self._extract_f4m_formats(format_url + '?hdcore=2.11.3', video_id)
113                 for fmt in hds_formats:
114                     fmt['preference'] = preference(format_id)
115                 formats.extend(hds_formats)
116             else:
117                 formats.append({
118                     'url': format_url,
119                     'format_id': format_id,
120                     'preference': preference(format_id),
121                 })
122         self._sort_formats(formats)
123
124         return {
125             'id': video_id,
126             'display_id': display_id,
127             'title': '%s - %s' % (infos.find('TITRAGE/TITRE').text,
128                                   infos.find('TITRAGE/SOUS_TITRE').text),
129             'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
130             'thumbnail': media.find('IMAGES/GRAND').text,
131             'description': infos.find('DESCRIPTION').text,
132             'view_count': int(infos.find('NB_VUES').text),
133             'like_count': int(infos.find('NB_LIKES').text),
134             'comment_count': int(infos.find('NB_COMMENTS').text),
135             'formats': formats,
136         }