3

Using the Spotify API, I am trying to search through tracks of an artist. The query I used is working, and looks like:

https://api.spotify.com/v1/search?q=track:' + song + '%20artist:' + artist + '&type=track&limit=10

It's working however it's returning tracks for artists containing the artist name provided in any part of their artist name string. For example, if I want to be explicit about artist name, and want to search in ZHU's tracks, I couldn't find a way how I should make the query, so it becomes explicit about artist's name.

As I said, if I want to search in ZHU's tracks, it also returns me Natalie Zhu's tracks as well.

What is the proper way of being explicit about artist name?

Is there a way to explicitly define that I am going to search in ZHU's tracks?


Edit: I also tried using quotation marks:

https://api.spotify.com/v1/search?q=track:"' + song + '"%20artist:"' + artist + '"&type=track&limit=10

Maybe I used it wrong, I am not sure, but if I didn't this doesn't seem to solve :/

senty
  • 12,385
  • 28
  • 130
  • 260
  • Put the song or artist name in quotation marks. https://developer.spotify.com/web-api/search-item/ – Cᴏʀʏ Dec 12 '16 at 22:50
  • I tried that but didn't make any difference. Maybe I did it wrong. `https://api.spotify.com/v1/search?q=track:"' + song + '"%20artist:"' + artist + '"&type=track&limit=10`. Is this the right way? Or did I do something wrong? If I did it right, then it's not solving the problem :/ – senty Dec 12 '16 at 22:53

3 Answers3

1

https://developer.spotify.com/web-api/search-item/

Under the track object (full) section, the keyword for the title of the song is not "track" but rather "name".

https://api.spotify.com/v1/search?q=name:' + song + '%20artist:' + artist + '&type=track&limit=10
Luca Guarro
  • 1,085
  • 1
  • 11
  • 25
1

It seems that currently there is no way to explicitly search for tracks of a single artist. You can only limit artists by name, as you have done, but this has the drawback of potentially including tracks by other artists whose name contains the name of the intended artist, as you explained. The only way around this is to filter the returned results and fetch more if necessary.

I have opened an issue about this at the Soptify API issue tracker Github repo, but so far, there is no info about the implementation.

Waiski
  • 9,214
  • 3
  • 21
  • 30
-1

I found a work around iterating trough the results of the query until you find an exact match to the artist name you are searching for. With the 20 limit I got 2269 out of 2280 artist I was looking for. Using Spotipy library.

 
        artist = "ZHU"
        search_results = sp.search(artist, 20, 0, "artist")
        if len(search_results['artists']['items']) != 0:
            total = len(search_results['artists']['items'])
            z = 0
            artist_info = search_results['artists']['items'][z]
            name = artist_info['name']
            while name != artist:
                z += 1
                artist_info = search_results['artists']['items'][z]
                name = artist_info['name']
                if z+1 == total:
                    break
                
Walt
  • 1
  • 2
  • 1
    Since you are formally posting an answer to an older question. It would be most helpful that you support your purported answer with some code and the output that results from using your code. – Gray Sep 24 '20 at 21:21