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