Merge remote-tracking branch 'rbrito/swap-dimensions'
[youtube-dl] / youtube_dl / extractor / canalplus.py
1 # encoding: utf-8
2 import re
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import unified_strdate
7
8
9 class CanalplusIE(InfoExtractor):
10     _VALID_URL = r'https?://(www\.canalplus\.fr/.*?/(?P<path>.*)|player\.canalplus\.fr/#/(?P<id>\d+))'
11     _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/cplus/%s'
12     IE_NAME = u'canalplus.fr'
13
14     _TEST = {
15         u'url': u'http://www.canalplus.fr/c-infos-documentaires/pid1830-c-zapping.html?vid=922470',
16         u'file': u'922470.flv',
17         u'info_dict': {
18             u'title': u'Zapping - 26/08/13',
19             u'description': u'Le meilleur de toutes les chaînes, tous les jours.\nEmission du 26 août 2013',
20             u'upload_date': u'20130826',
21         },
22         u'params': {
23             u'skip_download': True,
24         },
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.groupdict().get('id')
30         if video_id is None:
31             webpage = self._download_webpage(url, mobj.group('path'))
32             video_id = self._search_regex(r'videoId = "(\d+)";', webpage, u'video id')
33         info_url = self._VIDEO_INFO_TEMPLATE % video_id
34         info_page = self._download_webpage(info_url,video_id, 
35                                            u'Downloading video info')
36
37         self.report_extraction(video_id)
38         doc = xml.etree.ElementTree.fromstring(info_page.encode('utf-8'))
39         video_info = [video for video in doc if video.find('ID').text == video_id][0]
40         infos = video_info.find('INFOS')
41         media = video_info.find('MEDIA')
42         formats = [media.find('VIDEOS/%s' % format)
43             for format in ['BAS_DEBIT', 'HAUT_DEBIT', 'HD']]
44         video_url = [format.text for format in formats if format is not None][-1]
45
46         return {'id': video_id,
47                 'title': u'%s - %s' % (infos.find('TITRAGE/TITRE').text,
48                                        infos.find('TITRAGE/SOUS_TITRE').text),
49                 'url': video_url,
50                 'ext': 'flv',
51                 'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
52                 'thumbnail': media.find('IMAGES/GRAND').text,
53                 'description': infos.find('DESCRIPTION').text,
54                 'view_count': int(infos.find('NB_VUES').text),
55                 }