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