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

Code before:
            '_old_archive_ids': ['rtveinfantil 7048976'],
        },
    }]

    def _get_subtitles(self, video_id):
        subtitle_data = self._download_json(
            f'https://api2.rtve.es/api/videos/{video_id}/subtitulos.json', video_id,
            'Downloading subtitles info')
        return traverse_obj(subtitle_data, ('page', 'items', ..., {
            'id': ('lang', {str}),
            'url': ('src', {url_or_none}),
        }, all, {subs_list_to_dict(lang='es')}))

    def _real_extract(self, url):
        video_id = self._match_id(url)
        metadata = self._download_json(
            f'http://www.rtve.es/api/videos/{video_id}/config/alacarta_videos.json',
            video_id)['page']['items'][0]
        if metadata['state'] == 'DESPU':
            raise ExtractorError('The video is no longer available', expected=True)
        formats, subtitles = self._extract_png_formats_and_subtitles(video_id)

        self._merge_subtitles(self.extract_subtitles(video_id), target=subtitles)

        is_infantil = urllib.parse.urlparse(url).path.startswith('/infantil/')

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **self._parse_metadata(metadata),
            '_old_archive_ids': [f'rtveinfantil {video_id}'] if is_infantil else None,
        }


class RTVEAudioIE(RTVEBaseIE):
    IE_NAME = 'rtve.es:audio'
    IE_DESC = 'RTVE audio'
    _VALID_URL = r'https?://(?:www\.)?rtve\.es/(alacarta|play)/audios/[^/?#]+/[^/?#]+/(?P<id>\d+)'

    _TESTS = [{
        'url': 'https://www.rtve.es/alacarta/audios/a-hombros-de-gigantes/palabra-ingeniero-codigos-informaticos-27-04-21/5889192/',
        'md5': 'ae06d27bff945c4e87a50f89f6ce48ce',
        'info_dict': {
            'id': '5889192',
            'ext': 'mp3',
            'title': 'Códigos informáticos',
            'alt_title': 'Códigos informáticos - Escuchar ahora',
            'duration': 349.440,
            'series': 'A hombros de gigantes',
            'description': 'md5:72b0d7c1ca20fd327bdfff7ac0171afb',

Review comment (refactor):
could also make this a capture group in `_VALID_URL` and set the var at the beginning of `_real_extract`

Code after:
            'series': 'Agus & Lui: Churros y Crafts',
            '_old_archive_ids': ['rtveinfantil 7048976'],
        },
    }]

    def _get_subtitles(self, video_id):
        subtitle_data = self._download_json(
            f'https://api2.rtve.es/api/videos/{video_id}/subtitulos.json', video_id,
            'Downloading subtitles info')
        return traverse_obj(subtitle_data, ('page', 'items', ..., {
            'id': ('lang', {str}),
            'url': ('src', {url_or_none}),
        }, all, {subs_list_to_dict(lang='es')}))

    def _real_extract(self, url):
        video_id = self._match_id(url)
        metadata = self._download_json(
            f'http://www.rtve.es/api/videos/{video_id}/config/alacarta_videos.json',
            video_id)['page']['items'][0]
        if metadata['state'] == 'DESPU':
            raise ExtractorError('The video is no longer available', expected=True)
        formats, subtitles = self._extract_png_formats_and_subtitles(video_id)

        self._merge_subtitles(self.extract_subtitles(video_id), target=subtitles)

        is_infantil = urllib.parse.urlparse(url).path.startswith('/infantil/')

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **self._parse_metadata(metadata),
            '_old_archive_ids': [make_archive_id('rtveinfantil', video_id)] if is_infantil else None,
        }


class RTVEAudioIE(RTVEBaseIE):
    IE_NAME = 'rtve.es:audio'
    IE_DESC = 'RTVE audio'
    _VALID_URL = r'https?://(?:www\.)?rtve\.es/(alacarta|play)/audios/(?:[^/?#]+/){2}(?P<id>\d+)'

    _TESTS = [{
        'url': 'https://www.rtve.es/alacarta/audios/a-hombros-de-gigantes/palabra-ingeniero-codigos-informaticos-27-04-21/5889192/',
        'md5': 'ae06d27bff945c4e87a50f89f6ce48ce',
        'info_dict': {
            'id': '5889192',
            'ext': 'mp3',
            'title': 'Códigos informáticos',
            'alt_title': 'Códigos informáticos - Escuchar ahora',
            'duration': 349.440,
            'series': 'A hombros de gigantes',