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