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

Code before:
import binascii
import json
import time

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    int_or_none,
    jwt_decode_hs256,
    traverse_obj,
    url_or_none,
)


class WrestleUniverseBaseIE(InfoExtractor):
    _VALID_URL_TMPL = r'https?://(?:www\.)?wrestle-universe\.com/(?:(?P<lang>\w{2})/)?%s/(?P<id>\w+)'
    _API_PATH = None
    _TOKEN = None
    _TOKEN_EXPIRY = None

    def _get_token_cookie(self):
        if not self._TOKEN or not self._TOKEN_EXPIRY:
            token_cookie = self._get_cookies('https://www.wrestle-universe.com/').get('token')
            if not token_cookie or not token_cookie.value:
                self.raise_login_required(method='cookies')
            self._TOKEN = token_cookie.value
            expiry = traverse_obj(jwt_decode_hs256(self._TOKEN), ('exp', {int_or_none}))
            if not expiry:
                raise ExtractorError('There was a problem with the token cookie')
            self._TOKEN_EXPIRY = expiry

        if self._TOKEN_EXPIRY <= int(time.time()):
            raise ExtractorError(
                'Expired token. Refresh your cookies in browser and try again', expected=True)

        return self._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('utf-8')
        if auth:
            headers['Authorization'] = f'Bearer {self._get_token_cookie()}'
        return self._download_json(
            f'https://api.wrestle-universe.com/v1/{self._API_PATH}/{video_id}{param}', video_id,
            note=f'Downloading {msg} JSON', errnote=f'Failed to download {msg} JSON',
            data=data, headers=headers, query=query, fatal=fatal)

    def _call_encrypted_api(self, video_id, param='', msg='API', data={}, query={}, fatal=True):
        # TODO: Use `dependencies.cryptodome`

Review comment (suggestion):
```suggestion
            WrestleUniverseBaseIE._TOKEN = try_call(lambda: self._get_cookies(
                'https://www.wrestle-universe.com/')['token'].value)
            if not self._TOKEN:
                self.raise_login_required()
```

Code after:
import binascii
import json
import time

from .common import InfoExtractor
from ..dependencies import Cryptodome
from ..utils import (
    ExtractorError,
    int_or_none,
    jwt_decode_hs256,
    traverse_obj,
    try_call,
    url_or_none,
)


class WrestleUniverseBaseIE(InfoExtractor):
    _VALID_URL_TMPL = r'https?://(?:www\.)?wrestle-universe\.com/(?:(?P<lang>\w{2})/)?%s/(?P<id>\w+)'
    _API_PATH = None
    _TOKEN = None
    _TOKEN_EXPIRY = None

    def _get_token_cookie(self):
        if not self._TOKEN or not self._TOKEN_EXPIRY:
            self._TOKEN = try_call(lambda: self._get_cookies('https://www.wrestle-universe.com/')['token'].value)
            if not self._TOKEN:
                self.raise_login_required()
            expiry = traverse_obj(jwt_decode_hs256(self._TOKEN), ('exp', {int_or_none}))
            if not expiry:
                raise ExtractorError('There was a problem with the token cookie')
            self._TOKEN_EXPIRY = expiry

        if self._TOKEN_EXPIRY <= int(time.time()):
            raise ExtractorError(
                'Expired token. Refresh your cookies in browser and try again', expected=True)

        return self._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()
        if auth:
            headers['Authorization'] = f'Bearer {self._get_token_cookie()}'
        return self._download_json(
            f'https://api.wrestle-universe.com/v1/{self._API_PATH}/{video_id}{param}', video_id,
            note=f'Downloading {msg} JSON', errnote=f'Failed to download {msg} JSON',
            data=data, headers=headers, query=query, fatal=fatal)

    def _call_encrypted_api(self, video_id, param='', msg='API', data={}, query={}, fatal=True):