]>
Commit | Line | Data |
---|---|---|
92feb565 M |
1 | import hashlib |
2 | ||
3 | from .common import InfoExtractor | |
4 | from ..utils import ( | |
5 | ExtractorError, | |
6 | traverse_obj, | |
7 | urlencode_postdata, | |
8 | ) | |
9 | ||
10 | ||
11 | class BrilliantpalaBaseIE(InfoExtractor): | |
12 | _NETRC_MACHINE = 'brilliantpala' | |
13 | _DOMAIN = '{subdomain}.brilliantpala.org' | |
14 | ||
15 | def _initialize_pre_login(self): | |
16 | self._HOMEPAGE = f'https://{self._DOMAIN}' | |
17 | self._LOGIN_API = f'{self._HOMEPAGE}/login/' | |
18 | self._LOGOUT_DEVICES_API = f'{self._HOMEPAGE}/logout_devices/?next=/' | |
19 | self._CONTENT_API = f'{self._HOMEPAGE}/api/v2.4/contents/{{content_id}}/' | |
20 | self._HLS_AES_URI = f'{self._HOMEPAGE}/api/v2.5/video_contents/{{content_id}}/key/' | |
21 | ||
22 | def _get_logged_in_username(self, url, video_id): | |
23 | webpage, urlh = self._download_webpage_handle(url, video_id) | |
9b5bedf1 | 24 | if urlh.url.startswith(self._LOGIN_API): |
92feb565 M |
25 | self.raise_login_required() |
26 | return self._html_search_regex( | |
9b5bedf1 | 27 | r'"username"\s*:\s*"(?P<username>[^"]+)"', webpage, 'logged-in username') |
92feb565 M |
28 | |
29 | def _perform_login(self, username, password): | |
eead3bbc M |
30 | login_page, urlh = self._download_webpage_handle( |
31 | self._LOGIN_API, None, 'Downloading login page', expected_status=401) | |
32 | if urlh.status != 401 and not urlh.url.startswith(self._LOGIN_API): | |
33 | self.write_debug('Cookies are valid, no login required.') | |
34 | return | |
35 | ||
36 | if urlh.status == 401: | |
37 | self.write_debug('Got HTTP Error 401; cookies have been invalidated') | |
38 | login_page = self._download_webpage(self._LOGIN_API, None, 'Re-downloading login page') | |
39 | ||
40 | login_form = self._hidden_inputs(login_page) | |
92feb565 M |
41 | login_form.update({ |
42 | 'username': username, | |
43 | 'password': password, | |
44 | }) | |
45 | self._set_cookie(self._DOMAIN, 'csrftoken', login_form['csrfmiddlewaretoken']) | |
46 | ||
47 | logged_page = self._download_webpage( | |
48 | self._LOGIN_API, None, note='Logging in', headers={'Referer': self._LOGIN_API}, | |
49 | data=urlencode_postdata(login_form)) | |
50 | ||
51 | if self._html_search_regex( | |
52 | r'(Your username / email and password)', logged_page, 'auth fail', default=None): | |
53 | raise ExtractorError('wrong username or password', expected=True) | |
54 | ||
55 | # the maximum number of logins is one | |
56 | if self._html_search_regex( | |
57 | r'(Logout Other Devices)', logged_page, 'logout devices button', default=None): | |
58 | logout_device_form = self._hidden_inputs(logged_page) | |
59 | self._download_webpage( | |
60 | self._LOGOUT_DEVICES_API, None, headers={'Referer': self._LOGIN_API}, | |
61 | note='Logging out other devices', data=urlencode_postdata(logout_device_form)) | |
62 | ||
63 | def _real_extract(self, url): | |
64 | course_id, content_id = self._match_valid_url(url).group('course_id', 'content_id') | |
65 | video_id = f'{course_id}-{content_id}' | |
66 | ||
67 | username = self._get_logged_in_username(url, video_id) | |
68 | ||
69 | content_json = self._download_json( | |
70 | self._CONTENT_API.format(content_id=content_id), video_id, | |
71 | note='Fetching content info', errnote='Unable to fetch content info') | |
72 | ||
73 | entries = [] | |
74 | for stream in traverse_obj(content_json, ('video', 'streams', lambda _, v: v['id'] and v['url'])): | |
75 | formats = self._extract_m3u8_formats(stream['url'], video_id, fatal=False) | |
76 | if not formats: | |
77 | continue | |
78 | entries.append({ | |
79 | 'id': str(stream['id']), | |
80 | 'title': content_json.get('title'), | |
81 | 'formats': formats, | |
82 | 'hls_aes': {'uri': self._HLS_AES_URI.format(content_id=content_id)}, | |
83 | 'http_headers': {'X-Key': hashlib.sha256(username.encode('ascii')).hexdigest()}, | |
84 | 'thumbnail': content_json.get('cover_image'), | |
85 | }) | |
86 | ||
87 | return self.playlist_result( | |
88 | entries, playlist_id=video_id, playlist_title=content_json.get('title')) | |
89 | ||
90 | ||
91 | class BrilliantpalaElearnIE(BrilliantpalaBaseIE): | |
92 | IE_NAME = 'Brilliantpala:Elearn' | |
93 | IE_DESC = 'VoD on elearn.brilliantpala.org' | |
94 | _VALID_URL = r'https?://elearn\.brilliantpala\.org/courses/(?P<course_id>\d+)/contents/(?P<content_id>\d+)/?' | |
95 | _TESTS = [{ | |
96 | 'url': 'https://elearn.brilliantpala.org/courses/42/contents/12345/', | |
97 | 'only_matching': True, | |
98 | }, { | |
99 | 'url': 'https://elearn.brilliantpala.org/courses/98/contents/36683/', | |
100 | 'info_dict': { | |
101 | 'id': '23577', | |
102 | 'ext': 'mp4', | |
103 | 'title': 'Physical World, Units and Measurements - 1', | |
104 | 'thumbnail': 'https://d1j3vi2u94ebt0.cloudfront.net/institute/brilliantpalalms/chapter_contents/26237/e657f81b90874be19795c7ea081f8d5c.png', | |
105 | 'live_status': 'not_live', | |
106 | }, | |
107 | 'params': { | |
108 | 'skip_download': True, | |
109 | }, | |
110 | }] | |
111 | ||
112 | _DOMAIN = BrilliantpalaBaseIE._DOMAIN.format(subdomain='elearn') | |
113 | ||
114 | ||
115 | class BrilliantpalaClassesIE(BrilliantpalaBaseIE): | |
116 | IE_NAME = 'Brilliantpala:Classes' | |
117 | IE_DESC = 'VoD on classes.brilliantpala.org' | |
118 | _VALID_URL = r'https?://classes\.brilliantpala\.org/courses/(?P<course_id>\d+)/contents/(?P<content_id>\d+)/?' | |
119 | _TESTS = [{ | |
120 | 'url': 'https://classes.brilliantpala.org/courses/42/contents/12345/', | |
121 | 'only_matching': True, | |
122 | }, { | |
123 | 'url': 'https://classes.brilliantpala.org/courses/416/contents/25445/', | |
124 | 'info_dict': { | |
125 | 'id': '9128', | |
126 | 'ext': 'mp4', | |
127 | 'title': 'Motion in a Straight Line - Class 1', | |
128 | 'thumbnail': 'https://d3e4y8hquds3ek.cloudfront.net/institute/brilliantpalaelearn/chapter_contents/ff5ba838d0ec43419f67387fe1a01fa8.png', | |
129 | 'live_status': 'not_live', | |
130 | }, | |
131 | 'params': { | |
132 | 'skip_download': True, | |
133 | }, | |
134 | }] | |
135 | ||
136 | _DOMAIN = BrilliantpalaBaseIE._DOMAIN.format(subdomain='classes') |