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

Code before:
import json

from .common import InfoExtractor
from ..utils import ExtractorError, traverse_obj


class CanalsurmasIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?canalsurmas\.es/videos/(?P<id>\d+)'

    _TESTS = [
        {
            'url': 'https://www.canalsurmas.es/videos/44006-el-gran-queo-1-lora-del-rio-sevilla-20072014',
            'md5': '861f86fdc1221175e15523047d0087ef',
            'info_dict': {
                'id': '44006',
                'ext': 'mp4',
                'title': 'Lora del Río (Sevilla)  ',
                'description': 'md5:3d9ee40a9b1b26ed8259e6b71ed27b8b',
                'thumbnail': 'https://cdn2.rtva.interactvty.com/content_cards/00f3e8f67b0a4f3b90a4a14618a48b0d.jpg',
                'tags': [],
            },
        },
    ]

    def _real_extract(self, url):
        video_id = self._match_id(url)

        access_token: str | None = traverse_obj(
            self._download_json(
                url_or_request='https://api-rtva.interactvty.com/jwt/token/',
                headers={
                    'Content-Type': 'application/json',
                },
                data=json.dumps(
                    {
                        'username': 'canalsur_demo',
                        'password': 'dsUBXUcI',
                    },
                ).encode(),
                video_id=video_id,
            ),
            ('access'),
        )

        if access_token is None:
            raise ExtractorError('Failed to retrieve a required access token.')

        video_info = self._download_json(

Review comment (refactor):
Adjusting the formatting to be in line with other extractors
```suggestion
    _TESTS = [{
        'url': 'https://www.canalsurmas.es/videos/44006-el-gran-queo-1-lora-del-rio-sevilla-20072014',
        'md5': '861f86fdc1221175e15523047d0087ef',
        'info_dict': {
            'id': '44006',
            'ext': 'mp4',
            'title': 'Lora del Río (Sevilla)',
            'description': 'md5:3d9ee40a9b1b26ed8259e6b71ed27b8b',
            'thumbnail': 'https://cdn2.rtva.interactvty.com/content_cards/00f3e8f67b0a4f3b90a4a14618a48b0d.jpg',
            'timestamp': 1648123182,
            'upload_date': '20220324',
        },
    }]
```

Code after:
import json
import time

from .common import InfoExtractor
from ..utils import (
    determine_ext,
    float_or_none,
    jwt_decode_hs256,
    parse_iso8601,
    url_or_none,
    variadic,
)
from ..utils.traversal import traverse_obj


class CanalsurmasIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?canalsurmas\.es/videos/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://www.canalsurmas.es/videos/44006-el-gran-queo-1-lora-del-rio-sevilla-20072014',
        'md5': '861f86fdc1221175e15523047d0087ef',
        'info_dict': {
            'id': '44006',
            'ext': 'mp4',
            'title': 'Lora del Río (Sevilla)',
            'description': 'md5:3d9ee40a9b1b26ed8259e6b71ed27b8b',
            'thumbnail': 'https://cdn2.rtva.interactvty.com/content_cards/00f3e8f67b0a4f3b90a4a14618a48b0d.jpg',
            'timestamp': 1648123182,
            'upload_date': '20220324',
        },
    }]
    _API_BASE = 'https://api-rtva.interactvty.com'
    _access_token = None

    @staticmethod
    def _is_jwt_expired(token):
        return jwt_decode_hs256(token)['exp'] - time.time() < 300

    def _call_api(self, endpoint, video_id, fields=None):
        if not self._access_token or self._is_jwt_expired(self._access_token):
            self._access_token = self._download_json(
                f'{self._API_BASE}/jwt/token/', None,
                'Downloading access token', 'Failed to download access token',
                headers={'Content-Type': 'application/json'},
                data=json.dumps({
                    'username': 'canalsur_demo',
                    'password': 'dsUBXUcI',
                }).encode())['access']