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.

120 lines
4.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  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. 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]+[EC][0-9]+)?)'
  16. _access_token = None
  17. _claims = None
  18. _TESTS = [{
  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. 'url': 'http://ici.tou.tv/hackers',
  34. 'only_matching': True,
  35. }, {
  36. 'url': 'https://ici.tou.tv/l-age-adulte/S01C501',
  37. 'only_matching': True,
  38. }]
  39. def _real_initialize(self):
  40. email, password = self._get_login_info()
  41. if email is None:
  42. return
  43. state = 'http://ici.tou.tv/'
  44. webpage = self._download_webpage(state, None, 'Downloading homepage')
  45. toutvlogin = self._parse_json(self._search_regex(
  46. r'(?s)toutvlogin\s*=\s*({.+?});', webpage, 'toutvlogin'), None, js_to_json)
  47. authorize_url = toutvlogin['host'] + '/auth/oauth/v2/authorize'
  48. login_webpage = self._download_webpage(
  49. authorize_url, None, 'Downloading login page', query={
  50. 'client_id': toutvlogin['clientId'],
  51. 'redirect_uri': 'https://ici.tou.tv/login/loginCallback',
  52. 'response_type': 'token',
  53. 'scope': 'media-drmt openid profile email id.write media-validation.read.privileged',
  54. 'state': state,
  55. })
  56. def extract_form_url_and_data(wp, default_form_url, form_spec_re=''):
  57. form, form_elem = re.search(
  58. r'(?s)((<form[^>]+?%s[^>]*?>).+?</form>)' % form_spec_re, wp).groups()
  59. form_data = self._hidden_inputs(form)
  60. form_url = extract_attributes(form_elem).get('action') or default_form_url
  61. return form_url, form_data
  62. post_url, form_data = extract_form_url_and_data(
  63. login_webpage,
  64. 'https://services.radio-canada.ca/auth/oauth/v2/authorize/login',
  65. r'(?:id|name)="Form-login"')
  66. form_data.update({
  67. 'login-email': email,
  68. 'login-password': password,
  69. })
  70. consent_webpage = self._download_webpage(
  71. post_url, None, 'Logging in', data=urlencode_postdata(form_data))
  72. post_url, form_data = extract_form_url_and_data(
  73. consent_webpage,
  74. 'https://services.radio-canada.ca/auth/oauth/v2/authorize/consent')
  75. _, urlh = self._download_webpage_handle(
  76. post_url, None, 'Following Redirection',
  77. data=urlencode_postdata(form_data))
  78. self._access_token = self._search_regex(
  79. r'access_token=([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
  80. urlh.geturl(), 'access token')
  81. self._claims = self._download_json(
  82. 'https://services.radio-canada.ca/media/validation/v2/getClaims',
  83. None, 'Extracting Claims', query={
  84. 'token': self._access_token,
  85. 'access_token': self._access_token,
  86. })['claims']
  87. def _real_extract(self, url):
  88. path = self._match_id(url)
  89. metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
  90. # IsDrm does not necessarily mean the video is DRM protected (see
  91. # https://github.com/rg3/youtube-dl/issues/13994).
  92. if metadata.get('IsDrm'):
  93. self.report_warning('This video is probably DRM protected.', path)
  94. video_id = metadata['IdMedia']
  95. details = metadata['Details']
  96. title = details['OriginalTitle']
  97. video_url = 'radiocanada:%s:%s' % (metadata.get('AppCode', 'toutv'), video_id)
  98. if self._access_token and self._claims:
  99. video_url = smuggle_url(video_url, {
  100. 'access_token': self._access_token,
  101. 'claims': self._claims,
  102. })
  103. return {
  104. '_type': 'url_transparent',
  105. 'url': video_url,
  106. 'id': video_id,
  107. 'title': title,
  108. 'thumbnail': details.get('ImageUrl'),
  109. 'duration': int_or_none(details.get('LengthInSeconds')),
  110. }