[toutv] fix authentication(closes #20261)
[youtube-dl] / youtube_dl / extractor / toutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5
6 from .radiocanada import RadioCanadaIE
7 from ..utils import (
8     int_or_none,
9     merge_dicts,
10 )
11
12
13 class TouTvIE(RadioCanadaIE):
14     _NETRC_MACHINE = 'toutv'
15     IE_NAME = 'tou.tv'
16     _VALID_URL = r'https?://ici\.tou\.tv/(?P<id>[a-zA-Z0-9_-]+(?:/S[0-9]+[EC][0-9]+)?)'
17
18     _TESTS = [{
19         'url': 'http://ici.tou.tv/garfield-tout-court/S2015E17',
20         'info_dict': {
21             'id': '122017',
22             'ext': 'mp4',
23             'title': 'Saison 2015 Ă‰pisode 17',
24             'description': 'La photo de famille 2',
25             'upload_date': '20100717',
26         },
27         'params': {
28             # m3u8 download
29             'skip_download': True,
30         },
31         'skip': '404 Not Found',
32     }, {
33         'url': 'http://ici.tou.tv/hackers',
34         'only_matching': True,
35     }, {
36         'url': 'https://ici.tou.tv/l-age-adulte/S01C501',
37         'only_matching': True,
38     }]
39     _CLIENT_KEY = '4dd36440-09d5-4468-8923-b6d91174ad36'
40
41     def _real_initialize(self):
42         email, password = self._get_login_info()
43         if email is None:
44             return
45         self._access_token = self._download_json(
46             'https://services.radio-canada.ca/toutv/profiling/accounts/login',
47             None, 'Logging in', data=json.dumps({
48                 'ClientId': self._CLIENT_KEY,
49                 'ClientSecret': '34026772-244b-49b6-8b06-317b30ac9a20',
50                 'Email': email,
51                 'Password': password,
52                 'Scope': 'id.write media-validation.read',
53             }).encode(), headers={
54                 'Authorization': 'client-key ' + self._CLIENT_KEY,
55                 'Content-Type': 'application/json;charset=utf-8',
56             })['access_token']
57         self._claims = self._call_api('validation/v2/getClaims')['claims']
58
59     def _real_extract(self, url):
60         path = self._match_id(url)
61         metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
62         # IsDrm does not necessarily mean the video is DRM protected (see
63         # https://github.com/rg3/youtube-dl/issues/13994).
64         if metadata.get('IsDrm'):
65             self.report_warning('This video is probably DRM protected.', path)
66         video_id = metadata['IdMedia']
67         details = metadata['Details']
68
69         return merge_dicts({
70             'id': video_id,
71             'title': details.get('OriginalTitle'),
72             'thumbnail': details.get('ImageUrl'),
73             'duration': int_or_none(details.get('LengthInSeconds')),
74         }, self._extract_info(metadata.get('AppCode', 'toutv'), video_id))