【问题标题】:Python - Creating Spotify playlist repeats the same track?Python - 创建 Spotify 播放列表重复相同的曲目?
【发布时间】:2021-02-21 11:16:37
【问题描述】:

我正在尝试创建一个 Python 程序来根据用户的“心情”创建 Spotify 播放列表;也就是说,用户选择一种特定的情绪,程序返回一个播放列表,其中歌曲(取自用户保存的曲目)的特征表明它们与所述情绪相匹配。但是,我遇到了播放列表由重复 30 次的同一首歌曲组成的问题,而不是 30 首不同的歌曲。总的来说,我对 Python 和编程还很陌生,所以也许这不是一个非常困难的问题,但我正在努力解决这个问题,但我无法做到。

这是我的主要代码中引用此问题的路线(我的完整程序有更多路线,到目前为止一切正常)。所有其他情绪将由其他路线定义,但遵循相同的逻辑。为了更好地理解问题,我会提供任何可能需要的额外代码。

@app.route("/playlist_mood_angry")
def mood_playlist_angry():
    # define mood and create empty track list
    selected_mood = "Angry"
    tracks_uris = [] # we need all the tracks uri to add to the future playlist
    if 'auth_header' in session:
        auth_header = session['auth_header']
        # get user profile and saved tracks
        user_profile_data = spotify.get_users_profile(auth_header)
        user_id = user_profile_data["id"]
        saved_tracks_data = spotify.get_user_saved_tracks(auth_header)
        playlist_name = 'CSMoodlet: Angry'
        playlist_description = "A playlist for when you're just pissed off and want a soundtrack to go with it. Automatically curated by CSMoodlet."

        # go through saved tracks dictionary, get the tracks and for each one check if features average matches selected mood
        for item in saved_tracks_data["items"]:
            track = item["track"]
            features = sp.audio_features(track['id'])
            acousticness = features[0]['acousticness']
            danceability = features[0]['danceability']
            energy = features[0]['energy']
            speechiness = features[0]['speechiness']
            valence = features[0]['valence']
            track_mood = spotify.define_mood(acousticness, danceability, energy, speechiness, valence)

            # if the track's mood is "angry", if the list is not 30 tracks long yet, append it to the list
            if track_mood == "Angry": #if track's mood is not Angry, it will go back to the for loop to check the next one (THIS DOESN'T WORK)
                while len(tracks_uris) < 30:
                    track_uri = "spotify:track:{}".format(track['id'])
                    tracks_uris.append(track_uri)

        # once it has gone through all saved tracks, create the playlist and add the tracks
        new_playlist = spotify.create_playlist(auth_header, user_id, playlist_name, playlist_description)
        new_playlist_id = new_playlist['id']
        added_playlist = spotify.add_tracks_to_playlist(auth_header, new_playlist_id, tracks_uris)
        playlist = spotify.get_playlist(auth_header, new_playlist_id)
        tracks_data = []
        for item in playlist["items"]:
            track_data = item["track"]
            tracks_data.append(track_data)
        return render_template("created_playlist.html", selected_mood = selected_mood, playlist_tracks=tracks_data)

任何帮助将不胜感激。如果有什么不好的解释,我深表歉意,我是 Stackoverflow 的新手,英语不是我的第一语言。

【问题讨论】:

    标签: python python-3.x api spotify spotify-app


    【解决方案1】:

    发现错误! 对于遇到相同问题的任何人,我发现它实际上非常简单。当检查播放列表的长度小于 30 时,只需用 if 循环替换 while 循环。

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2017-03-15
      • 2012-12-20
      相关资源
      最近更新 更多