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

Code before:

        api = self.get_api_with_signature(name_api=self._SLUG_API[type_url], param={
            'ctime': self._TIMESTAMP,
            'id': song_id})
        info = self.download_json(api, song_id)
        return self._process_data(info.get('data'), song_id, type_url)

    def download_json(self, api, video_id):
        # For the first time making the request, we need to make one more temp request to update the cookie
        # and the next time we don't need to do it.
        # But if download with cookieFile, so we don't need to make one more temp request
        if self._downloader.params.get('cookiefile'):
            self._IS_UPDATE_COOKIES = True

        if not self._IS_UPDATE_COOKIES:
            self._download_json(api, video_id=video_id, note='Update cookies for request')
            self._IS_UPDATE_COOKIES = True

        return self._download_json(api, video_id=video_id)

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

        sha256 = self.get_hash256(text)
        data = {
            'ctime': param.get('ctime'),
            'apiKey': self._API_KEY,
            'sig': self.get_hmac512(f'{name_api}{sha256}')
        }
        data.update(param)
        return f'{self._DOMAIN}{name_api}?{urlencode(data)}'

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

    def get_hmac512(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',

Review comment (suggestion):
```suggestion
        sha256 = self.get_hash256(''.join(f'{k}={v}' for k, v in param.items()))
```

Code after:

        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',
        },
    }, {
        'url': 'https://zingmp3.vn/video-clip/Suong-Hoa-Dua-Loi-K-ICM-RYO/ZO8ZF7C7.html',
        'md5': 'e9c972b693aa88301ef981c8151c4343',
        'info_dict': {
            'id': 'ZO8ZF7C7',
            'title': 'Sương Hoa Đưa Lối',
            'ext': 'mp4',
            'thumbnail': r're:^https?://.+\.jpg',
            'duration': 207,
            'track': 'Sương Hoa Đưa Lối',
            'artist': 'K-ICM, RYO',
        },
    }, {
        'url': 'https://zingmp3.vn/embed/song/ZWZEI76B?start=false',
        'only_matching': True,
    }, {