Skip to content

Instantly share code, notes, and snippets.

@c-basalt
Created April 23, 2024 19:30
Show Gist options
  • Save c-basalt/9bc9feeed8e39e928b6607ddf01840b5 to your computer and use it in GitHub Desktop.
Save c-basalt/9bc9feeed8e39e928b6607ddf01840b5 to your computer and use it in GitHub Desktop.
signing algorithm for `musics.fcg` endpoint
class QQMusicBaseIE(InfoExtractor):
def _get_musics_sign(self, payload: bytes):
# This may not work for domains other than `y.qq.com` and browswer UA that contains `Headless`
md5hex = hashlib.md5(payload).hexdigest().upper()
hex_digits = [int(c, base=16) for c in md5hex]
xor_digits = []
for i, xor in enumerate([212, 45, 80, 68, 195, 163, 163, 203, 157, 220, 254, 91, 204, 79, 104, 6]):
xor_digits.append((hex_digits[i * 2] * 16 + hex_digits[i * 2 + 1]) ^ xor)
char_indicies = []
for i in range(0, len(xor_digits) - 1, 3):
char_indicies.extend([
xor_digits[i] >> 2,
((xor_digits[i] & 3) << 4) | (xor_digits[i + 1] >> 4),
((xor_digits[i + 1] & 15) << 2) | (xor_digits[i + 2] >> 6),
xor_digits[i + 2] & 63,
])
char_indicies.extend([
xor_digits[15] >> 2,
(xor_digits[15] & 3) << 4,
])
head = ''.join(md5hex[i] for i in [21, 4, 9, 26, 16, 20, 27, 30])
tail = ''.join(md5hex[i] for i in [18, 11, 3, 2, 1, 7, 6, 25])
body = ''.join('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='[i] for i in char_indicies)
return re.sub(r'[\/+]', '', f'zzb{head}{body}{tail}'.lower())
def make_fcu_req(self, mid, req_dict, **kwargs):
payload = json.dumps({
'comm': {
'cv': 4747474,
'ct': 24,
'format': 'json',
'inCharset': 'utf-8',
'outCharset': 'utf-8',
'notice': 0,
'platform': 'yqq.json',
'needNewCode': 1,
'uin': self._get_uin(),
'g_tk_new_20200303': self._get_g_tk(),
'g_tk': self._get_g_tk(),
},
**req_dict
}, separators=(',', ':')).encode('utf-8')
return self._download_json(
'https://u.y.qq.com/cgi-bin/musics.fcg', mid, data=payload,
query={'_': int(time.time()), 'sign': self._get_sign(payload)}, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment