]>
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): | |
30 | login_form = self._hidden_inputs(self._download_webpage( | |
31 | self._LOGIN_API, None, 'Downloading login page')) | |
32 | login_form.update({ | |
33 | 'username': username, | |
34 | 'password': password, | |
35 | }) | |
36 | self._set_cookie(self._DOMAIN, 'csrftoken', login_form['csrfmiddlewaretoken']) | |
37 | ||
38 | logged_page = self._download_webpage( | |
39 | self._LOGIN_API, None, note='Logging in', headers={'Referer': self._LOGIN_API}, | |
40 | data=urlencode_postdata(login_form)) | |
41 | ||
42 | if self._html_search_regex( | |
43 | r'(Your username / email and password)', logged_page, 'auth fail', default=None): | |
44 | raise ExtractorError('wrong username or password', expected=True) | |
45 | ||
46 | # the maximum number of logins is one | |
47 | if self._html_search_regex( | |
48 | r'(Logout Other Devices)', logged_page, 'logout devices button', default=None): | |
49 | logout_device_form = self._hidden_inputs(logged_page) | |
50 | self._download_webpage( | |
51 | self._LOGOUT_DEVICES_API, None, headers={'Referer': self._LOGIN_API}, | |
52 | note='Logging out other devices', data=urlencode_postdata(logout_device_form)) | |
53 | ||
54 | def _real_extract(self, url): | |
55 | course_id, content_id = self._match_valid_url(url).group('course_id', 'content_id') | |
56 | video_id = f'{course_id}-{content_id}' | |
57 | ||
58 | username = self._get_logged_in_username(url, video_id) | |
59 | ||
60 | content_json = self._download_json( | |
61 | self._CONTENT_API.format(content_id=content_id), video_id, | |
62 | note='Fetching content info', errnote='Unable to fetch content info') | |
63 | ||
64 | entries = [] | |
65 | for stream in traverse_obj(content_json, ('video', 'streams', lambda _, v: v['id'] and v['url'])): | |
66 | formats = self._extract_m3u8_formats(stream['url'], video_id, fatal=False) | |
67 | if not formats: | |
68 | continue | |
69 | entries.append({ | |
70 | 'id': str(stream['id']), | |
71 | 'title': content_json.get('title'), | |
72 | 'formats': formats, | |
73 | 'hls_aes': {'uri': self._HLS_AES_URI.format(content_id=content_id)}, | |
74 | 'http_headers': {'X-Key': hashlib.sha256(username.encode('ascii')).hexdigest()}, | |
75 | 'thumbnail': content_json.get('cover_image'), | |
76 | }) | |
77 | ||
78 | return self.playlist_result( | |
79 | entries, playlist_id=video_id, playlist_title=content_json.get('title')) | |
80 | ||
81 | ||
82 | class BrilliantpalaElearnIE(BrilliantpalaBaseIE): | |
83 | IE_NAME = 'Brilliantpala:Elearn' | |
84 | IE_DESC = 'VoD on elearn.brilliantpala.org' | |
85 | _VALID_URL = r'https?://elearn\.brilliantpala\.org/courses/(?P<course_id>\d+)/contents/(?P<content_id>\d+)/?' | |
86 | _TESTS = [{ | |
87 | 'url': 'https://elearn.brilliantpala.org/courses/42/contents/12345/', | |
88 | 'only_matching': True, | |
89 | }, { | |
90 | 'url': 'https://elearn.brilliantpala.org/courses/98/contents/36683/', | |
91 | 'info_dict': { | |
92 | 'id': '23577', | |
93 | 'ext': 'mp4', | |
94 | 'title': 'Physical World, Units and Measurements - 1', | |
95 | 'thumbnail': 'https://d1j3vi2u94ebt0.cloudfront.net/institute/brilliantpalalms/chapter_contents/26237/e657f81b90874be19795c7ea081f8d5c.png', | |
96 | 'live_status': 'not_live', | |
97 | }, | |
98 | 'params': { | |
99 | 'skip_download': True, | |
100 | }, | |
101 | }] | |
102 | ||
103 | _DOMAIN = BrilliantpalaBaseIE._DOMAIN.format(subdomain='elearn') | |
104 | ||
105 | ||
106 | class BrilliantpalaClassesIE(BrilliantpalaBaseIE): | |
107 | IE_NAME = 'Brilliantpala:Classes' | |
108 | IE_DESC = 'VoD on classes.brilliantpala.org' | |
109 | _VALID_URL = r'https?://classes\.brilliantpala\.org/courses/(?P<course_id>\d+)/contents/(?P<content_id>\d+)/?' | |
110 | _TESTS = [{ | |
111 | 'url': 'https://classes.brilliantpala.org/courses/42/contents/12345/', | |
112 | 'only_matching': True, | |
113 | }, { | |
114 | 'url': 'https://classes.brilliantpala.org/courses/416/contents/25445/', | |
115 | 'info_dict': { | |
116 | 'id': '9128', | |
117 | 'ext': 'mp4', | |
118 | 'title': 'Motion in a Straight Line - Class 1', | |
119 | 'thumbnail': 'https://d3e4y8hquds3ek.cloudfront.net/institute/brilliantpalaelearn/chapter_contents/ff5ba838d0ec43419f67387fe1a01fa8.png', | |
120 | 'live_status': 'not_live', | |
121 | }, | |
122 | 'params': { | |
123 | 'skip_download': True, | |
124 | }, | |
125 | }] | |
126 | ||
127 | _DOMAIN = BrilliantpalaBaseIE._DOMAIN.format(subdomain='classes') |