加快音频特征的查询速度(Spotify Web API)
Speed up the query of audio features (Spotify Web API)
我目前正在编写一个 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
有没有人知道我怎样才能加快这个功能,或者音频特征的查询是否总是需要很长时间?
非常感谢您的回答。
提前致谢。
埃里克
我目前正在编写一个 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
有没有人知道我怎样才能加快这个功能,或者音频特征的查询是否总是需要很长时间?
非常感谢您的回答。 提前致谢。 埃里克