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

Code before:
            'ext': 'mp3',
            'duration': 52.82,
            'uploader': 'Brad',
            'uploader_id': 1,
            'thumbnail': 'https://cdn.whyp.it/a537bb36-3373-4c61-96c8-27fc1b2f427a.jpg',
            'vcodec': 'none',
        }
    }

    def _real_extract(self, url):
        mobj = self._match_valid_url(url)
        unique_id = mobj.group('number_id')

        webpage = self._download_webpage(url, unique_id)

        data = self._search_nuxt_data(webpage, unique_id)['rawTrack']

        return {
            'url': data['audio_url'],
            'id': unique_id,
            'title': data.get('title'),
            'description': data.get('description'),
            'duration': data.get('duration'),
            'uploader': traverse_obj(data, ('user', 'username')),
            'uploader_id': traverse_obj(data, ('user', 'id')),
            'thumbnail': data.get('artwork_url'),
            'ext': 'mp3',
            'vcodec': 'none',
            # Need referer header otherwise get 403 error from cdn
            'http_headers': {'Referer': 'https://whyp.it/'},
        }


Review comment (refactor):
Dict key traversal with `traverse_obj` can make the metadata extraction a bit less grueling.
`traverse_obj(data, {})` will always return a dict, so it is safe to unpack in return

It's a good idea to always call `float_or_none` or `int_or_none` on the duration value, in case it was a numeric string or otherwise invalid in the site's JSON. It's done below using a `traverse_obj` transformation, e.g. `{float_or_none}`

`uploader_id` should be a string, but is an integer in the JSON, so we want to safely stringify it with `str_or_none`. (you will need to update the test)
```suggestion
            **traverse_obj(data, {
                'title': 'title',
                'description': 'description',
                'duration': ('duration', {float_or_none}),
                'uploader': ('user', 'username'),
                'uploader_id': ('user', 'id', {str_or_none}),
                'thumbnail': ('artwork_url', {url_or_none}),
            }),
```
import `url_or_none`, `float_or_none` and `str_or_none` from utils

Code after:
        'info_dict': {
            'url': 'https://cdn.whyp.it/50eb17cc-e9ff-4e18-b89b-dc9206a95cb1.mp3',
            'id': '18337',
            'title': 'Home Page Example Track',
            'description': 'md5:bd758000fb93f3159339c852b5b9133c',
            'ext': 'mp3',
            'duration': 52.82,
            'uploader': 'Brad',
            'uploader_id': '1',
            'thumbnail': 'https://cdn.whyp.it/a537bb36-3373-4c61-96c8-27fc1b2f427a.jpg',
        },
    }, {
        'url': 'https://www.whyp.it/tracks/18337',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        unique_id = self._match_id(url)
        webpage = self._download_webpage(url, unique_id)
        data = self._search_nuxt_data(webpage, unique_id)['rawTrack']

        return {
            'url': data['audio_url'],
            'id': unique_id,
            **traverse_obj(data, {
                'title': 'title',
                'description': 'description',
                'duration': ('duration', {float_or_none}),
                'uploader': ('user', 'username'),
                'uploader_id': ('user', 'id', {str_or_none}),
                'thumbnail': ('artwork_url', {url_or_none}),
            }),
            'ext': 'mp3',
            'vcodec': 'none',
            'http_headers': {'Referer': 'https://whyp.it/'},
        }