]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/toutv.py
Start new release.
[youtubedl] / youtube_dl / extractor / toutv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 int_or_none,
9 js_to_json,
10 urlencode_postdata,
11 extract_attributes,
12 smuggle_url,
13 )
14
15
16 class TouTvIE(InfoExtractor):
17 _NETRC_MACHINE = 'toutv'
18 IE_NAME = 'tou.tv'
19 _VALID_URL = r'https?://ici\.tou\.tv/(?P<id>[a-zA-Z0-9_-]+(?:/S[0-9]+[EC][0-9]+)?)'
20 _access_token = None
21 _claims = None
22
23 _TESTS = [{
24 'url': 'http://ici.tou.tv/garfield-tout-court/S2015E17',
25 'info_dict': {
26 'id': '122017',
27 'ext': 'mp4',
28 'title': 'Saison 2015 Épisode 17',
29 'description': 'La photo de famille 2',
30 'upload_date': '20100717',
31 },
32 'params': {
33 # m3u8 download
34 'skip_download': True,
35 },
36 'skip': '404 Not Found',
37 }, {
38 'url': 'http://ici.tou.tv/hackers',
39 'only_matching': True,
40 }, {
41 'url': 'https://ici.tou.tv/l-age-adulte/S01C501',
42 'only_matching': True,
43 }]
44
45 def _real_initialize(self):
46 email, password = self._get_login_info()
47 if email is None:
48 return
49 state = 'http://ici.tou.tv/'
50 webpage = self._download_webpage(state, None, 'Downloading homepage')
51 toutvlogin = self._parse_json(self._search_regex(
52 r'(?s)toutvlogin\s*=\s*({.+?});', webpage, 'toutvlogin'), None, js_to_json)
53 authorize_url = toutvlogin['host'] + '/auth/oauth/v2/authorize'
54 login_webpage = self._download_webpage(
55 authorize_url, None, 'Downloading login page', query={
56 'client_id': toutvlogin['clientId'],
57 'redirect_uri': 'https://ici.tou.tv/login/loginCallback',
58 'response_type': 'token',
59 'scope': 'media-drmt openid profile email id.write media-validation.read.privileged',
60 'state': state,
61 })
62
63 def extract_form_url_and_data(wp, default_form_url, form_spec_re=''):
64 form, form_elem = re.search(
65 r'(?s)((<form[^>]+?%s[^>]*?>).+?</form>)' % form_spec_re, wp).groups()
66 form_data = self._hidden_inputs(form)
67 form_url = extract_attributes(form_elem).get('action') or default_form_url
68 return form_url, form_data
69
70 post_url, form_data = extract_form_url_and_data(
71 login_webpage,
72 'https://services.radio-canada.ca/auth/oauth/v2/authorize/login',
73 r'(?:id|name)="Form-login"')
74 form_data.update({
75 'login-email': email,
76 'login-password': password,
77 })
78 consent_webpage = self._download_webpage(
79 post_url, None, 'Logging in', data=urlencode_postdata(form_data))
80 post_url, form_data = extract_form_url_and_data(
81 consent_webpage,
82 'https://services.radio-canada.ca/auth/oauth/v2/authorize/consent')
83 _, urlh = self._download_webpage_handle(
84 post_url, None, 'Following Redirection',
85 data=urlencode_postdata(form_data))
86 self._access_token = self._search_regex(
87 r'access_token=([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
88 urlh.geturl(), 'access token')
89 self._claims = self._download_json(
90 'https://services.radio-canada.ca/media/validation/v2/getClaims',
91 None, 'Extracting Claims', query={
92 'token': self._access_token,
93 'access_token': self._access_token,
94 })['claims']
95
96 def _real_extract(self, url):
97 path = self._match_id(url)
98 metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
99 # IsDrm does not necessarily mean the video is DRM protected (see
100 # https://github.com/rg3/youtube-dl/issues/13994).
101 if metadata.get('IsDrm'):
102 self.report_warning('This video is probably DRM protected.', path)
103 video_id = metadata['IdMedia']
104 details = metadata['Details']
105 title = details['OriginalTitle']
106 video_url = 'radiocanada:%s:%s' % (metadata.get('AppCode', 'toutv'), video_id)
107 if self._access_token and self._claims:
108 video_url = smuggle_url(video_url, {
109 'access_token': self._access_token,
110 'claims': self._claims,
111 })
112
113 return {
114 '_type': 'url_transparent',
115 'url': video_url,
116 'id': video_id,
117 'title': title,
118 'thumbnail': details.get('ImageUrl'),
119 'duration': int_or_none(details.get('LengthInSeconds')),
120 }