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

Code before:

class CamFMEpisodeIE(CamFMBaseIE):
    _VALID_URL = r'https://(?:www\.)?camfm\.co\.uk/player/(?P<id>[^/]+)/?'
    _TESTS = [{
        'url': 'https://camfm.co.uk/player/43336',
        'skip': 'Episode will expire - don\'t actually know when, but it will go eventually',
        'info_dict': {
            'id': '43336',
            'title': 'AITAA: Am I the Agony Aunt? - 19:00 Tue 16/05/2023',
            'ext': 'mp3',
            'upload_date': '20230516',
            'description': 'md5:f165144f94927c0f1bfa2ee6e6ab7bbf',
            'timestamp': 1684263600,
            'series': 'AITAA: Am I the Agony Aunt?',
            'thumbnail': 'md5:5980a831360d0744c3764551be3d09c1',
            'categories': ['Entertainment'],
        }
    }]

    def _real_extract(self, url):
        episode_id = self._match_id(url)
        page = self._download_webpage(url, episode_id)
        audios = self._parse_html5_media_entries('https://audio.camfm.co.uk', page, episode_id)
        formats = []
        for i in audios:
            formats.extend(i['formats'])

        series = self._html_search_regex(r'<div[^>]+class="caption">\n\s*(.+)', page, 'title', fatal=False)
        date = self._html_search_regex('>Aired at ([^<]+)<', page, 'air date', fatal=False)
        title = join_nonempty(series, date, delim=' - ')

        return {
            'id': episode_id,
            'title': title,
            'formats': formats,
            'timestamp': unified_timestamp(date),  # this will be off by an hour when DST is in effect
            # https://www.gov.uk/when-do-the-clocks-change - too annoying to calculate, will just treat as UTC
            # it'll be right half the time at least
            'series': series,
            'description': self._html_search_regex(r'>Aired at [^<]+<.*\n\s*(.*)', page, 'description', fatal=False),
            'thumbnail': self._absolute_url(self._search_regex(r'<div[^>]+class="cover-art"[^>]+style="[^"]+url\(\'([^\']+)\'\)[^>]+>',
                                            page, 'thumbnail', fatal=False)),
            'categories': [self._html_search_regex(r'<span[^>]+class="label"[^>]+>([^<]+)<', page, 'category', fatal=False)],
            'was_live': True,
        }


Review comment (suggestion):
```suggestion
        formats = traverse_obj(audios, (..., 'formats', ...))
```
Can be inlined in return too.

PS: The `...` are literal. See docstring of traverse_obj for details.

Code after:

class CamFMEpisodeIE(InfoExtractor):
    _VALID_URL = r'https://(?:www\.)?camfm\.co\.uk/player/(?P<id>[^/]+)'
    _TESTS = [{
        'url': 'https://camfm.co.uk/player/43336',
        'skip': 'Episode will expire - don\'t actually know when, but it will go eventually',
        'info_dict': {
            'id': '43336',
            'title': 'AITAA: Am I the Agony Aunt? - 19:00 Tue 16/05/2023',
            'ext': 'mp3',
            'upload_date': '20230516',
            'description': 'md5:f165144f94927c0f1bfa2ee6e6ab7bbf',
            'timestamp': 1684263600,
            'series': 'AITAA: Am I the Agony Aunt?',
            'thumbnail': 'md5:5980a831360d0744c3764551be3d09c1',
            'categories': ['Entertainment'],
        }
    }]

    def _real_extract(self, url):
        episode_id = self._match_id(url)
        page = self._download_webpage(url, episode_id)
        audios = self._parse_html5_media_entries('https://audio.camfm.co.uk', page, episode_id)

        caption = get_element_by_class('caption', page)
        series = clean_html(re.sub(r'<span[^<]+<[^<]+>', '', caption))

        card_section = get_element_by_class('card-section', page)
        date = self._html_search_regex('>Aired at ([^<]+)<', card_section, 'air date', fatal=False)

        return {
            'id': episode_id,
            'title': join_nonempty(series, date, delim=' - '),
            'formats': traverse_obj(audios, (..., 'formats', ...)),
            'timestamp': unified_timestamp(date),  # XXX: Does not account for UK's daylight savings
            'series': series,
            'description': clean_html(re.sub(r'<b>[^<]+</b><br[^>]+/>', '', card_section)),
            'thumbnail': urljoin('https://camfm.co.uk', self._search_regex(
                r'<div[^>]+class="cover-art"[^>]+style="[^"]+url\(\'([^\']+)',
                page, 'thumbnail', fatal=False)),
            'categories': get_elements_by_class('label', caption),
            'was_live': True,
        }