[canalplus] Download the video in the test
[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 unified_strdate
8
9
10 class CanalplusIE(InfoExtractor):
11     _VALID_URL = r'https?://(www\.canalplus\.fr/.*?/(?P<path>.*)|player\.canalplus\.fr/#/(?P<id>\d+))'
12     _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/cplus/%s'
13     IE_NAME = 'canalplus.fr'
14
15     _TEST = {
16         'url': 'http://www.canalplus.fr/c-infos-documentaires/pid1830-c-zapping.html?vid=922470',
17         'md5': '60c29434a416a83c15dae2587d47027d',
18         'info_dict': {
19             'id': '922470',
20             'ext': 'flv',
21             'title': 'Zapping - 26/08/13',
22             'description': 'Le meilleur de toutes les chaînes, tous les jours.\nEmission du 26 août 2013',
23             'upload_date': '20130826',
24         },
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.group('id')
30
31         if video_id is None:
32             webpage = self._download_webpage(url, mobj.group('path'))
33             video_id = self._search_regex(r'<canal:player videoId="(\d+)"', webpage, 'video id')
34
35         info_url = self._VIDEO_INFO_TEMPLATE % video_id
36         doc = self._download_xml(info_url, video_id, 'Downloading video XML')
37
38         video_info = [video for video in doc if video.find('ID').text == video_id][0]
39         media = video_info.find('MEDIA')
40         infos = video_info.find('INFOS')
41
42         preferences = ['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD', 'HLS', 'HDS']
43
44         formats = [
45             {
46                 'url': fmt.text + '?hdcore=2.11.3' if fmt.tag == 'HDS' else fmt.text,
47                 'format_id': fmt.tag,
48                 'ext': 'mp4' if fmt.tag == 'HLS' else 'flv',
49                 'preference': preferences.index(fmt.tag) if fmt.tag in preferences else -1,
50             } for fmt in media.find('VIDEOS') if fmt.text
51         ]
52         self._sort_formats(formats)
53
54         return {
55             'id': video_id,
56             'title': '%s - %s' % (infos.find('TITRAGE/TITRE').text,
57                                   infos.find('TITRAGE/SOUS_TITRE').text),
58             'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
59             'thumbnail': media.find('IMAGES/GRAND').text,
60             'description': infos.find('DESCRIPTION').text,
61             'view_count': int(infos.find('NB_VUES').text),
62             'like_count': int(infos.find('NB_LIKES').text),
63             'comment_count': int(infos.find('NB_COMMENTS').text),
64             'formats': formats,
65         }