Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a song to a playlist with pyspotify

Tags:

python

spotify

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.

like image 852
dwj300 Avatar asked Nov 24 '25 02:11

dwj300


1 Answers

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)
like image 140
jodal Avatar answered Nov 26 '25 15:11

jodal