]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/toutv.py
New upstream version 2017.02.07
[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 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 _TESTS = [{
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 'url': 'http://ici.tou.tv/hackers',
38 'only_matching': True,
39 }]
40
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(
59 r'(?s)(<form[^>]+(?:id|name)="Form-login".+?</form>)', login_webpage, 'login form')
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
78 def _real_extract(self, url):
79 path = self._match_id(url)
80 metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
81 if metadata.get('IsDrm'):
82 raise ExtractorError('This video is DRM protected.', expected=True)
83 video_id = metadata['IdMedia']
84 details = metadata['Details']
85 title = details['OriginalTitle']
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 })
92
93 return {
94 '_type': 'url_transparent',
95 'url': video_url,
96 'id': video_id,
97 'title': title,
98 'thumbnail': details.get('ImageUrl'),
99 'duration': int_or_none(details.get('LengthInSeconds')),
100 }