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

Code before:
            'series': 'The Tim Dillon Show',
            'thumbnail': 're:.+[.](png|jpe?g|webp)',
        },
    }, {
        'url': 'https://podcasts.apple.com/podcast/207-whitney-webb-returns/id1135137367?i=1000482637777',
        'only_matching': True,
    }, {
        'url': 'https://podcasts.apple.com/podcast/207-whitney-webb-returns?i=1000482637777',
        'only_matching': True,
    }, {
        'url': 'https://podcasts.apple.com/podcast/id1135137367?i=1000482637777',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        episode_id = self._match_id(url)
        webpage = self._download_webpage(url, episode_id)
        server_data = self._parse_json(
            get_element_by_id('serialized-server-data', webpage),
            episode_id) or [{}]
        model_data = traverse_obj(
            server_data,
            (0, 'data', 'headerButtonItems',
             {lambda x: next(y for y in x
                             if y.get('$kind') == 'bookmark' and y.get('modelType') == 'EpisodeOffer')},
             'model'))
        schema_content = traverse_obj(server_data, (0, 'data', 'seoData', 'schemaContent'))

        return {
            'id': episode_id,
            'title': model_data.get('title') or schema_content.get('name') or self._og_search_title(webpage),
            'url': clean_podcast_url(model_data['streamUrl']),
            'description': schema_content.get('description'),
            'timestamp': parse_iso8601(model_data.get('releaseDate')),
            'duration': model_data.get('duration'),
            'series': schema_content.get('partOfSeries', {}).get('name'),
            'thumbnail': self._og_search_thumbnail(webpage),
            'vcodec': 'none',
        }


Review comment (refactor):
Let's simplify this to:
```suggestion
        model_data = traverse_obj(server_data, (
            'headerButtonItems', lambda _, v: v['$kind'] == 'bookmark' and v['modelType'] == 'EpisodeOffer',
            'model', {dict}, any))
```
Also, was there any reason for choosing `bookmark`/`EpisodeOffer`? `download`/`EpisodeLockup` seems more fitting and has more metadata available.

Code after:
            'upload_date': '20200705',
            'timestamp': 1593932400,
            'duration': 5369,
            'series': 'The Tim Dillon Show',
            'thumbnail': 're:.+[.](png|jpe?g|webp)',
        },
    }, {
        'url': 'https://podcasts.apple.com/podcast/207-whitney-webb-returns/id1135137367?i=1000482637777',
        'only_matching': True,
    }, {
        'url': 'https://podcasts.apple.com/podcast/207-whitney-webb-returns?i=1000482637777',
        'only_matching': True,
    }, {
        'url': 'https://podcasts.apple.com/podcast/id1135137367?i=1000482637777',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        episode_id = self._match_id(url)
        webpage = self._download_webpage(url, episode_id)
        server_data = self._search_json(
            r'<script [^>]*\bid=["\']serialized-server-data["\'][^>]*>', webpage,
            'server data', episode_id, contains_pattern=r'\[{(?s:.+)}\]')[0]['data']
        model_data = traverse_obj(server_data, (
            'headerButtonItems', lambda _, v: v['$kind'] == 'bookmark' and v['modelType'] == 'EpisodeOffer',
            'model', {dict}, any))

        return {
            'id': episode_id,
            **self._json_ld(
                traverse_obj(server_data, ('seoData', 'schemaContent', {dict}))
                or self._yield_json_ld(webpage, episode_id, fatal=False), episode_id, fatal=False),
            **traverse_obj(model_data, {
                'title': ('title', {str}),
                'url': ('streamUrl', {clean_podcast_url}),
                'timestamp': ('releaseDate', {parse_iso8601}),
                'duration': ('duration', {int_or_none}),
            }),
            'thumbnail': self._og_search_thumbnail(webpage),
            'vcodec': 'none',
        }