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