]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/canalplus.py
[tiktok] Fix `extractor_key` used in archive
[yt-dlp.git] / yt_dlp / extractor / canalplus.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4
5 from .common import InfoExtractor
6 from ..utils import (
7 # ExtractorError,
8 # HEADRequest,
9 int_or_none,
10 qualities,
11 unified_strdate,
12 )
13
14
15 class CanalplusIE(InfoExtractor):
16 IE_DESC = 'mycanal.fr and piwiplus.fr'
17 _VALID_URL = r'https?://(?:www\.)?(?P<site>mycanal|piwiplus)\.fr/(?:[^/]+/)*(?P<display_id>[^?/]+)(?:\.html\?.*\bvid=|/p/)(?P<id>\d+)'
18 _VIDEO_INFO_TEMPLATE = 'http://service.canal-plus.com/video/rest/getVideosLiees/%s/%s?format=json'
19 _SITE_ID_MAP = {
20 'mycanal': 'cplus',
21 'piwiplus': 'teletoon',
22 }
23
24 # Only works for direct mp4 URLs
25 _GEO_COUNTRIES = ['FR']
26
27 _TESTS = [{
28 'url': 'https://www.mycanal.fr/d17-emissions/lolywood/p/1397061',
29 'info_dict': {
30 'id': '1397061',
31 'display_id': 'lolywood',
32 'ext': 'mp4',
33 'title': 'Euro 2016 : Je préfère te prévenir - Lolywood - Episode 34',
34 'description': 'md5:7d97039d455cb29cdba0d652a0efaa5e',
35 'upload_date': '20160602',
36 },
37 }, {
38 # geo restricted, bypassed
39 'url': 'http://www.piwiplus.fr/videos-piwi/pid1405-le-labyrinthe-boing-super-ranger.html?vid=1108190',
40 'info_dict': {
41 'id': '1108190',
42 'display_id': 'pid1405-le-labyrinthe-boing-super-ranger',
43 'ext': 'mp4',
44 'title': 'BOING SUPER RANGER - Ep : Le labyrinthe',
45 'description': 'md5:4cea7a37153be42c1ba2c1d3064376ff',
46 'upload_date': '20140724',
47 },
48 'expected_warnings': ['HTTP Error 403: Forbidden'],
49 }]
50
51 def _real_extract(self, url):
52 site, display_id, video_id = self._match_valid_url(url).groups()
53
54 site_id = self._SITE_ID_MAP[site]
55
56 info_url = self._VIDEO_INFO_TEMPLATE % (site_id, video_id)
57 video_data = self._download_json(info_url, video_id, 'Downloading video JSON')
58
59 if isinstance(video_data, list):
60 video_data = [video for video in video_data if video.get('ID') == video_id][0]
61 media = video_data['MEDIA']
62 infos = video_data['INFOS']
63
64 preference = qualities(['MOBILE', 'BAS_DEBIT', 'HAUT_DEBIT', 'HD'])
65
66 # _, fmt_url = next(iter(media['VIDEOS'].items()))
67 # if '/geo' in fmt_url.lower():
68 # response = self._request_webpage(
69 # HEADRequest(fmt_url), video_id,
70 # 'Checking if the video is georestricted')
71 # if '/blocage' in response.geturl():
72 # raise ExtractorError(
73 # 'The video is not available in your country',
74 # expected=True)
75
76 formats = []
77 for format_id, format_url in media['VIDEOS'].items():
78 if not format_url:
79 continue
80 if format_id == 'HLS':
81 formats.extend(self._extract_m3u8_formats(
82 format_url, video_id, 'mp4', 'm3u8_native', m3u8_id=format_id, fatal=False))
83 elif format_id == 'HDS':
84 formats.extend(self._extract_f4m_formats(
85 format_url + '?hdcore=2.11.3', video_id, f4m_id=format_id, fatal=False))
86 else:
87 formats.append({
88 # the secret extracted from ya function in http://player.canalplus.fr/common/js/canalPlayer.js
89 'url': format_url + '?secret=pqzerjlsmdkjfoiuerhsdlfknaes',
90 'format_id': format_id,
91 'quality': preference(format_id),
92 })
93 self._sort_formats(formats)
94
95 thumbnails = [{
96 'id': image_id,
97 'url': image_url,
98 } for image_id, image_url in media.get('images', {}).items()]
99
100 titrage = infos['TITRAGE']
101
102 return {
103 'id': video_id,
104 'display_id': display_id,
105 'title': '%s - %s' % (titrage['TITRE'],
106 titrage['SOUS_TITRE']),
107 'upload_date': unified_strdate(infos.get('PUBLICATION', {}).get('DATE')),
108 'thumbnails': thumbnails,
109 'description': infos.get('DESCRIPTION'),
110 'duration': int_or_none(infos.get('DURATION')),
111 'view_count': int_or_none(infos.get('NB_VUES')),
112 'like_count': int_or_none(infos.get('NB_LIKES')),
113 'comment_count': int_or_none(infos.get('NB_COMMENTS')),
114 'formats': formats,
115 }