4

Usually, spotipy requires track IDs as parameters to be passed, in order to return track names.

Say I have two lists, not obtained from the Spotify API:

  1. artists

    [u'Moses Sumney', u'Cherry Glazerr', u'Danny Brown', u'NxWorries']
    

    and their respective songs:

  2. tracks

    [u'Lonely World', u"Told You I'd Be With the Guys", u'Really Doe [ft. Kendrick Lamar, Ab-Soul, and Earl Sweatshirt]', u'Lyk Dis']
    

Is it possible to do it the other way around and get track IDs?

Nissa
  • 4,636
  • 8
  • 29
  • 37
8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

1 Answers1

11

Spotipy.search() is what you are looking for.

import spotipy
sp = spotipy.Spotify()

artist= 'Moses Sumney'
track= 'Lonely World'

track_id = sp.search(q='artist:' + artist + ' track:' + track, type='track')

This will return a list of songs that match the query, depending on how precise your search is will depend on how many results are returned.

macleay
  • 125
  • 3
  • 5
  • 1
    Thanks. May I ask where you found documentation of the search query syntax? I couldn't figure it out until I found your answer – Mike S Apr 11 '17 at 03:36
  • 3
    I just tested this and needed to add an auth token when creating the `sp` object: `sp = spotipy.Spotipy(auth='TOKEN')`. – atwalsh Jun 29 '17 at 00:52
  • Not clear to me how to extract the id from the returned JSON. You seem to have to use a list index which looks suspiciously like it might change position. – Superdooperhero Jan 01 '21 at 08:02
  • 1
    `trackId = track_id['tracks']['items'][0]['id']` did it for me – Superdooperhero Jan 01 '21 at 08:05