[canalplus] Prefer f4m and modernize (Closes #2749)
[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         'info_dict': {
18             'id': '922470',
19             'ext': 'flv',
20             'title': 'Zapping - 26/08/13',
21             'description': 'Le meilleur de toutes les chaînes, tous les jours.\nEmission du 26 août 2013',
22             'upload_date': '20130826',
23         },
24         'params': {
25             'skip_download': True,
26         },
27     }
28
29     def _real_extract(self, url):
30         mobj = re.match(self._VALID_URL, url)
31         video_id = mobj.group('id')
32
33         if video_id is None:
34             webpage = self._download_webpage(url, mobj.group('path'))
35             video_id = self._search_regex(r'<canal:player videoId="(\d+)"', webpage, 'video id')
36
37         info_url = self._VIDEO_INFO_TEMPLATE % video_id
38         doc = self._download_xml(info_url, video_id, 'Downloading video XML')
39
40         video_info = [video for video in doc if video.find('ID').text == video_id][0]
41         media = video_info.find('MEDIA')
42         infos = video_info.find('INFOS')
43
44         preferences = ['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD', 'HLS', 'HDS']
45
46         formats = [
47             {
48                 'url': fmt.text + '?hdcore=2.11.3' if fmt.tag == 'HDS' else fmt.text,
49                 'format_id': fmt.tag,
50                 'ext': 'mp4' if fmt.tag == 'HLS' else 'flv',
51                 'preference': preferences.index(fmt.tag) if fmt.tag in preferences else -1,
52             } for fmt in media.find('VIDEOS') if fmt.text
53         ]
54         self._sort_formats(formats)
55
56         return {
57             'id': video_id,
58             'title': '%s - %s' % (infos.find('TITRAGE/TITRE').text,
59                                   infos.find('TITRAGE/SOUS_TITRE').text),
60             'upload_date': unified_strdate(infos.find('PUBLICATION/DATE').text),
61             'thumbnail': media.find('IMAGES/GRAND').text,
62             'description': infos.find('DESCRIPTION').text,
63             'view_count': int(infos.find('NB_VUES').text),
64             'like_count': int(infos.find('NB_LIKES').text),
65             'comment_count': int(infos.find('NB_COMMENTS').text),
66             'formats': formats,
67         }