]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/canalplus.py
release 2017.01.25
[yt-dlp.git] / youtube_dl / extractor / canalplus.py
CommitLineData
dcdb292f 1# coding: utf-8
b075d25b
S
2from __future__ import unicode_literals
3
ffca4b5c 4import re
ffca4b5c
JMF
5
6from .common import InfoExtractor
74193838 7from ..compat import compat_urllib_parse_urlparse
4de9e9a6 8from ..utils import (
e2004cca 9 dict_get,
817f786f
JMF
10 ExtractorError,
11 HEADRequest,
d5f6429d 12 int_or_none,
e2004cca
YCH
13 qualities,
14 remove_end,
15 unified_strdate,
4de9e9a6 16)
ffca4b5c 17
69545c2a 18
ffca4b5c 19class CanalplusIE(InfoExtractor):
6e1cff9c 20 IE_DESC = 'canalplus.fr, piwiplus.fr and d8.tv'
74193838
S
21 _VALID_URL = r'''(?x)
22 https?://
23 (?:
24 (?:
25 (?:(?:www|m)\.)?canalplus\.fr|
26 (?:www\.)?piwiplus\.fr|
27 (?:www\.)?d8\.tv|
b29f842e 28 (?:www\.)?c8\.fr|
57b6e965 29 (?:www\.)?d17\.tv|
74193838
S
30 (?:www\.)?itele\.fr
31 )/(?:(?:[^/]+/)*(?P<display_id>[^/?#&]+))?(?:\?.*\bvid=(?P<vid>\d+))?|
32 player\.canalplus\.fr/#/(?P<id>\d+)
33 )
34
35 '''
d5f6429d 36 _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
72975729 37 _SITE_ID_MAP = {
74193838
S
38 'canalplus': 'cplus',
39 'piwiplus': 'teletoon',
40 'd8': 'd8',
b29f842e 41 'c8': 'd8',
57b6e965 42 'd17': 'd17',
74193838 43 'itele': 'itele',
72975729 44 }
ffca4b5c 45
72975729 46 _TESTS = [{
3d9b3605 47 'url': 'http://www.canalplus.fr/c-emissions/pid1830-c-zapping.html?vid=1192814',
b075d25b 48 'info_dict': {
e2004cca
YCH
49 'id': '1405510',
50 'display_id': 'pid1830-c-zapping',
d5f6429d 51 'ext': 'mp4',
e2004cca
YCH
52 'title': 'Zapping - 02/07/2016',
53 'description': 'Le meilleur de toutes les chaînes, tous les jours',
54 'upload_date': '20160702',
ad94a6fe 55 },
72975729
S
56 }, {
57 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
72975729
S
58 'info_dict': {
59 'id': '1108190',
e2004cca
YCH
60 'display_id': 'pid1405-le-labyrinthe-boing-super-ranger',
61 'ext': 'mp4',
62 'title': 'BOING SUPER RANGER - Ep : Le labyrinthe',
6e1cff9c 63 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
72975729
S
64 'upload_date': '20140724',
65 },
6e1cff9c
S
66 'skip': 'Only works from France',
67 }, {
e2004cca
YCH
68 'url': 'http://www.c8.fr/c8-divertissement/ms-touche-pas-a-mon-poste/pid6318-videos-integrales.html',
69 'md5': '4b47b12b4ee43002626b97fad8fb1de5',
6e1cff9c 70 'info_dict': {
e2004cca
YCH
71 'id': '1420213',
72 'display_id': 'pid6318-videos-integrales',
3d9b3605 73 'ext': 'mp4',
e2004cca
YCH
74 'title': 'TPMP ! Même le matin - Les 35H de Baba - 14/10/2016',
75 'description': 'md5:f96736c1b0ffaa96fd5b9e60ad871799',
76 'upload_date': '20161014',
6e1cff9c 77 },
e2004cca 78 'skip': 'Only works from France',
ea5db846 79 }, {
e2004cca 80 'url': 'http://www.itele.fr/chroniques/invite-michael-darmon/rachida-dati-nicolas-sarkozy-est-le-plus-en-phase-avec-les-inquietudes-des-francais-171510',
ea5db846 81 'info_dict': {
e2004cca
YCH
82 'id': '1420176',
83 'display_id': 'rachida-dati-nicolas-sarkozy-est-le-plus-en-phase-avec-les-inquietudes-des-francais-171510',
d5f6429d 84 'ext': 'mp4',
e2004cca
YCH
85 'title': 'L\'invité de Michaël Darmon du 14/10/2016 - ',
86 'description': 'Chaque matin du lundi au vendredi, Michaël Darmon reçoit un invité politique à 8h25.',
87 'upload_date': '20161014',
ea5db846 88 },
74193838
S
89 }, {
90 'url': 'http://m.canalplus.fr/?vid=1398231',
91 'only_matching': True,
57b6e965
S
92 }, {
93 'url': 'http://www.d17.tv/emissions/pid8303-lolywood.html?vid=1397061',
94 'only_matching': True,
72975729 95 }]
ffca4b5c
JMF
96
97 def _real_extract(self, url):
98 mobj = re.match(self._VALID_URL, url)
4de9e9a6 99
74193838 100 site_id = self._SITE_ID_MAP[compat_urllib_parse_urlparse(url).netloc.rsplit('.', 2)[-2]]
72975729 101
4de9e9a6 102 # Beware, some subclasses do not define an id group
e2004cca 103 display_id = remove_end(dict_get(mobj.groupdict(), ('display_id', 'id', 'vid')), '.html')
b075d25b 104
e2004cca
YCH
105 webpage = self._download_webpage(url, display_id)
106 video_id = self._search_regex(
107 [r'<canal:player[^>]+?videoId=(["\'])(?P<id>\d+)',
6ca478d4
S
108 r'id=["\']canal_video_player(?P<id>\d+)',
109 r'data-video=["\'](?P<id>\d+)'],
aaf2b7c5 110 webpage, 'video id', default=mobj.group('vid'), group='id')
b075d25b 111
72975729 112 info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
d5f6429d 113 video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
ffca4b5c 114
d5f6429d 115 if isinstance(video_data, list):
116 video_data = [video for video in video_data if video.get('ID') == video_id][0]
117 media = video_data['MEDIA']
118 infos = video_data['INFOS']
b075d25b 119
d5f6429d 120 preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
b075d25b 121
d5f6429d 122 fmt_url = next(iter(media.get('VIDEOS')))
817f786f
JMF
123 if '/geo' in fmt_url.lower():
124 response = self._request_webpage(
125 HEADRequest(fmt_url), video_id,
126 'Checking if the video is georestricted')
127 if '/blocage' in response.geturl():
128 raise ExtractorError(
129 'The video is not available in your country',
130 expected=True)
131
6e1cff9c 132 formats = []
d5f6429d 133 for format_id, format_url in media['VIDEOS'].items():
6e1cff9c
S
134 if not format_url:
135 continue
6e1cff9c 136 if format_id == 'HLS':
f3f0b8e4 137 formats.extend(self._extract_m3u8_formats(
d5f6429d 138 format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
6e1cff9c 139 elif format_id == 'HDS':
f3f0b8e4 140 formats.extend(self._extract_f4m_formats(
d5f6429d 141 format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
6e1cff9c
S
142 else:
143 formats.append({
d5f6429d 144 # the secret extracted ya function in http://player.canalplus.fr/common/js/canalPlayer.js
145 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
6e1cff9c
S
146 'format_id': format_id,
147 'preference': preference(format_id),
148 })
b075d25b
S
149 self._sort_formats(formats)
150
d5f6429d 151 thumbnails = [{
152 'id': image_id,
153 'url': image_url,
154 } for image_id, image_url in media.get('images', {}).items()]
155
156 titrage = infos['TITRAGE']
157
b075d25b
S
158 return {
159 'id': video_id,
4de9e9a6 160 'display_id': display_id,
d5f6429d 161 'title': '%s - %s' % (titrage['TITRE'],
162 titrage['SOUS_TITRE']),
163 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
164 'thumbnails': thumbnails,
165 'description': infos.get('DESCRIPTION'),
166 'duration': int_or_none(infos.get('DURATION')),
167 'view_count': int_or_none(infos.get('NB_VUES')),
168 'like_count': int_or_none(infos.get('NB_LIKES')),
169 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
b075d25b 170 'formats': formats,
5f6a1245 171 }