dwj300
dwj300

Reputation: 31

Adding a song to a playlist with pyspotify

I am trying to use pyspotify to search spotify for a track, then add the most popular one (most relevant search result) to a specific playlist. I can do it in c with libspotify, like this:

sp_playlistcontainer *container = sp_session_playlistcontainer(session);
sp_playlist *playlist = sp_playlistcontainer_playlist(container, 66); // 66 is the index of a playlist that I want to add songs to
sp_search *search = sp_search_create(session, "wild ones", 0, 1, 0, 0, 0, 0, 0, 0, SP_SEARCH_STANDARD, &search_complete, NULL);
sp_track *track = sp_search_track(search, 0);
sp_playlist_add_tracks(playlist, (const sp_track **) &track, 1, 0, session);

How would I do effectively the same thing using pyspotify? The only example given is jukebox.py and that is way too complicated for what I am doing. I would like to be able to achieve this in under 20 lines of python.

Also, how would I handle logging in in pyspotify? With libspotify I simply used one of the examples.

Upvotes: 3

Views: 1027

Answers (1)

jodal
jodal

Reputation: 193

I'm two years late, but for future readers, this is roughly how you'd do this with pyspotify v2.x:

import spotify
session = spotify.Session()
# Login, etc.
playlist = session.playlist_container[66]
search = session.search('wild ones')
search.loaded_event.wait()
track = search.tracks[0]
playlist.add_tracks(track)

Upvotes: 1

Related Questions