toutv.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .radiocanada import RadioCanadaIE
  5. from ..utils import (
  6. extract_attributes,
  7. int_or_none,
  8. merge_dicts,
  9. urlencode_postdata,
  10. )
  11. class TouTvIE(RadioCanadaIE):
  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]+[EC][0-9]+)?)'
  15. _TESTS = [{
  16. 'url': 'http://ici.tou.tv/garfield-tout-court/S2015E17',
  17. 'info_dict': {
  18. 'id': '122017',
  19. 'ext': 'mp4',
  20. 'title': 'Saison 2015 Épisode 17',
  21. 'description': 'La photo de famille 2',
  22. 'upload_date': '20100717',
  23. },
  24. 'params': {
  25. # m3u8 download
  26. 'skip_download': True,
  27. },
  28. 'skip': '404 Not Found',
  29. }, {
  30. 'url': 'http://ici.tou.tv/hackers',
  31. 'only_matching': True,
  32. }, {
  33. 'url': 'https://ici.tou.tv/l-age-adulte/S01C501',
  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. login_webpage = self._download_webpage(
  41. 'https://services.radio-canada.ca/auth/oauth/v2/authorize',
  42. None, 'Downloading login page', query={
  43. 'client_id': '4dd36440-09d5-4468-8923-b6d91174ad36',
  44. 'redirect_uri': 'https://ici.tou.tv/logincallback',
  45. 'response_type': 'token',
  46. 'scope': 'id.write media-validation.read',
  47. 'state': '/',
  48. })
  49. def extract_form_url_and_data(wp, default_form_url, form_spec_re=''):
  50. form, form_elem = re.search(
  51. r'(?s)((<form[^>]+?%s[^>]*?>).+?</form>)' % form_spec_re, wp).groups()
  52. form_data = self._hidden_inputs(form)
  53. form_url = extract_attributes(form_elem).get('action') or default_form_url
  54. return form_url, form_data
  55. post_url, form_data = extract_form_url_and_data(
  56. login_webpage,
  57. 'https://services.radio-canada.ca/auth/oauth/v2/authorize/login',
  58. r'(?:id|name)="Form-login"')
  59. form_data.update({
  60. 'login-email': email,
  61. 'login-password': password,
  62. })
  63. consent_webpage = self._download_webpage(
  64. post_url, None, 'Logging in', data=urlencode_postdata(form_data))
  65. post_url, form_data = extract_form_url_and_data(
  66. consent_webpage,
  67. 'https://services.radio-canada.ca/auth/oauth/v2/authorize/consent')
  68. _, urlh = self._download_webpage_handle(
  69. post_url, None, 'Following Redirection',
  70. data=urlencode_postdata(form_data))
  71. self._access_token = self._search_regex(
  72. r'access_token=([\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
  73. urlh.geturl(), 'access token')
  74. self._claims = self._call_api('validation/v2/getClaims')['claims']
  75. def _real_extract(self, url):
  76. path = self._match_id(url)
  77. metadata = self._download_json('http://ici.tou.tv/presentation/%s' % path, path)
  78. # IsDrm does not necessarily mean the video is DRM protected (see
  79. # https://github.com/rg3/youtube-dl/issues/13994).
  80. if metadata.get('IsDrm'):
  81. self.report_warning('This video is probably DRM protected.', path)
  82. video_id = metadata['IdMedia']
  83. details = metadata['Details']
  84. return merge_dicts({
  85. 'id': video_id,
  86. 'title': details.get('OriginalTitle'),
  87. 'thumbnail': details.get('ImageUrl'),
  88. 'duration': int_or_none(details.get('LengthInSeconds')),
  89. }, self._extract_info(metadata.get('AppCode', 'toutv'), video_id))