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.

100 lines
3.7 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. _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. def _real_initialize(self):
  37. email, password = self._get_login_info()
  38. if email is None:
  39. return
  40. state = 'http://ici.tou.tv//'
  41. webpage = self._download_webpage(state, None, 'Downloading homepage')
  42. toutvlogin = self._parse_json(self._search_regex(
  43. r'(?s)toutvlogin\s*=\s*({.+?});', webpage, 'toutvlogin'), None, js_to_json)
  44. authorize_url = toutvlogin['host'] + '/auth/oauth/v2/authorize'
  45. login_webpage = self._download_webpage(
  46. authorize_url, None, 'Downloading login page', query={
  47. 'client_id': toutvlogin['clientId'],
  48. 'redirect_uri': 'https://ici.tou.tv/login/loginCallback',
  49. 'response_type': 'token',
  50. 'scope': 'media-drmt openid profile email id.write media-validation.read.privileged',
  51. 'state': state,
  52. })
  53. login_form = self._search_regex(
  54. r'(?s)(<form[^>]+(?:id|name)="Form-login".+?</form>)', login_webpage, 'login form')
  55. form_data = self._hidden_inputs(login_form)
  56. form_data.update({
  57. 'login-email': email,
  58. 'login-password': password,
  59. })
  60. post_url = extract_attributes(login_form).get('action') or authorize_url
  61. _, urlh = self._download_webpage_handle(
  62. post_url, None, 'Logging in', data=urlencode_postdata(form_data))
  63. self._access_token = self._search_regex(
  64. r'access_token=([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
  65. urlh.geturl(), 'access token')
  66. self._claims = self._download_json(
  67. 'https://services.radio-canada.ca/media/validation/v2/getClaims',
  68. None, 'Extracting Claims', query={
  69. 'token': self._access_token,
  70. 'access_token': self._access_token,
  71. })['claims']
  72. def _real_extract(self, url):
  73. path = self._match_id(url)
  74. metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
  75. if metadata.get('IsDrm'):
  76. raise ExtractorError('This video is DRM protected.', expected=True)
  77. video_id = metadata['IdMedia']
  78. details = metadata['Details']
  79. title = details['OriginalTitle']
  80. video_url = 'radiocanada:%s:%s' % (metadata.get('AppCode', 'toutv'), video_id)
  81. if self._access_token and self._claims:
  82. video_url = smuggle_url(video_url, {
  83. 'access_token': self._access_token,
  84. 'claims': self._claims,
  85. })
  86. return {
  87. '_type': 'url_transparent',
  88. 'url': video_url,
  89. 'id': video_id,
  90. 'title': title,
  91. 'thumbnail': details.get('ImageUrl'),
  92. 'duration': int_or_none(details.get('LengthInSeconds')),
  93. }