[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / scte.py
blob39711325fc671e6f362ccaabb7159370f0f64ada
1 import re
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 decode_packed_codes,
7 urlencode_postdata,
11 class SCTEBaseIE(InfoExtractor):
12 _LOGIN_URL = 'https://www.scte.org/SCTE/Sign_In.aspx'
13 _NETRC_MACHINE = 'scte'
15 def _perform_login(self, username, password):
16 login_popup = self._download_webpage(
17 self._LOGIN_URL, None, 'Downloading login popup')
19 def is_logged(webpage):
20 return any(re.search(p, webpage) for p in (
21 r'class=["\']welcome\b', r'>Sign Out<'))
23 # already logged in
24 if is_logged(login_popup):
25 return
27 login_form = self._hidden_inputs(login_popup)
29 login_form.update({
30 'ctl01$TemplateBody$WebPartManager1$gwpciNewContactSignInCommon$ciNewContactSignInCommon$signInUserName': username,
31 'ctl01$TemplateBody$WebPartManager1$gwpciNewContactSignInCommon$ciNewContactSignInCommon$signInPassword': password,
32 'ctl01$TemplateBody$WebPartManager1$gwpciNewContactSignInCommon$ciNewContactSignInCommon$RememberMe': 'on',
35 response = self._download_webpage(
36 self._LOGIN_URL, None, 'Logging in',
37 data=urlencode_postdata(login_form))
39 if '|pageRedirect|' not in response and not is_logged(response):
40 error = self._html_search_regex(
41 r'(?s)<[^>]+class=["\']AsiError["\'][^>]*>(.+?)</',
42 response, 'error message', default=None)
43 if error:
44 raise ExtractorError(f'Unable to login: {error}', expected=True)
45 raise ExtractorError('Unable to log in')
48 class SCTEIE(SCTEBaseIE):
49 _WORKING = False
50 _VALID_URL = r'https?://learning\.scte\.org/mod/scorm/view\.php?.*?\bid=(?P<id>\d+)'
51 _TESTS = [{
52 'url': 'https://learning.scte.org/mod/scorm/view.php?id=31484',
53 'info_dict': {
54 'title': 'Introduction to DOCSIS Engineering Professional',
55 'id': '31484',
57 'playlist_count': 5,
58 'skip': 'Requires account credentials',
61 def _real_extract(self, url):
62 video_id = self._match_id(url)
64 webpage = self._download_webpage(url, video_id)
66 title = self._search_regex(r'<h1>(.+?)</h1>', webpage, 'title')
68 context_id = self._search_regex(r'context-(\d+)', webpage, video_id)
69 content_base = f'https://learning.scte.org/pluginfile.php/{context_id}/mod_scorm/content/8/'
70 context = decode_packed_codes(self._download_webpage(
71 f'{content_base}mobile/data.js', video_id))
73 data = self._parse_xml(
74 self._search_regex(
75 r'CreateData\(\s*"(.+?)"', context, 'data').replace(r"\'", "'"),
76 video_id)
78 entries = []
79 for asset in data.findall('.//asset'):
80 asset_url = asset.get('url')
81 if not asset_url or not asset_url.endswith('.mp4'):
82 continue
83 asset_id = self._search_regex(
84 r'video_([^_]+)_', asset_url, 'asset id', default=None)
85 if not asset_id:
86 continue
87 entries.append({
88 'id': asset_id,
89 'title': title,
90 'url': content_base + asset_url,
93 return self.playlist_result(entries, video_id, title)
96 class SCTECourseIE(SCTEBaseIE):
97 _WORKING = False
98 _VALID_URL = r'https?://learning\.scte\.org/(?:mod/sub)?course/view\.php?.*?\bid=(?P<id>\d+)'
99 _TESTS = [{
100 'url': 'https://learning.scte.org/mod/subcourse/view.php?id=31491',
101 'only_matching': True,
102 }, {
103 'url': 'https://learning.scte.org/course/view.php?id=3639',
104 'only_matching': True,
105 }, {
106 'url': 'https://learning.scte.org/course/view.php?id=3073',
107 'only_matching': True,
110 def _real_extract(self, url):
111 course_id = self._match_id(url)
113 webpage = self._download_webpage(url, course_id)
115 title = self._search_regex(
116 r'<h1>(.+?)</h1>', webpage, 'title', default=None)
118 entries = []
119 for mobj in re.finditer(
120 r'''(?x)
121 <a[^>]+
122 href=(["\'])
123 (?P<url>
124 https?://learning\.scte\.org/mod/
125 (?P<kind>scorm|subcourse)/view\.php?(?:(?!\1).)*?
126 \bid=\d+
128 ''',
129 webpage):
130 item_url = mobj.group('url')
131 if item_url == url:
132 continue
133 ie = (SCTEIE.ie_key() if mobj.group('kind') == 'scorm'
134 else SCTECourseIE.ie_key())
135 entries.append(self.url_result(item_url, ie=ie))
137 return self.playlist_result(entries, course_id, title)