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

Code before:
import math

from .common import InfoExtractor
from ..utils import (
    InAdvancePagedList,
    int_or_none,
    str_or_none,
    traverse_obj,
    unified_strdate,
    url_or_none,
)


class SubsplashVideoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?subsplash\.com/u/[^/]+/media/d/(?P<id>\w+)'
    _TESTS = [{
        'url': 'https://subsplash.com/u/skywatchtv/media/d/5whnx5s-the-grand-delusion-taking-place-right-now',
        'md5': '2d67c50deac3c6c49c6e25c4a5b25afe',
        'info_dict': {
            'id': '33f8d305-68ab-414c-acf2-f2317a0abe21',
            'ext': 'mp4',
            'title': 'THE GRAND DELUSION TAKING PLACE RIGHT NOW!',
            'description': 'md5:220a630865c3697b0ec9dcb3a70cbc33',
            'upload_date': '20240901',
            'duration': 1710,
            'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
        },
    }, {
        'url': 'https://subsplash.com/u/prophecywatchers/media/d/n4dr8b2-the-transhumanist-plan-for-humanity-billy-crone',
        'md5': 'f7b4109ba7f012dff953391d6b400730',
        'info_dict': {
            'id': 'e16348f1-040e-4596-b922-77b45fa8d253',
            'ext': 'mp4',
            'title': 'The Transhumanist Plan for Humanity | Billy Crone',
            'description': None,
            'upload_date': '20240903',
            'duration': 1709,
            'thumbnail': r're:^https?://.*\.(?:jpg|png)$',
        },
    }]

    def _fetch_json(self, url, display_id, token):
        return self._download_json(url, display_id, headers={'Authorization': f'Bearer {token}'})

    def _extract_metadata(self, data, display_id):
        return traverse_obj(data, {
            'id': ('id', {str_or_none}),
            'title': ('title', {str_or_none}),
            'description': ('summary_text', {str_or_none}),
            'thumbnail': ('_embedded', 'images', 0, '_links', 'related', 'href', {url_or_none}),
            'duration': ('_embedded', 'video', 'duration', {lambda x: int_or_none(x, 1000)}),

Review comment (suggestion):
```suggestion
            'thumbnail': r're:https?://.*\.(?:jpg|png)$',
```
See https://discord.com/channels/807245652072857610/1112613156934668338/1293369656098160702. `re.match` is used here when running tests(which matches from the start of the string already)


Code after:
import math

from .common import InfoExtractor
from ..utils import (
    InAdvancePagedList,
    int_or_none,
    parse_iso8601,
    try_call,
    url_or_none,
)
from ..utils.traversal import traverse_obj


class SubsplashBaseIE(InfoExtractor):
    def _get_headers(self, url, display_id):
        token = try_call(lambda: self._get_cookies(url)['ss-token-guest'].value)
        if not token:
            webpage, urlh = self._download_webpage_handle(url, display_id)
            token = (
                try_call(lambda: self._get_cookies(url)['ss-token-guest'].value)
                or urlh.get_header('x-api-token')
                or self._search_json(
                    r'<script[^>]+\bid="shoebox-tokens"[^>]*>', webpage, 'shoebox tokens',
                    display_id, default={}).get('apiToken')
                or self._search_regex(r'\\"tokens\\":{\\"guest\\":\\"([A-Za-z0-9._-]+)\\"', webpage, 'token', default=None))

        if not token:
            self.report_warning('Unable to extract auth token')
            return None
        return {'Authorization': f'Bearer {token}'}

    def _extract_video(self, data, video_id):
        formats = []
        video_data = traverse_obj(data, ('_embedded', 'video', '_embedded', {dict}))
        m3u8_url = traverse_obj(video_data, ('playlists', 0, '_links', 'related', 'href', {url_or_none}))
        if m3u8_url:
            formats.extend(self._extract_m3u8_formats(m3u8_url, video_id, 'mp4', m3u8_id='hls', fatal=False))
        mp4_entry = traverse_obj(video_data, ('video-outputs', lambda _, v: url_or_none(v['_links']['related']['href']), any))
        if mp4_entry:
            formats.append({
                'url': mp4_entry['_links']['related']['href'],
                'format_id': 'direct',
                'quality': 1,
                **traverse_obj(mp4_entry, {
                    'height': ('height', {int_or_none}),
                    'width': ('width', {int_or_none}),
                    'filesize': ('file_size', {int_or_none}),
                }),
            })
        return {
            'id': video_id,