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