我目前正在编写一个小的Python程序,用来使用Spotify Web API分析保存的歌曲的音频特征。不幸的是,查询每首歌曲的音频特征需要很长时间。大约每秒分析3-4首歌曲。对于2500首存储的歌曲,该函数需要很长时间。作为一个初学者,我还没有找到加速函数的方法。以下是该函数的源代码:
def avg_features(token, tracklist):
track_counter: int = 0
danceability = 0
energy = 0
loudness = 0
speechiness = 0
acousticness = 0
instrumentalness = 0
liveness = 0
valence = 0
tempo = 0
for track in tracklist:
query = f"https://api.spotify.com/v1/audio-features/{track}"
response=requests.get(query, headers={"Authorization": f"Bearer {token}"})
response=response.json()
danceability += response['danceability']
energy += response['energy']
loudness += response['loudness']
speechiness += response['speechiness']
acousticness += response['acousticness']
instrumentalness += response['instrumentalness']
liveness += response['liveness']
valence += response['valence']
tempo += response['tempo']
track_counter += 1
print(track_counter)
danceability /= track_counter
energy /= track_counter
loudness /= track_counter
speechiness /= track_counter
acousticness /= track_counter
instrumentalness /= track_counter
liveness /= track_counter
valence /= track_counter
tempo /= track_counter
feature_list: Dict[str, int] = {'danceability': danceability, 'energy': energy, 'loudness': loudness, 'speechiness': speechiness, 'acousticness': acousticness', 'instrumentalness': instrumentalness, 'liveness': liveness, 'valence': valence, 'tempo': tempo}
return feature_list有没有人知道如何提高函数的速度,或者音频特征的查询是否总是需要很长时间?
我将非常感谢你的回答。提前谢谢。埃里克
发布于 2020-09-13 15:42:02
https://stackoverflow.com/questions/63861802
复制相似问题