scte.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. decode_packed_codes,
  5. ExtractorError,
  6. urlencode_postdata,
  7. )
  8. class SCTEBaseIE(InfoExtractor):
  9. _LOGIN_URL = 'https://www.scte.org/SCTE/Sign_In.aspx'
  10. _NETRC_MACHINE = 'scte'
  11. def _perform_login(self, username, password):
  12. login_popup = self._download_webpage(
  13. self._LOGIN_URL, None, 'Downloading login popup')
  14. def is_logged(webpage):
  15. return any(re.search(p, webpage) for p in (
  16. r'class=["\']welcome\b', r'>Sign Out<'))
  17. # already logged in
  18. if is_logged(login_popup):
  19. return
  20. login_form = self._hidden_inputs(login_popup)
  21. login_form.update({
  22. 'ctl01$TemplateBody$WebPartManager1$gwpciNewContactSignInCommon$ciNewContactSignInCommon$signInUserName': username,
  23. 'ctl01$TemplateBody$WebPartManager1$gwpciNewContactSignInCommon$ciNewContactSignInCommon$signInPassword': password,
  24. 'ctl01$TemplateBody$WebPartManager1$gwpciNewContactSignInCommon$ciNewContactSignInCommon$RememberMe': 'on',
  25. })
  26. response = self._download_webpage(
  27. self._LOGIN_URL, None, 'Logging in',
  28. data=urlencode_postdata(login_form))
  29. if '|pageRedirect|' not in response and not is_logged(response):
  30. error = self._html_search_regex(
  31. r'(?s)<[^>]+class=["\']AsiError["\'][^>]*>(.+?)</',
  32. response, 'error message', default=None)
  33. if error:
  34. raise ExtractorError('Unable to login: %s' % error, expected=True)
  35. raise ExtractorError('Unable to log in')
  36. class SCTEIE(SCTEBaseIE):
  37. _VALID_URL = r'https?://learning\.scte\.org/mod/scorm/view\.php?.*?\bid=(?P<id>\d+)'
  38. _TESTS = [{
  39. 'url': 'https://learning.scte.org/mod/scorm/view.php?id=31484',
  40. 'info_dict': {
  41. 'title': 'Introduction to DOCSIS Engineering Professional',
  42. 'id': '31484',
  43. },
  44. 'playlist_count': 5,
  45. 'skip': 'Requires account credentials',
  46. }]
  47. def _real_extract(self, url):
  48. video_id = self._match_id(url)
  49. webpage = self._download_webpage(url, video_id)
  50. title = self._search_regex(r'<h1>(.+?)</h1>', webpage, 'title')
  51. context_id = self._search_regex(r'context-(\d+)', webpage, video_id)
  52. content_base = 'https://learning.scte.org/pluginfile.php/%s/mod_scorm/content/8/' % context_id
  53. context = decode_packed_codes(self._download_webpage(
  54. '%smobile/data.js' % content_base, video_id))
  55. data = self._parse_xml(
  56. self._search_regex(
  57. r'CreateData\(\s*"(.+?)"', context, 'data').replace(r"\'", "'"),
  58. video_id)
  59. entries = []
  60. for asset in data.findall('.//asset'):
  61. asset_url = asset.get('url')
  62. if not asset_url or not asset_url.endswith('.mp4'):
  63. continue
  64. asset_id = self._search_regex(
  65. r'video_([^_]+)_', asset_url, 'asset id', default=None)
  66. if not asset_id:
  67. continue
  68. entries.append({
  69. 'id': asset_id,
  70. 'title': title,
  71. 'url': content_base + asset_url,
  72. })
  73. return self.playlist_result(entries, video_id, title)
  74. class SCTECourseIE(SCTEBaseIE):
  75. _VALID_URL = r'https?://learning\.scte\.org/(?:mod/sub)?course/view\.php?.*?\bid=(?P<id>\d+)'
  76. _TESTS = [{
  77. 'url': 'https://learning.scte.org/mod/subcourse/view.php?id=31491',
  78. 'only_matching': True,
  79. }, {
  80. 'url': 'https://learning.scte.org/course/view.php?id=3639',
  81. 'only_matching': True,
  82. }, {
  83. 'url': 'https://learning.scte.org/course/view.php?id=3073',
  84. 'only_matching': True,
  85. }]
  86. def _real_extract(self, url):
  87. course_id = self._match_id(url)
  88. webpage = self._download_webpage(url, course_id)
  89. title = self._search_regex(
  90. r'<h1>(.+?)</h1>', webpage, 'title', default=None)
  91. entries = []
  92. for mobj in re.finditer(
  93. r'''(?x)
  94. <a[^>]+
  95. href=(["\'])
  96. (?P<url>
  97. https?://learning\.scte\.org/mod/
  98. (?P<kind>scorm|subcourse)/view\.php?(?:(?!\1).)*?
  99. \bid=\d+
  100. )
  101. ''',
  102. webpage):
  103. item_url = mobj.group('url')
  104. if item_url == url:
  105. continue
  106. ie = (SCTEIE.ie_key() if mobj.group('kind') == 'scorm'
  107. else SCTECourseIE.ie_key())
  108. entries.append(self.url_result(item_url, ie=ie))
  109. return self.playlist_result(entries, course_id, title)