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

Code before:
from ..utils import traverse_obj


class ZaikoIE(InfoExtractor):
    _VALID_URL = r'https://zaiko\.io/event/(?P<id>\d+)/stream(?:/\d+)+'
    _TESTS = [{
        'url': 'https://zaiko.io/event/324868/stream/20571/20571',
        'info_dict': {
            'id': 324868,
            'ext': 'mp4',
            'title': 'ZAIKO STREAMING TEST',
            'uploader_id': 454,
            'timestamp': 1583809200,
        }
    }]

    def _parse_vue_element_attr(self, name, string):
        page_elem = self._search_regex(r'<' + name + '([^>]+)>', string, name)
        attr = {}
        for m in re.finditer(r'(:?[\w\-_]+)="([^"]+)"', page_elem):
            key, value = m.groups()
            if key[0] == ':':
                key = key[1:]
                value = json.loads(html.unescape(value))
            attr[key] = value
        return attr

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

        webpage = self._download_webpage(url, video_id)
        stream_meta = self._parse_vue_element_attr('stream-page', webpage)

        headers = {'referer': 'https://zaiko.io/'}
        player_page = self._download_webpage(stream_meta['stream-access']['video_source'], video_id, headers=headers)
        player_meta = self._parse_vue_element_attr('player', player_page)

        return {
            'id': video_id,
            'title': stream_meta['event']['name'],
            "alt_title": traverse_obj(player_meta, ("initial_event_info", "title")),
            'formats': self._extract_m3u8_formats(player_meta["initial_event_info"]["endpoint"], video_id),
            "thumbnail": traverse_obj(player_meta, ("initial_event_info", "poster_url")),
            "uploader": traverse_obj(stream_meta, ("profile", "name")),
            "timestamp": traverse_obj(stream_meta, ("stream", "start", "timestamp")),
            "release_timestamp": traverse_obj(stream_meta, ("stream", "start", "timestamp")),
            "uploader_id": traverse_obj(stream_meta, ("profile", "id")),
            "categories": traverse_obj(stream_meta, ("event", "genres")),
            "_raw": [stream_meta, player_meta],
        }


Review comment (security):
I like the idea here. We should use the tools already in the yt-dlp toolbox, though:
 - non-fatal lenient JSON parsing with `_parse_json()`
 - and utils imports: `extract_attributes`, `remove_start`, `unescapeHTML`
```suggestion
    def _parse_vue_element_attr(self, name, string, video_id):
        page_elem = self._search_regex(rf'(<{name}[^>]+>)', string, name)
        attrs = {}
        for key, value in extract_attributes(page_elem).items():
            attrs[remove_start(key, ':')] = self._parse_json(
                value, video_id, transform_source=unescapeHTML, fatal=False)
        return attrs
```

Code after:
    str_or_none,
    traverse_obj,
    unescapeHTML,
    url_or_none,
)


class ZaikoIE(InfoExtractor):
    _VALID_URL = r'https?://(?:[\w-]+\.)?zaiko\.io/event/(?P<id>\d+)/stream(?:/\d+)+'
    _TESTS = [{
        'url': 'https://zaiko.io/event/324868/stream/20571/20571',
        'info_dict': {
            'id': '324868',
            'ext': 'mp4',
            'title': 'ZAIKO STREAMING TEST',
            'alt_title': '[VOD] ZAIKO STREAMING TEST_20210603(Do Not Delete)',
            'uploader_id': '454',
            'uploader': 'ZAIKO ZERO',
            'release_timestamp': 1583809200,
            'thumbnail': r're:https://[a-z0-9]+.cloudfront.net/[a-z0-9_]+/[a-z0-9_]+',
            'release_date': '20200310',
            'categories': ['Tech House'],
            'live_status': 'was_live',
        },
        'params': {'skip_download': 'm3u8'},
    }]

    def _parse_vue_element_attr(self, name, string, video_id):
        page_elem = self._search_regex(rf'(<{name}[^>]+>)', string, name)
        attrs = {}
        for key, value in extract_attributes(page_elem).items():
            if key.startswith(':'):
                attrs[key[1:]] = self._parse_json(
                    value, video_id, transform_source=unescapeHTML, fatal=False)
        return attrs

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

        webpage, urlh = self._download_webpage_handle(url, video_id)
        final_url = urlh.geturl()
        if 'zaiko.io/login' in final_url:
            self.raise_login_required()
        elif '/_buy/' in final_url:
            raise ExtractorError('Your account does not have tickets to this event', expected=True)
        stream_meta = self._parse_vue_element_attr('stream-page', webpage, video_id)

        player_page = self._download_webpage(
            stream_meta['stream-access']['video_source'], video_id,
            'Downloading player page', headers={'referer': 'https://zaiko.io/'})
        player_meta = self._parse_vue_element_attr('player', player_page, video_id)