]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/toutv.py
[extractor] Add `_perform_login` function (#2943)
[yt-dlp.git] / yt_dlp / 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 ..compat import compat_HTTPError
8 from ..utils import (
9 ExtractorError,
10 int_or_none,
11 merge_dicts,
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 _CLIENT_KEY = '90505c8d-9c34-4f34-8da1-3a85bdc6d4f4'
42
43 def _perform_login(self, username, password):
44 try:
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': username,
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 except ExtractorError as e:
58 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
59 error = self._parse_json(e.cause.read().decode(), None)['Message']
60 raise ExtractorError(error, expected=True)
61 raise
62 self._claims = self._call_api('validation/v2/getClaims')['claims']
63
64 def _real_extract(self, url):
65 path = self._match_id(url)
66 metadata = self._download_json(
67 'https://services.radio-canada.ca/toutv/presentation/%s' % path, path, query={
68 'client_key': self._CLIENT_KEY,
69 'device': 'web',
70 'version': 4,
71 })
72 # IsDrm does not necessarily mean the video is DRM protected (see
73 # https://github.com/ytdl-org/youtube-dl/issues/13994).
74 if not self.get_param('allow_unplayable_formats') and metadata.get('IsDrm'):
75 self.report_warning('This video is probably DRM protected.', path)
76 video_id = metadata['IdMedia']
77 details = metadata['Details']
78
79 return merge_dicts({
80 'id': video_id,
81 'title': details.get('OriginalTitle'),
82 'description': details.get('Description'),
83 'thumbnail': details.get('ImageUrl'),
84 'duration': int_or_none(details.get('LengthInSeconds')),
85 'series': metadata.get('ProgramTitle'),
86 'season_number': int_or_none(metadata.get('SeasonNumber')),
87 'season': metadata.get('SeasonTitle'),
88 'episode_number': int_or_none(metadata.get('EpisodeNumber')),
89 'episode': metadata.get('EpisodeTitle'),
90 }, self._extract_info(metadata.get('AppCode', 'toutv'), video_id))