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

Code before:
    def _extract_formats_and_subs(self, options, video_id):
        formats, subs = [], {}
        for format_id, format_url in options.items():
            if format_id not in ('stream', 'httpstream', 'mpegdash'):
                continue
            ext = determine_ext(format_url)
            if ext == 'm3u8':
                formats_, subs_ = self._extract_m3u8_formats_and_subtitles(
                    format_url, video_id, 'mp4', m3u8_id=format_id,
                    fatal=False)
            elif ext == 'mpd':
                formats_, subs_ = self._extract_mpd_formats_and_subtitles(
                    format_url, video_id, 'mp4', fatal=False)
            else:
                formats.append({
                    'url': format_url,
                    'format_id': format_id,
                })
                continue
            formats.extend(formats_)
            subs.update(subs_)
        self._sort_formats(formats)
        return formats, subs

    def _real_extract(self, url):
        scheme, netloc, video_id, display_id = self._match_valid_url(url).group('scheme', 'netloc', 'id', 'slug')
        if netloc.find('tvopen.gr') == -1:
            return self._return_canonical_url(url, video_id)
        webpage = self._download_webpage(url, video_id)
        info = self._search_json_ld(webpage, video_id, expected_type='VideoObject')
        info['formats'], info['subtitles'] = self._extract_formats_and_subs(
            self._download_api_data(video_id, scheme=scheme), video_id)
        max_dimensions = max(
            [tuple(format.get(k) or 0 for k in ('width', 'height')) for format in info['formats']],
            default=None)
        if max_dimensions[0]:
            for thumbnail in info['thumbnails']:
                thumbnail['url'] = re.sub(r'(/imgHandler/)\d+', rf'\g<1>{max_dimensions[0]}', thumbnail['url'])
                thumbnail['width'], thumbnail['height'] = max_dimensions
        description, _html = next(get_elements_text_and_html_by_attribute('class', 'description', webpage))
        if description and _html.startswith('<span '):
            info['description'] = description
        info['id'] = video_id
        info['display_id'] = display_id
        return info


class TVOpenGrEmbedIE(TVOpenGrBaseIE):
    IE_NAME = 'tvopengr:embed'
    IE_DESC = 'tvopen.gr embedded videos'
    _VALID_URL = r'https?://(?:www\.|cdn\.|)(?:tvopen|ethnos).gr/embed/(?P<id>\d+)'

Review comment (refactor):
As I mentioned in the other PR, do not follow the scheme of user provided URL. The "correct" way is to use `self.http_scheme`, but most extractors just hardcode `https` (there is rarely any reason for user to use http nowadays)

Code after:
            elif ext == 'mpd':
                formats_, subs_ = self._extract_mpd_formats_and_subtitles(
                    format_url, video_id, 'mp4', fatal=False)
            else:
                formats.append({
                    'url': format_url,
                    'format_id': format_id,
                })
                continue
            formats.extend(formats_)
            self._merge_subtitles(subs_, target=subs)
        self._sort_formats(formats)
        return formats, subs

    @staticmethod
    def _scale_thumbnails_to_max_width(formats, thumbnails, url_width_re):
        _keys = ('width', 'height')
        max_dimensions = max(
            [tuple(format.get(k) or 0 for k in _keys) for format in formats],
            default=(0, 0))
        if not max_dimensions[0]:
            return thumbnails
        return [
            merge_dicts(
                {'url': re.sub(url_width_re, str(max_dimensions[0]), thumbnail['url'])},
                dict(zip(_keys, max_dimensions)), thumbnail)
            for thumbnail in thumbnails
        ]

    def _real_extract(self, url):
        netloc, video_id, display_id = self._match_valid_url(url).group('netloc', 'id', 'slug')
        if netloc.find('tvopen.gr') == -1:
            return self._return_canonical_url(url, video_id)
        webpage = self._download_webpage(url, video_id)
        info = self._search_json_ld(webpage, video_id, expected_type='VideoObject')
        info['formats'], info['subtitles'] = self._extract_formats_and_subs(
            self._download_json(self._API_ENDPOINT, video_id, query={'cid': video_id}),
            video_id)
        info['thumbnails'] = self._scale_thumbnails_to_max_width(
            info['formats'], info['thumbnails'], r'(?<=/imgHandler/)\d+')
        description, _html = next(get_elements_text_and_html_by_attribute('class', 'description', webpage))
        if description and _html.startswith('<span '):
            info['description'] = description
        info['id'] = video_id
        info['display_id'] = display_id
        return info


class TVOpenGrEmbedIE(TVOpenGrBaseIE):
    IE_NAME = 'tvopengr:embed'
    IE_DESC = 'tvopen.gr embedded videos'