[toutv] add support for authentication(closes #10669)
[youtube-dl] / youtube_dl / extractor / toutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     int_or_none,
7     js_to_json,
8     ExtractorError,
9     urlencode_postdata,
10     extract_attributes,
11     smuggle_url,
12 )
13
14
15 class TouTvIE(InfoExtractor):
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]+E[0-9]+)'
19     _access_token = None
20     _claims = None
21
22     _TEST = {
23         'url': 'http://ici.tou.tv/garfield-tout-court/S2015E17',
24         'info_dict': {
25             'id': '122017',
26             'ext': 'mp4',
27             'title': 'Saison 2015 Ă‰pisode 17',
28             'description': 'La photo de famille 2',
29             'upload_date': '20100717',
30         },
31         'params': {
32             # m3u8 download
33             'skip_download': True,
34         },
35         'skip': '404 Not Found',
36     }
37
38     def _real_initialize(self):
39         email, password = self._get_login_info()
40         if email is None:
41             return
42         state = 'http://ici.tou.tv//'
43         webpage = self._download_webpage(state, None, 'Downloading homepage')
44         toutvlogin = self._parse_json(self._search_regex(
45             r'(?s)toutvlogin\s*=\s*({.+?});', webpage, 'toutvlogin'), None, js_to_json)
46         authorize_url = toutvlogin['host'] + '/auth/oauth/v2/authorize'
47         login_webpage = self._download_webpage(
48             authorize_url, None, 'Downloading login page', query={
49                 'client_id': toutvlogin['clientId'],
50                 'redirect_uri': 'https://ici.tou.tv/login/loginCallback',
51                 'response_type': 'token',
52                 'scope': 'media-drmt openid profile email id.write media-validation.read.privileged',
53                 'state': state,
54             })
55         login_form = self._search_regex(
56             r'(?s)(<form[^>]+id="Form-login".+?</form>)', login_webpage, 'login form')
57         form_data = self._hidden_inputs(login_form)
58         form_data.update({
59             'login-email': email,
60             'login-password': password,
61         })
62         post_url = extract_attributes(login_form).get('action') or authorize_url
63         _, urlh = self._download_webpage_handle(
64             post_url, None, 'Logging in', data=urlencode_postdata(form_data))
65         self._access_token = self._search_regex(
66             r'access_token=([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
67             urlh.geturl(), 'access token')
68         self._claims = self._download_json(
69             'https://services.radio-canada.ca/media/validation/v2/getClaims',
70             None, 'Extracting Claims', query={
71                 'token': self._access_token,
72                 'access_token': self._access_token,
73             })['claims']
74
75     def _real_extract(self, url):
76         path = self._match_id(url)
77         metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
78         if metadata.get('IsDrm'):
79             raise ExtractorError('This video is DRM protected.', expected=True)
80         video_id = metadata['IdMedia']
81         details = metadata['Details']
82         title = details['OriginalTitle']
83         video_url = 'radiocanada:%s:%s' % (metadata.get('AppCode', 'toutv'), video_id)
84         if self._access_token and self._claims:
85             video_url = smuggle_url(video_url, {
86                 'access_token': self._access_token,
87                 'claims': self._claims,
88             })
89
90         return {
91             '_type': 'url_transparent',
92             'url': video_url,
93             'id': video_id,
94             'title': title,
95             'thumbnail': details.get('ImageUrl'),
96             'duration': int_or_none(details.get('LengthInSeconds')),
97         }