[Python] problem with spotify's discover weekly playlist [closed]

Discussão em 'Python' iniciado por Stack, Maio 4, 2025.

  1. Stack

    Stack Membro Participativo

    I've been trying to make a program that weekly copies the DW playlist into another playlist so you don't lose the songs.

    Here is my code:

    import spotipy


    def check_playlist_exists(sp: spotipy.Spotify, playlist_name: str) -> bool:
    playlists = sp.current_user_playlists()
    for playlist in playlists['items']:
    if playlist['name'] == playlist_name:
    return playlist['id']
    return None


    def create_discover_weekly_archive_playlist(sp: spotipy.Spotify):
    sp_user = sp.current_user()
    user_name = sp_user['uri'].split(":")[2]
    playlist_name = 'Discover Weekly Archive'
    playlist = check_playlist_exists(sp, playlist_name)

    if playlist == None:
    print(f"Creating playlist for user: {user_name}")
    playlist = sp.user_playlist_create(user=user_name, name=playlist_name, public=True)
    return playlist['id']
    else:
    return playlist


    def get_discover_weekly_tracks(sp: spotipy.Spotify):
    ids = []
    result = sp.playlist('37i9dQZEVXcVdyhNQ3ect2')
    for track in result['tracks']['items']:
    ids.append(track['track']['id'])
    return ids


    def add_tracks_to_playlist(sp: spotipy.Spotify, playlist_id, track_ids):
    # check if the track already exists in the playlist
    existing_tracks = sp.playlist_tracks(playlist_id)
    existing_track_ids = [item['track']['id'] for item in existing_tracks['items']]
    new_track_ids = [track_id for track_id in track_ids if track_id not in existing_track_ids]
    print(f"Adding {len(new_track_ids)} new tracks to the playlist")
    if len(new_track_ids) > 0:
    sp.playlist_add_items(playlist_id, new_track_ids)


    if __name__ == "__main__":
    # Initialize the Spotify client
    sp = spotipy.Spotify(auth_manager=spotipy.SpotifyOAuth(client_id='bd6a20ffd03c42009cd858f8a110ec6b',

    client_secret='3f1f7290c6654c80a281d3c92e50a75c',

    redirect_uri='https://127.0.0.1:8888/callback/',
    scope='playlist-modify-public'))

    track_ids = get_discover_weekly_tracks(sp)
    playlist_id = create_discover_weekly_archive_playlist(sp)
    add_tracks_to_playlist(sp, playlist_id, track_ids)


    It works on any other playlist but this one. My friend tried it and it somehow worked on his machine. and then magically stopped working after that. It worked on my machine but only create the other playlist on his account in mine (the one that's not DW).

    image containing the errors I'm getting

    Continue reading...

Compartilhe esta Página