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

Code before:
from .common import InfoExtractor


class TelegramEmbedIE(InfoExtractor):
    IE_NAME = 'telegram:embed'
    _VALID_URL = r'https?://t\.me/(?P<channel_name>[^/]+)/(?P<id>\d+)'
    _TEST = {
        'url': 'https://t.me/europa_press/613',
        'info_dict': {
            'id': '613',
            'ext': 'mp4',
            'title': 'Europa Press',
            'description': '6ce2d7e8d56eda16d80607b23db7b252',
            'thumbnail': r're:^https?:\/\/cdn.*?telesco\.pe\/file\/\w+',
        },
    }

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

        webpage = self._download_webpage(url, video_id=video_id)

        webpage_embed = self._download_webpage(f'{url}?embed=1', video_id=video_id)

        source = self._search_regex('<video[^>]+src=\"([^"]+)\"', webpage_embed, 'source')

        formats = [{
            'ext': 'mp4',
            'url': self._proto_relative_url(source),
            'vcodec': 'none'
        }]

        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True),
            'description': self._html_search_meta(['og:description', 'twitter:description'], webpage, fatal=True),
            'thumbnail': self._search_regex(r'tgme_widget_message_video_thumb"[^>]+background\-image\:url\(\'(.*?)\'\)',
                                            webpage_embed, 'thumbnail'),
            'formats': formats,
        }


Review comment (suggestion):
The URL passed can be `https://t.me/europa_press/613?embed=1`, `https://t.me/europa_press/613?xxx=yyy`, `https://t.me/europa_press/613#abcd` etc.

Use urllib to correctly add the embed query or construct the URL from `video_id`

Code after:
from .common import InfoExtractor


class TelegramEmbedIE(InfoExtractor):
    IE_NAME = 'telegram:embed'
    _VALID_URL = r'https?://t\.me/(?P<channel_name>[^/]+)/(?P<id>\d+)'
    _TESTS = [{
        'url': 'https://t.me/europa_press/613',
        'info_dict': {
            'id': '613',
            'ext': 'mp4',
            'title': 'Europa Press',
            'description': '6ce2d7e8d56eda16d80607b23db7b252',
            'thumbnail': r're:^https?:\/\/cdn.*?telesco\.pe\/file\/\w+',
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        webpage = self._download_webpage(url, video_id)
        webpage_embed = self._download_webpage(f'{url}?embed=1', video_id)

        formats = [{
            'url': self._proto_relative_url(self._search_regex(
                '<video[^>]+src="([^"]+)"', webpage_embed, 'source')),
            'ext': 'mp4',
        }]
        self._sort_formats(formats)

        return {
            'id': video_id,
            'title': self._html_search_meta(['og:title', 'twitter:title'], webpage, fatal=True),
            'description': self._html_search_meta(['og:description', 'twitter:description'], webpage, fatal=True),
            'thumbnail': self._search_regex(r'tgme_widget_message_video_thumb"[^>]+background-image:url\(\'([^\']+)\'\)',
                                            webpage_embed, 'thumbnail'),
            'formats': formats,
        }