[英]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.