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.

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