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

Code before:
            'track': title,
            'artist': try_get(item, lambda x: x['artistsNames']) or try_get(item, lambda x: x['artists_names']),
            'album': try_get(album, lambda x: x['name']) or try_get(album, lambda x: x['title']),
            'album_artist': try_get(album, lambda x: x['artistsNames']) or try_get(album, lambda x: x['artists_names']),
        }

    def _real_extract(self, url):
        song_id, type_url = self._match_valid_url(url).group('id', 'type')

        api = self.get_api_with_signature(name_api=self._SLUG_API[type_url], param={'id': song_id})

        # Check the cookie file, if cookie file doesn't exist, create a dummy request to update the cookie
        if not self._downloader.params.get('cookiefile'):
            self._download_json(api, video_id=song_id, note='Dummy request to update cookie')

        info = self._download_json(api, video_id=song_id)
        return self._process_data(info.get('data'), song_id, type_url)

    def get_api_with_signature(self, name_api, param):
        sha256 = self.sha256_params(''.join(f'{k}={v}' for k, v in param.items()))

        data = {
            'apiKey': self._API_KEY,
            'sig': self.hmac512_string(f'{name_api}{sha256}')
        }
        data.update(param)
        return f'{self._DOMAIN}{name_api}?{urlencode(data)}'

    def sha256_params(self, string):
        return hashlib.sha256(string.encode('utf-8')).hexdigest()

    def hmac512_string(self, string):
        return hmac.new(self._SECRET_KEY, string.encode('utf-8'), hashlib.sha512).hexdigest()


class ZingMp3IE(ZingMp3BaseIE):
    _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
    _TESTS = [{
        'url': 'https://zingmp3.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
        'md5': 'ead7ae13693b3205cbc89536a077daed',
        'info_dict': {
            'id': 'ZWZB9WAB',
            'title': 'Xa Mãi Xa',
            'ext': 'mp3',
            'thumbnail': r're:^https?://.+\.jpg',
            'subtitles': {
                'origin': [{
                    'ext': 'lrc',
                }]
            },
            'duration': 255,

Review comment (suggestion):
```suggestion
        data = {
            'apiKey': self._API_KEY,
            'sig': self.hmac512_string(f'{name_api}{sha256}'),
            **param,
        }
```

Code after:
            'duration': int_or_none(item.get('duration')),
            'track': title,
            'artist': traverse_obj(item, 'artistsNames', 'artists_names'),
            'album': traverse_obj(album, 'name', 'title'),
            'album_artist': traverse_obj(album, 'artistsNames', 'artists_names'),
        }

    def _real_initialize(self):
        if not self.get_param('cookiefile') and not self.get_param('cookiesfrombrowser'):
            self._request_webpage(HEADRequest(self._DOMAIN), None, note='Updating cookies')

    def _real_extract(self, url):
        song_id, type_url = self._match_valid_url(url).group('id', 'type')

        api = self.get_api_with_signature(name_api=self._SLUG_API[type_url], param={'id': song_id})

        return self._process_data(self._download_json(api, song_id)['data'], song_id, type_url)

    def get_api_with_signature(self, name_api, param):
        sha256 = hashlib.sha256(''.join(f'{k}={v}' for k, v in param.items()).encode('utf-8')).hexdigest()

        data = {
            'apiKey': self._API_KEY,
            'sig': hmac.new(self._SECRET_KEY, f'{name_api}{sha256}'.encode('utf-8'), hashlib.sha512).hexdigest(),
            **param,
        }
        return f'{self._DOMAIN}{name_api}?{urllib.parse.urlencode(data)}'


class ZingMp3IE(ZingMp3BaseIE):
    _VALID_URL = ZingMp3BaseIE._VALID_URL_TMPL % 'bai-hat|video-clip|embed'
    _TESTS = [{
        'url': 'https://mp3.zing.vn/bai-hat/Xa-Mai-Xa-Bao-Thy/ZWZB9WAB.html',
        'md5': 'ead7ae13693b3205cbc89536a077daed',
        'info_dict': {
            'id': 'ZWZB9WAB',
            'title': 'Xa Mãi Xa',
            'ext': 'mp3',
            'thumbnail': r're:^https?://.+\.jpg',
            'subtitles': {
                'origin': [{
                    'ext': 'lrc',
                }]
            },
            'duration': 255,
            'track': 'Xa Mãi Xa',
            'artist': 'Bảo Thy',
            'album': 'Special Album',
            'album_artist': 'Bảo Thy',
        },
    }, {