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

Code before:
        'url': 'https://www.s4c.cymru/clic/programme/861362209',
        'info_dict': {
            'id': '861362209',
            'ext': 'mp4',
            'title': 'Y Swn',
            'description': 'When Margaret Thatcher\'s government breaks a promise to establish a Welsh language channel in 1979 it unleashes a wave of anger across Wales.  This film portrays the efforts of the Welsh Language Society and Plaid Cymru to realise a television channel and Gwynfor Evans\'s declaration to fast until death over the matter.  A unique film about one of the most colourful chapters in modern Welsh history.',
            'duration': 5340
        }
    },
        {
        'url': 'https://www.s4c.cymru/clic/programme/856636948',
        'info_dict': {
            'id': '856636948',
            'ext': 'mp4',
            'title': 'Am Dro',
            'duration': 2880,
            'description': 'In this programme Geraint will be leading the crew on a walk through Llanelli while introducing them to some unusual creatures. Barney will be taking them on a walk along the border near Oswestry. Ena will be following St David\'s footsteps in Pembrokeshire, while Ann-Marie will take them up the mountain in Eglwysilan before descending to the town of Senghenydd',
        }
    },
    ]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        programme_metadata = self._download_json(f'https://www.s4c.cymru/df/full_prog_details?lang=e&programme_id={video_id}', video_id)

        programme_details = programme_metadata.get('full_prog_details') or []
        metadata = {
            'id': video_id,
            **traverse_obj(programme_details[0], {
                'description': ('full_billing', {str.strip}),
                'duration': ('duration', {lambda x: int(x) * 60}),
            })
        }

        title = programme_details[0].get('programme_title') or programme_details[0].get('series_title')

        metadata['title'] = title

        player_config = self._download_json(f'https://player-api.s4c-cdn.co.uk/player-configuration/prod?programme_id={video_id}&signed=0&lang=en&mode=od&appId=clic&streamName=', video_id)

        filename = player_config.get('filename')

        streaming_urls = self._download_json(f'https://player-api.s4c-cdn.co.uk/streaming-urls/prod?mode=od&application=clic&region=WW&extra=false&thirdParty=false&filename={filename}', filename)

        m3u8_url = streaming_urls.get('hls')

        formats = self._extract_m3u8_formats(
            m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls',
            note='Downloading HD m3u8 information', errnote='Unable to download HD m3u8 information')
        metadata['formats'] = formats
        return metadata

Review comment (suggestion):
`programme_metadata` is only used once, we can reduce this to a single var/line. And `details` will only be used for metadata, so it should be non-fatal
```suggestion
        details = self._download_json(
            f'https://www.s4c.cymru/df/full_prog_details?lang=e&programme_id={video_id}',
            video_id, fatal=False)
```

Code after:
            'ext': 'mp4',
            'title': 'Y Swn',
            'description': 'md5:f7681a30e4955b250b3224aa9fe70cf0',
            'duration': 5340
        },
    }, {
        'url': 'https://www.s4c.cymru/clic/programme/856636948',
        'info_dict': {
            'id': '856636948',
            'ext': 'mp4',
            'title': 'Am Dro',
            'duration': 2880,
            'description': 'md5:100d8686fc9a632a0cb2db52a3433ffe',
        },
    }]

    def _real_extract(self, url):
        video_id = self._match_id(url)
        details = self._download_json(
            f'https://www.s4c.cymru/df/full_prog_details?lang=e&programme_id={video_id}',
            video_id, fatal=False)

        filename = self._download_json(
            'https://player-api.s4c-cdn.co.uk/player-configuration/prod', video_id, query={
                'programme_id': video_id,
                'signed': '0',
                'lang': 'en',
                'mode': 'od',
                'appId': 'clic',
                'streamName': '',
            }, note='Downloading player config JSON')['filename']
        m3u8_url = self._download_json(
            'https://player-api.s4c-cdn.co.uk/streaming-urls/prod', video_id, query={
                'mode': 'od',
                'application': 'clic',
                'region': 'WW',
                'extra': 'false',
                'thirdParty': 'false',
                'filename': filename,
            }, note='Downloading streaming urls JSON')['hls']
        formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id, 'mp4', m3u8_id='hls')

        return {
            'id': video_id,
            'formats': formats,
            'subtitles': subtitles,
            **traverse_obj(details, ('full_prog_details', 0, {
                'title': (('programme_title', 'series_title'), {str}),
                'description': ('full_billing', {str.strip}),
                'duration': ('duration', {lambda x: int(x) * 60}),
            }), get_all=False),