Search code examples
pythonspotifyspotipy

Checking to see if a song is available to play with Spotipy


My problem: Differentiate between tracks that are playable and not.

I am trying to use Spotipy to print me the metadata details of all the tracks in a playlist. Some songs I have added to my playlist is no longer available, and therefore do not show in my spotify playlist (greyed out). However they do print normally togheter with the rest of the tracks that are available. I do not want them there; if I can't play them at this time I don't want them to show in my printed playlist.

I check which markets the track is available in to determine if it should print or not. This is working fine.

'available_markets': []

The problem I have is that there are some tracks that do not have any available markets and are empty like the code snippet shows. They are identical to the songs tracks that are unavailable to me. But I can still play them! This means my code to remove unavailable songs also removes playable song.

I haven't found another way to get data on if a track really is available or not. Anything I have missed?

Update: Found a solution. Posted as answer.


Solution

  • I recently picked this project back up, and found a solution that kind of works.

    Every track has a is_playable entry, but it only shows up if you specify a market when looking up the track. This solves the problem where the market list of the track is blank, but the track is still playable in your market. At least so far in my tests it found the unplayable tracks correctly.

    track(track_id, market='NO')
    TypeError: track() got an unexpected keyword argument 'market'
    

    I couldn't get my code to work when adding this parameter to Spotipys track(). I only got errors. My market is Norway.

    However I discovered that if I added the market directly into the function it worked.. Somehow.

    track('spotify:track:0eueRkrPsChXjeJA421OWp?market=NO')
    mytrack['is_playable']
    

    I also discovered that if I used the market argument in the playlist_tracks() function it worked with no error, even though I used it in the same way as the in the track() function.

    user_playlist_tracks(user=None, playlist_id=None, fields=None, limit=100, offset=0, market=None)
    

    I used it like this:

    playlist_tracks('myplaylistid', offset=0, market='NO')
    mytrack['track']['is_playable']
    

    I have no idea why. With the same input in the two different functions I get an error on one and not on the other. Maybe I am doing something wrong? Anyway I thought I should update here in case it might help somebody else with the same problem.