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.

137 lines
6.1 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import re
  4. import time
  5. import xml.etree.ElementTree as etree
  6. from .common import InfoExtractor
  7. from ..utils import (
  8. unescapeHTML,
  9. urlencode_postdata,
  10. unified_timestamp,
  11. )
  12. class AdobePassIE(InfoExtractor):
  13. _SERVICE_PROVIDER_TEMPLATE = 'https://sp.auth.adobe.com/adobe-services/%s'
  14. _USER_AGENT = 'Mozilla/5.0 (X11; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0'
  15. @staticmethod
  16. def _get_mvpd_resource(provider_id, title, guid, rating):
  17. channel = etree.Element('channel')
  18. channel_title = etree.SubElement(channel, 'title')
  19. channel_title.text = provider_id
  20. item = etree.SubElement(channel, 'item')
  21. resource_title = etree.SubElement(item, 'title')
  22. resource_title.text = title
  23. resource_guid = etree.SubElement(item, 'guid')
  24. resource_guid.text = guid
  25. resource_rating = etree.SubElement(item, 'media:rating')
  26. resource_rating.attrib = {'scheme': 'urn:v-chip'}
  27. resource_rating.text = rating
  28. return '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">' + etree.tostring(channel).decode() + '</rss>'
  29. def _extract_mvpd_auth(self, url, video_id, requestor_id, resource):
  30. def xml_text(xml_str, tag):
  31. return self._search_regex(
  32. '<%s>(.+?)</%s>' % (tag, tag), xml_str, tag)
  33. def is_expired(token, date_ele):
  34. token_expires = unified_timestamp(re.sub(r'[_ ]GMT', '', xml_text(token, date_ele)))
  35. return token_expires and token_expires <= int(time.time())
  36. mvpd_headers = {
  37. 'ap_42': 'anonymous',
  38. 'ap_11': 'Linux i686',
  39. 'ap_z': self._USER_AGENT,
  40. 'User-Agent': self._USER_AGENT,
  41. }
  42. guid = xml_text(resource, 'guid')
  43. requestor_info = self._downloader.cache.load('mvpd', requestor_id) or {}
  44. authn_token = requestor_info.get('authn_token')
  45. if authn_token and is_expired(authn_token, 'simpleTokenExpires'):
  46. authn_token = None
  47. if not authn_token:
  48. # TODO add support for other TV Providers
  49. mso_id = 'DTV'
  50. username, password = self._get_netrc_login_info(mso_id)
  51. if not username or not password:
  52. return ''
  53. def post_form(form_page, note, data={}):
  54. post_url = self._html_search_regex(r'<form[^>]+action=(["\'])(?P<url>.+?)\1', form_page, 'post url', group='url')
  55. return self._download_webpage(
  56. post_url, video_id, note, data=urlencode_postdata(data or self._hidden_inputs(form_page)), headers={
  57. 'Content-Type': 'application/x-www-form-urlencoded',
  58. })
  59. provider_redirect_page = self._download_webpage(
  60. self._SERVICE_PROVIDER_TEMPLATE % 'authenticate/saml', video_id,
  61. 'Downloading Provider Redirect Page', query={
  62. 'noflash': 'true',
  63. 'mso_id': mso_id,
  64. 'requestor_id': requestor_id,
  65. 'no_iframe': 'false',
  66. 'domain_name': 'adobe.com',
  67. 'redirect_url': url,
  68. })
  69. provider_login_page = post_form(
  70. provider_redirect_page, 'Downloading Provider Login Page')
  71. mvpd_confirm_page = post_form(provider_login_page, 'Logging in', {
  72. 'username': username,
  73. 'password': password,
  74. })
  75. post_form(mvpd_confirm_page, 'Confirming Login')
  76. session = self._download_webpage(
  77. self._SERVICE_PROVIDER_TEMPLATE % 'session', video_id,
  78. 'Retrieving Session', data=urlencode_postdata({
  79. '_method': 'GET',
  80. 'requestor_id': requestor_id,
  81. }), headers=mvpd_headers)
  82. if '<pendingLogout' in session:
  83. self._downloader.cache.store('mvpd', requestor_id, {})
  84. return self._extract_mvpd_auth(url, video_id, requestor_id, resource)
  85. authn_token = unescapeHTML(xml_text(session, 'authnToken'))
  86. requestor_info['authn_token'] = authn_token
  87. self._downloader.cache.store('mvpd', requestor_id, requestor_info)
  88. authz_token = requestor_info.get(guid)
  89. if authz_token and is_expired(authz_token, 'simpleTokenTTL'):
  90. authz_token = None
  91. if not authz_token:
  92. authorize = self._download_webpage(
  93. self._SERVICE_PROVIDER_TEMPLATE % 'authorize', video_id,
  94. 'Retrieving Authorization Token', data=urlencode_postdata({
  95. 'resource_id': resource,
  96. 'requestor_id': requestor_id,
  97. 'authentication_token': authn_token,
  98. 'mso_id': xml_text(authn_token, 'simpleTokenMsoID'),
  99. 'userMeta': '1',
  100. }), headers=mvpd_headers)
  101. if '<pendingLogout' in authorize:
  102. self._downloader.cache.store('mvpd', requestor_id, {})
  103. return self._extract_mvpd_auth(url, video_id, requestor_id, resource)
  104. authz_token = unescapeHTML(xml_text(authorize, 'authzToken'))
  105. requestor_info[guid] = authz_token
  106. self._downloader.cache.store('mvpd', requestor_id, requestor_info)
  107. mvpd_headers.update({
  108. 'ap_19': xml_text(authn_token, 'simpleSamlNameID'),
  109. 'ap_23': xml_text(authn_token, 'simpleSamlSessionIndex'),
  110. })
  111. short_authorize = self._download_webpage(
  112. self._SERVICE_PROVIDER_TEMPLATE % 'shortAuthorize',
  113. video_id, 'Retrieving Media Token', data=urlencode_postdata({
  114. 'authz_token': authz_token,
  115. 'requestor_id': requestor_id,
  116. 'session_guid': xml_text(authn_token, 'simpleTokenAuthenticationGuid'),
  117. 'hashed_guid': 'false',
  118. }), headers=mvpd_headers)
  119. if '<pendingLogout' in short_authorize:
  120. self._downloader.cache.store('mvpd', requestor_id, {})
  121. return self._extract_mvpd_auth(url, video_id, requestor_id, resource)
  122. return short_authorize