2 from __future__ import unicode_literals
4 from .common import InfoExtractor
15 class TouTvIE(InfoExtractor):
16 _NETRC_MACHINE = 'toutv'
18 _VALID_URL = r'https?://ici\.tou\.tv/(?P<id>[a-zA-Z0-9_-]+/S[0-9]+E[0-9]+)'
23 'url': 'http://ici.tou.tv/garfield-tout-court/S2015E17',
27 'title': 'Saison 2015 Épisode 17',
28 'description': 'La photo de famille 2',
29 'upload_date': '20100717',
33 'skip_download': True,
35 'skip': '404 Not Found',
38 def _real_initialize(self):
39 email, password = self._get_login_info()
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',
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)
60 'login-password': password,
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,
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,
91 '_type': 'url_transparent',
95 'thumbnail': details.get('ImageUrl'),
96 'duration': int_or_none(details.get('LengthInSeconds')),