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

Code before:
from .common import InfoExtractor
from ..utils import (
    clean_html,
    merge_dicts,
    try_get
)


class LRTStreamIE(InfoExtractor):
    _VALID_URL = r'https?://(?:www\.)?lrt\.lt/mediateka/tiesiogiai/(?P<id>[\w-]+)'
    _TESTS = [{
        'url': 'https://www.lrt.lt/mediateka/tiesiogiai/lrt-opus',
        'info_dict': {
            'id': 'lrt-opus',
            'live_status': 'is_live',
            'title': 're:^LRT Opus.+$',
            'ext': 'mp4'
        }
    }]

    def _extract_js_var(self, webpage, var_name, default):
        return self._search_regex(
            r'%s\s*=\s*(["\'])((?:(?!\1).)+)\1' % var_name,
            webpage, var_name.replace('_', ' '), default, group=2)

    def _real_extract(self, url):
        matches = self._match_valid_url(url).groupdict()
        video_id = matches['id']

        webpage = self._download_webpage(url, video_id)
        token_url = self._extract_js_var(webpage, 'tokenURL', None)
        stream_title = self._extract_js_var(webpage, 'video_title', 'LRT')
        title = self._og_search_title(webpage)

        streams_data = self._download_json(token_url, video_id)

        formats = []
        for key in ['content', 'content2', 'content3']:
            stream_url = try_get(streams_data, lambda x: x['response']['data'][key])
            if stream_url == '':
                continue
            formats.extend(self._extract_m3u8_formats(stream_url, video_id, m3u8_id='hls', live=True))

        self._sort_formats(formats)

        return {
            'id': video_id,
            'formats': formats,
            'is_live': True,

Review comment (suggestion):
this code is repeated. Either move it into a base class, or, if the code is robust enough*, this could be useful in common.py as well

\* it needs a `\s`/`\b` at the start. Otherwise, looks robust to me

Code after:
from .common import InfoExtractor
from ..utils import (
    clean_html,
    merge_dicts,
    traverse_obj,
    url_or_none,
)


class LRTBaseIE(InfoExtractor):
    def _extract_js_var(self, webpage, var_name, default=None):
        return self._search_regex(
            fr'{var_name}\s*=\s*(["\'])((?:(?!\1).)+)\1',
            webpage, var_name.replace('_', ' '), default, group=2)


class LRTStreamIE(LRTBaseIE):
    _VALID_URL = r'https?://(?:www\.)?lrt\.lt/mediateka/tiesiogiai/(?P<id>[\w-]+)'
    _TESTS = [{
        'url': 'https://www.lrt.lt/mediateka/tiesiogiai/lrt-opus',
        'info_dict': {
            'id': 'lrt-opus',
            'live_status': 'is_live',
            'title': 're:^LRT Opus.+$',
            'ext': 'mp4'
        }
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        streams_data = self._download_json(self._extract_js_var(webpage, 'tokenURL'), video_id)

        formats, subtitles = [], {}
        for stream_url in traverse_obj(streams_data, (
                'response', 'data', lambda k, _: k.startswith('content')), expected_type=url_or_none):
            fmts, subs = self._extract_m3u8_formats_and_subtitles(stream_url, video_id, 'mp4', m3u8_id='hls', live=True)
            formats.extend(fmts)
            subtitles = self._merge_subtitles(subtitles, subs)
        self._sort_formats(formats)

        stream_title = self._extract_js_var(webpage, 'video_title', 'LRT')
        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            'is_live': True,
            'title': f'{self._og_search_title(webpage)} - {stream_title}'
        }