[toutv] fix authentication(closes #16398)(closes #18700)
[youtube-dl] / youtube_dl / extractor / toutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .radiocanada import RadioCanadaIE
7 from ..utils import (
8     extract_attributes,
9     int_or_none,
10     merge_dicts,
11     urlencode_postdata,
12 )
13
14
15 class TouTvIE(RadioCanadaIE):
16     _NETRC_MACHINE = 'toutv'
17     IE_NAME = 'tou.tv'
18     _VALID_URL = r'https?://ici\.tou\.tv/(?P<id>[a-zA-Z0-9_-]+(?:/S[0-9]+[EC][0-9]+)?)'
19
20     _TESTS = [{
21         'url': 'http://ici.tou.tv/garfield-tout-court/S2015E17',
22         'info_dict': {
23             'id': '122017',
24             'ext': 'mp4',
25             'title': 'Saison 2015 Ă‰pisode 17',
26             'description': 'La photo de famille 2',
27             'upload_date': '20100717',
28         },
29         'params': {
30             # m3u8 download
31             'skip_download': True,
32         },
33         'skip': '404 Not Found',
34     }, {
35         'url': 'http://ici.tou.tv/hackers',
36         'only_matching': True,
37     }, {
38         'url': 'https://ici.tou.tv/l-age-adulte/S01C501',
39         'only_matching': True,
40     }]
41
42     def _real_initialize(self):
43         email, password = self._get_login_info()
44         if email is None:
45             return
46         login_webpage = self._download_webpage(
47             'https://services.radio-canada.ca/auth/oauth/v2/authorize',
48             None, 'Downloading login page', query={
49                 'client_id': '4dd36440-09d5-4468-8923-b6d91174ad36',
50                 'redirect_uri': 'https://ici.tou.tv/logincallback',
51                 'response_type': 'token',
52                 'scope': 'id.write media-validation.read',
53                 'state': '/',
54             })
55
56         def extract_form_url_and_data(wp, default_form_url, form_spec_re=''):
57             form, form_elem = re.search(
58                 r'(?s)((<form[^>]+?%s[^>]*?>).+?</form>)' % form_spec_re, wp).groups()
59             form_data = self._hidden_inputs(form)
60             form_url = extract_attributes(form_elem).get('action') or default_form_url
61             return form_url, form_data
62
63         post_url, form_data = extract_form_url_and_data(
64             login_webpage,
65             'https://services.radio-canada.ca/auth/oauth/v2/authorize/login',
66             r'(?:id|name)="Form-login"')
67         form_data.update({
68             'login-email': email,
69             'login-password': password,
70         })
71         consent_webpage = self._download_webpage(
72             post_url, None, 'Logging in', data=urlencode_postdata(form_data))
73         post_url, form_data = extract_form_url_and_data(
74             consent_webpage,
75             'https://services.radio-canada.ca/auth/oauth/v2/authorize/consent')
76         _, urlh = self._download_webpage_handle(
77             post_url, None, 'Following Redirection',
78             data=urlencode_postdata(form_data))
79         self._access_token = self._search_regex(
80             r'access_token=([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
81             urlh.geturl(), 'access token')
82         self._claims = self._call_api('validation/v2/getClaims')['claims']
83
84     def _real_extract(self, url):
85         path = self._match_id(url)
86         metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
87         # IsDrm does not necessarily mean the video is DRM protected (see
88         # https://github.com/rg3/youtube-dl/issues/13994).
89         if metadata.get('IsDrm'):
90             self.report_warning('This video is probably DRM protected.', path)
91         video_id = metadata['IdMedia']
92         details = metadata['Details']
93
94         return merge_dicts({
95             'id': video_id,
96             'title': details.get('OriginalTitle'),
97             'thumbnail': details.get('ImageUrl'),
98             'duration': int_or_none(details.get('LengthInSeconds')),
99         }, self._extract_info(metadata.get('AppCode', 'toutv'), video_id))