You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.6 KiB

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