[英]Python result of a strange function, that I can't reproduce in another language
[英]Can I transform a function result into a variable for another function in python/FLASK?
我正在学习Python 2.7和FLASK。 我有一个消耗Spotify API的练习,所以使用一个函数我得到一个艺术家的ID。
我可以使用该函数的结果(id)并将其存储为变量,以便我可以在另一个API端点中使用它吗? 或者我该如何使用该结果?
这是我的代码:
@app.route("/api/artist/<artist>")
def api_artist(artist):
params = get_id(artist)
return jsonify(params)
def get_id(artist):
headers = {
"client_id": "XxXXxxXXX",
"cliente_secret": "XXXxxxxXXX"
}
response = requests.get("https://api.spotify.com/v1/search?q=" + artist +"&type=artist", headers=headers)
if response.status_code == 200:
print(response.text)
lista=[]
response_dict = response.json()
results = response_dict["artists"]
items = results ["items"]
for value in items:
lista.append(value["id"])
params = {
"id": lista[0]
}
return params
这是我尝试使用的Spotify端点: https://api.spotify.com/v1/artists/{id}/top-tracks
://api.spotify.com/v1/artists/{id}/top-tracks
REST API应该是“无状态的”,因此不像@ cricket_007所指出的那样好
所以这就是你做的:做2个REST API调用并在客户端处理用户艺术家偏好的个性化
1.)搜索艺术家并获取ID并将其返回给用户(客户端)应用程序
2.)使用获取的id进行第二次REST API调用并获取顶部曲目
希望这可以帮助。 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.