我可以从 Spotify API 保存哪些数据?

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

我正在构建一个网站,并使用 Spotify API 作为音乐库。我想添加比 API 允许的更多的过滤器和排序选项来搜索曲目,所以我想知道我可以从 API 将哪些曲目/歌曲数据保存到我的数据库中,例如艺术家姓名或受欢迎程度。

我想保存:姓名、艺术家、专辑和其他一些内容。这可能吗,还是违反条款和条件?

spotify libspotify
1个回答
2
投票

是的,这是可能的。

数据存储在端点的 Spotify API 中。

Spotify API 端点参考此处。

每个端点处理客户端(您)请求的特定类型的数据。

我给你举一个例子。相同的逻辑适用于所有其他端点。

import requests

   """
   Import library in order to make api calls.
   Alternatively, ou can also use a wrapper like "Spotipy" 
   instead of requesting directely.
   """

# hit desired endpoint
SEARCH_ENDPOINT = 'https://api.spotify.com/v1/search'

# define your call
def search_by_track_and_artist(artist, track):

    path = 'token.json' # you need to get a token for this call
                        # endpoint reference page will provide you with one
                        # you can store it in a file

    with open(path) as t:
        token = json.load(t)

    # call API with authentication
    myparams = {'type': 'track'}
    myparams['q'] = "artist:{} track:{}".format(artist,track)
    resp = requests.get(SEARCH_ENDPOINT, params=myparams, headers={"Authorization": "Bearer {}".format(token)})
    return resp.json()

尝试一下:

search_by_track_and_artist('Radiohead', 'Karma Police')

存储数据并根据需要进行处理。但您必须遵守 Spotify 条款才能将其公开。

旁注:Spotipy 文档。

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