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