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

Code before:
            raise ExtractorError('There was a problem with the auth token')
        self._TOKEN_EXPIRY = expiry

    def _perform_login(self, username, password):
        login = self._download_json(
            'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword', None,
            'Logging in', query=self._LOGIN_QUERY, headers=self._LOGIN_HEADERS, data=json.dumps({
                'returnSecureToken': True,
                'email': username,
                'password': password,
            }, separators=(',', ':')).encode())
        self._REFRESH_TOKEN = traverse_obj(login, ('refreshToken', {str}))
        if not self._REFRESH_TOKEN:
            self.report_warning('No refresh token was granted')
        self._TOKEN = traverse_obj(login, ('idToken', {str}))

    def _real_initialize(self):
        if WrestleUniverseBaseIE._DEVICE_ID:
            return

        WrestleUniverseBaseIE._DEVICE_ID = self._configuration_arg('device_id', [None], ie_key='WrestleUniverse')[0]
        if not WrestleUniverseBaseIE._DEVICE_ID:
            WrestleUniverseBaseIE._DEVICE_ID = self.cache.load(self._NETRC_MACHINE, 'device_id')
            if WrestleUniverseBaseIE._DEVICE_ID:
                return
            WrestleUniverseBaseIE._DEVICE_ID = str(uuid.uuid4())

        self.cache.store(self._NETRC_MACHINE, 'device_id', WrestleUniverseBaseIE._DEVICE_ID)

    def _refresh_token(self):
        refresh = self._download_json(
            'https://securetoken.googleapis.com/v1/token', None, 'Refreshing token',
            query=self._LOGIN_QUERY, data=urlencode_postdata({
                'grant_type': 'refresh_token',
                'refresh_token': self._REFRESH_TOKEN,
            }), headers={
                **self._LOGIN_HEADERS,
                'Content-Type': 'application/x-www-form-urlencoded',
            })
        if traverse_obj(refresh, ('refresh_token', {str})):
            self._REFRESH_TOKEN = refresh['refresh_token']
        token = traverse_obj(refresh, 'access_token', 'id_token', expected_type=str)
        if not token:
            raise ExtractorError('No auth token returned from refresh request')
        self._TOKEN = token

    def _call_api(self, video_id, param='', msg='API', auth=True, data=None, query={}, fatal=True):
        headers = {'CA-CID': ''}
        if data:
            headers['Content-Type'] = 'application/json;charset=utf-8'
            data = json.dumps(data, separators=(',', ':')).encode()

Review comment (refactor):
```suggestion
        login = self._download_json(
            'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword', None,
            'Logging in', query=self._LOGIN_QUERY, headers=self._LOGIN_HEADERS, data=json.dumps({
                'returnSecureToken': True,
                'email': username,
                'password': password,
            }, separators=(',', ':')).encode(), expected_status=400)
        token = traverse_obj(login, ('idToken', {str}))
        if not token:
            raise ExtractorError(
                f'Unable to log in: {traverse_obj(login, ("error", "message"))}', expected=True)
        self._REFRESH_TOKEN = traverse_obj(login, ('refreshToken', {str}))
        if not self._REFRESH_TOKEN:
            self.report_warning('No refresh token was granted')
        self._TOKEN = token
```

Code after:
        expiry = traverse_obj(value, ({jwt_decode_hs256}, 'exp', {int_or_none}))
        if not expiry:
            raise ExtractorError('There was a problem with the auth token')
        self._TOKEN_EXPIRY = expiry

    def _perform_login(self, username, password):
        login = self._download_json(
            'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword', None,
            'Logging in', query=self._LOGIN_QUERY, headers=self._LOGIN_HEADERS, data=json.dumps({
                'returnSecureToken': True,
                'email': username,
                'password': password,
            }, separators=(',', ':')).encode(), expected_status=400)
        token = traverse_obj(login, ('idToken', {str}))
        if not token:
            raise ExtractorError(
                f'Unable to log in: {traverse_obj(login, ("error", "message"))}', expected=True)
        self._REFRESH_TOKEN = traverse_obj(login, ('refreshToken', {str}))
        if not self._REFRESH_TOKEN:
            self.report_warning('No refresh token was granted')
        self._TOKEN = token

    def _real_initialize(self):
        if self._DEVICE_ID:
            return

        self._DEVICE_ID = self._configuration_arg('device_id', [None], ie_key=self._NETRC_MACHINE)[0]
        if not self._DEVICE_ID:
            self._DEVICE_ID = self.cache.load(self._NETRC_MACHINE, 'device_id')
            if self._DEVICE_ID:
                return
            self._DEVICE_ID = str(uuid.uuid4())

        self.cache.store(self._NETRC_MACHINE, 'device_id', self._DEVICE_ID)

    def _refresh_token(self):
        refresh = self._download_json(
            'https://securetoken.googleapis.com/v1/token', None, 'Refreshing token',
            query=self._LOGIN_QUERY, data=urlencode_postdata({
                'grant_type': 'refresh_token',
                'refresh_token': self._REFRESH_TOKEN,
            }), headers={
                **self._LOGIN_HEADERS,
                'Content-Type': 'application/x-www-form-urlencoded',
            })
        if traverse_obj(refresh, ('refresh_token', {str})):
            self._REFRESH_TOKEN = refresh['refresh_token']
        token = traverse_obj(refresh, 'access_token', 'id_token', expected_type=str)
        if not token:
            raise ExtractorError('No auth token returned from refresh request')
        self._TOKEN = token