Merge pull request #8063 from bpfoley/rteradio
[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     ExtractorError,
9     HEADRequest,
10     unified_strdate,
11     url_basename,
12     qualities,
13     int_or_none,
14 )
15
16
17 class CanalplusIE(InfoExtractor):
18     IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
19     _VALID_URL = r'https?://(?:www\.(?P<site>canalplus\.fr|piwiplus\.fr|d8\.tv|itele\.fr)/.*?/(?P<path>.*)|player\.canalplus\.fr/#/(?P<id>[0-9]+))'
20     _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
21     _SITE_ID_MAP = {
22         'canalplus.fr': 'cplus',
23         'piwiplus.fr': 'teletoon',
24         'd8.tv': 'd8',
25         'itele.fr': 'itele',
26     }
27
28     _TESTS = [{
29         'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1263092',
30         'md5': '12164a6f14ff6df8bd628e8ba9b10b78',
31         'info_dict': {
32             'id': '1263092',
33             'ext': 'mp4',
34             'title': 'Le Zapping - 13/05/15',
35             'description': 'md5:09738c0d06be4b5d06a0940edb0da73f',
36             'upload_date': '20150513',
37         },
38     }, {
39         'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
40         'info_dict': {
41             'id': '1108190',
42             'ext': 'flv',
43             'title': 'Le labyrinthe - Boing super ranger',
44             'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
45             'upload_date': '20140724',
46         },
47         'skip': 'Only works from France',
48     }, {
49         'url': 'http://www.d8.tv/d8-docs-mags/pid6589-d8-campagne-intime.html',
50         'info_dict': {
51             'id': '966289',
52             'ext': 'flv',
53             'title': 'Campagne intime - Documentaire exceptionnel',
54             'description': 'md5:d2643b799fb190846ae09c61e59a859f',
55             'upload_date': '20131108',
56         },
57         'skip': 'videos get deleted after a while',
58     }, {
59         'url': 'http://www.itele.fr/france/video/aubervilliers-un-lycee-en-colere-111559',
60         'md5': '38b8f7934def74f0d6f3ba6c036a5f82',
61         'info_dict': {
62             'id': '1213714',
63             'ext': 'mp4',
64             'title': 'Aubervilliers : un lycée en colère - Le 11/02/2015 à 06h45',
65             'description': 'md5:8216206ec53426ea6321321f3b3c16db',
66             'upload_date': '20150211',
67         },
68     }]
69
70     def _real_extract(self, url):
71         mobj = re.match(self._VALID_URL, url)
72         video_id = mobj.groupdict().get('id')
73
74         site_id = self._SITE_ID_MAP[mobj.group('site') or 'canal']
75
76         # Beware, some subclasses do not define an id group
77         display_id = url_basename(mobj.group('path'))
78
79         if video_id is None:
80             webpage = self._download_webpage(url, display_id)
81             video_id = self._search_regex(
82                 [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)', r'id=["\']canal_video_player(?P<id>\d+)'],
83                 webpage, 'video id', group='id')
84
85         info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
86         video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
87
88         if isinstance(video_data, list):
89             video_data = [video for video in video_data if video.get('ID') == video_id][0]
90         media = video_data['MEDIA']
91         infos = video_data['INFOS']
92
93         preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
94
95         fmt_url = next(iter(media.get('VIDEOS')))
96         if '/geo' in fmt_url.lower():
97             response = self._request_webpage(
98                 HEADRequest(fmt_url), video_id,
99                 'Checking if the video is georestricted')
100             if '/blocage' in response.geturl():
101                 raise ExtractorError(
102                     'The video is not available in your country',
103                     expected=True)
104
105         formats = []
106         for format_id, format_url in media['VIDEOS'].items():
107             if not format_url:
108                 continue
109             if format_id == 'HLS':
110                 formats.extend(self._extract_m3u8_formats(
111                     format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
112             elif format_id == 'HDS':
113                 formats.extend(self._extract_f4m_formats(
114                     format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
115             else:
116                 formats.append({
117                     # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
118                     'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
119                     'format_id': format_id,
120                     'preference': preference(format_id),
121                 })
122         self._sort_formats(formats)
123
124         thumbnails = [{
125             'id': image_id,
126             'url': image_url,
127         } for image_id, image_url in media.get('images', {}).items()]
128
129         titrage = infos['TITRAGE']
130
131         return {
132             'id': video_id,
133             'display_id': display_id,
134             'title': '%s - %s' % (titrage['TITRE'],
135                                   titrage['SOUS_TITRE']),
136             'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
137             'thumbnails': thumbnails,
138             'description': infos.get('DESCRIPTION'),
139             'duration': int_or_none(infos.get('DURATION')),
140             'view_count': int_or_none(infos.get('NB_VUES')),
141             'like_count': int_or_none(infos.get('NB_LIKES')),
142             'comment_count': int_or_none(infos.get('NB_COMMENTS')),
143             'formats': formats,
144         }