Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/brilliantpala.py (Python)

Code before:

    def _initialize_pre_login(self):
        self._HOMEPAGE = f'https://{self._DOMAIN}'
        self._LOGIN_API = f'{self._HOMEPAGE}/login/'
        self._LOGOUT_DEVICES_API = f'{self._HOMEPAGE}/logout_devices/?next=/'
        self._CONTENT_API = f'{self._HOMEPAGE}/api/v2.4/contents/{{content_id}}/'
        self._HLS_AES_URI = f'{self._HOMEPAGE}/api/v2.5/video_contents/{{content_id}}/key/'

    def _get_logged_in_username(self, url, video_id):
        webpage, urlh = self._download_webpage_handle(url, video_id)
        if urlh.url.startswith(self._LOGIN_API):
            self.raise_login_required()
        return self._html_search_regex(
            r'"username"\s*:\s*"(?P<username>[^"]+)"', webpage, 'logged-in username')

    def _perform_login(self, username, password):
        login_page, urlh = self._download_webpage_handle(
            self._LOGIN_API, None, 'Downloading login page', expected_status=401)
        if not (urlh.status == 401 or urlh.url.startswith(self._LOGIN_API)):
            self.write_debug('Cookies are valid, no login required.')
            return

        if urlh.status == 401:
            # The stored cookies have been invalidated.
            # No login page will be returned and cookies will be reset, so visit the page again.
            login_page = self._download_webpage(self._LOGIN_API, None, 'Downloading login page')

        login_form = self._hidden_inputs(login_page)
        login_form.update({
            'username': username,
            'password': password,
        })
        self._set_cookie(self._DOMAIN, 'csrftoken', login_form['csrfmiddlewaretoken'])

        logged_page = self._download_webpage(
            self._LOGIN_API, None, note='Logging in', headers={'Referer': self._LOGIN_API},
            data=urlencode_postdata(login_form))

        if self._html_search_regex(
                r'(Your username / email and password)', logged_page, 'auth fail', default=None):
            raise ExtractorError('wrong username or password', expected=True)

        # the maximum number of logins is one
        if self._html_search_regex(
                r'(Logout Other Devices)', logged_page, 'logout devices button', default=None):
            logout_device_form = self._hidden_inputs(logged_page)
            self._download_webpage(
                self._LOGOUT_DEVICES_API, None, headers={'Referer': self._LOGIN_API},
                note='Logging out other devices', data=urlencode_postdata(logout_device_form))

    def _real_extract(self, url):

Review comment (bug):
```suggestion
            self.write_debug('Got HTTP Error 401; cookies have been invalidated')
            login_page = self._download_webpage(self._LOGIN_API, None, 'Re-downloading login page')
```

Code after:

    def _initialize_pre_login(self):
        self._HOMEPAGE = f'https://{self._DOMAIN}'
        self._LOGIN_API = f'{self._HOMEPAGE}/login/'
        self._LOGOUT_DEVICES_API = f'{self._HOMEPAGE}/logout_devices/?next=/'
        self._CONTENT_API = f'{self._HOMEPAGE}/api/v2.4/contents/{{content_id}}/'
        self._HLS_AES_URI = f'{self._HOMEPAGE}/api/v2.5/video_contents/{{content_id}}/key/'

    def _get_logged_in_username(self, url, video_id):
        webpage, urlh = self._download_webpage_handle(url, video_id)
        if urlh.url.startswith(self._LOGIN_API):
            self.raise_login_required()
        return self._html_search_regex(
            r'"username"\s*:\s*"(?P<username>[^"]+)"', webpage, 'logged-in username')

    def _perform_login(self, username, password):
        login_page, urlh = self._download_webpage_handle(
            self._LOGIN_API, None, 'Downloading login page', expected_status=401)
        if urlh.status != 401 and not urlh.url.startswith(self._LOGIN_API):
            self.write_debug('Cookies are valid, no login required.')
            return

        if urlh.status == 401:
            self.write_debug('Got HTTP Error 401; cookies have been invalidated')
            login_page = self._download_webpage(self._LOGIN_API, None, 'Re-downloading login page')

        login_form = self._hidden_inputs(login_page)
        login_form.update({
            'username': username,
            'password': password,
        })
        self._set_cookie(self._DOMAIN, 'csrftoken', login_form['csrfmiddlewaretoken'])

        logged_page = self._download_webpage(
            self._LOGIN_API, None, note='Logging in', headers={'Referer': self._LOGIN_API},
            data=urlencode_postdata(login_form))

        if self._html_search_regex(
                r'(Your username / email and password)', logged_page, 'auth fail', default=None):
            raise ExtractorError('wrong username or password', expected=True)

        # the maximum number of logins is one
        if self._html_search_regex(
                r'(Logout Other Devices)', logged_page, 'logout devices button', default=None):
            logout_device_form = self._hidden_inputs(logged_page)
            self._download_webpage(
                self._LOGOUT_DEVICES_API, None, headers={'Referer': self._LOGIN_API},
                note='Logging out other devices', data=urlencode_postdata(logout_device_form))

    def _real_extract(self, url):
        course_id, content_id = self._match_valid_url(url).group('course_id', 'content_id')