Spotify - 曲目的“gid”

问题描述 投票:0回答:2

我看到 Spotify 上的每首曲目都有一个

gid
。 正如您在图像中看到的,
B
是曲目ID,但我不明白
A
。 请检查上图此处中显示的请求的响应。

你知道这个 gid 是什么以及我们如何检索它吗?

提前致谢

更新: 例如,Spotify 将曲目

https://open.spotify.com/track/0VtMV3IYHAu7fyZmqnGE99
的 API 调用为 https://spclient.wg.spotify.com/metadata/4/track/1e763423ad0e4862b20f9dbfdadc2cb7。如您所见,API URL 末尾的 id 是
1e763423ad0e4862b20f9dbfdadc2cb7
,它与曲目 id (
0VtMV3IYHAu7fyZmqnGE99
) 不同。 API 的响应是
{"gid": "1e763423ad0e4862b20f9dbfdadc2cb7","name": "Oh Yeah","album": {"gid": "b6a45388cac54953b3fe0f8e0ab7c729","name": "Oh Glory","artist": [{"gid": "e8dba066dc3644b1b69510eb3cdd8a42","name": "Aime Simone"}],"label": "No Start No End","date": {"year": 2023,"month": 5,"day": 5},"cover_group": {"image": [{"file_id": "ab67616d00001e026cddc8afe19c5950b5f6f5b6","size": "DEFAULT","width": 300,"height": 300},{"file_id": "ab67616d000048516cddc8afe19c5950b5f6f5b6","size": "SMALL","width": 64,"height": 64},{"file_id": "ab67616d0000b2736cddc8afe19c5950b5f6f5b6","size": "LARGE","width": 640,"height": 640}]},"licensor": {"uuid": "1b3fb428ae7b41aa97c9b46c5b962d57"},"prerelease_config": {"earliest_reveal_date": {"year": 2023,"month": 5,"day": 4,"hour": 11},"earliest_coverart_reveal_date": {"year": 2023,"month": 5,"day": 4,"hour": 11}}},"artist": [{"gid": "e8dba066dc3644b1b69510eb3cdd8a42","name": "Aime Simone"}],"number": 10,"disc_number": 1,"duration": 122293,"popularity": 41,"external_id": [{"type": "isrc","id": "FR9W12241433"}],"file": [{"file_id": "a0ee4ae82c323cf5e45361670079e6a68f391b2f","format": "OGG_VORBIS_320"},{"file_id": "0d508123a2197901602399756f091ba8225ff781","format": "OGG_VORBIS_160"},{"file_id": "698098f862955a08c9204e9cd723de32ad4e48e2","format": "OGG_VORBIS_96"},{"file_id": "0833e88386b0320aabcc56ab893c1ae157a5fffc","format": "MP4_256_DUAL"},{"file_id": "999bff5d82f078c4d6c12a88ee7401e6cf36c048","format": "MP4_256"},{"file_id": "cca2b23924cc42ed2b8f1efc1c95766c5f26a7c8","format": "MP4_128"},{"file_id": "f80419a11fa42bb9054ef4975bdba2a7c2226077","format": "MP4_128_DUAL"},{"file_id": "41af902734adfc123dba2bb91fa1138c7632af6c","format": "AAC_24"}],"preview": [{"file_id": "b2f08b0f87cdef5489d51588dc61e5b6fd7e5ca2","format": "MP3_96"}],"earliest_live_timestamp": 1683198000,"has_lyrics": true,"licensor": {"uuid": "1b3fb428ae7b41aa97c9b46c5b962d57"},"language_of_performance": ["en"],"original_audio": {"uuid": "87a724f901a34555a86efdc16b65ee9b"},"original_title": "Oh Yeah","artist_with_role": [{"artist_gid": "e8dba066dc3644b1b69510eb3cdd8a42","artist_name": "Aime Simone","role": "ARTIST_ROLE_MAIN_ARTIST"}],"canonical_uri": "spotify:track:0VtMV3IYHAu7fyZmqnGE99","prerelease_config": {"earliest_reveal_date": {"year": 2023,"month": 5,"day": 4,"hour": 11}}}

spotify
2个回答
2
投票

gid
可能意味着类似
Global ID
或类似的东西。我做了一些研究(python代码库JS代码库),发现这个
gid
是一些十六进制数字,需要转换为base62数字才能获得实际的ID。

在这两个代码库中,用于将

gid
转换为 ID,然后转换为 URI 的函数称为
gid2id
id2uri
。 我已经尝试过这两种方法,但它们要么没有按预期工作得很好,这意味着它们没有给出所需的结果或引用了代码库中没有的函数,所以我决定将我自己的快速转换器放在一起蟒蛇:

def gid_to_base62(gid):
base62_charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
decimal_number = int(gid, 16)
base62_id = ''

while decimal_number > 0:
    remainder = decimal_number % 62
    base62_id = base62_charset[remainder] + base62_id
    decimal_number = decimal_number // 62

return base62_id

gid = '1e763423ad0e4862b20f9dbfdadc2cb7'
base62_id = gid_to_base62(gid)
print(base62_id) # -> 0VtMV3IYHAu7fyZmqnGE99

现在我只在 2 个示例 gid 上进行了测试,因此请谨慎使用此转换器。


0
投票

作为 fabian 答案的后续,这里有一个函数可以反转该过程 - 它将 base62 trackID 转换回原始的十六进制 gid。

function base62ToHex(base62Str) {
  //convert base 62 to decimal
  const characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let decimalValue = BigInt(0);
  for (let i = 0; i < base62Str.length; i++) {
    const char = base62Str[i];
    const index = characters.indexOf(char);
    if (index === -1) {
      throw new Error("Invalid character in base62 string");
    }
    decimalValue = decimalValue * BigInt(62) + BigInt(index);
  }
  //convert decimal to hexadecimal
  let hexValue = decimalValue.toString(16);
  hexValue.padStart(32, '0');
  return hexValue;
}

const trackID = "0VtMV3IYHAu7fyZmqnGE99";
const gid = base62ToHex(trackID);
console.log(gid); // Outputs 1e763423ad0e4862b20f9dbfdadc2cb7

© www.soinside.com 2019 - 2024. All rights reserved.