Search code examples
c#apispotify

Spotify Search API - C# Code works but I get no result found


Dears, I am querying Spotify API using the following code

 public Spotify_Search_Result Search(string artist_name, string song_name, int limit=1) {

        Spotify_Search_Result result = new Spotify_Search_Result();


        string text = artist_name + "%20" + song_name;
        //string text = artist_name + "+" + song_name;
        //string text = artist_name + " " + song_name;

        //string text = Uri.EscapeDataString(artist_name) + " " + Uri.EscapeDataString(song_name);
        //string text = Uri.EscapeDataString(artist_name) + "%20" + Uri.EscapeDataString(song_name);
        //string text = Uri.EscapeDataString(artist_name) + "+" + Uri.EscapeDataString(song_name);
        string url = "https://api.spotify.com/v1/search";
        string query =  url + 
                        "?q="+
                        text+
                        "&type=track"+           
                        "&offset=0"+
                        "&limit="+
                        limit.ToString();

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(query);

        webRequest.Method = "GET";
        webRequest.ContentType = "application/json";
        webRequest.Accept = "application/json";
        webRequest.Headers.Add("Authorization", "Bearer " + access_token);
        
        String json = null;

        try
        {

            HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
           
            using (Stream respStr = resp.GetResponseStream())
            {
                using (StreamReader rdr = new StreamReader(respStr, Encoding.UTF8))
                {
                    //should get back a string i can then turn to json and parse for accesstoken
                    json = rdr.ReadToEnd();
                    rdr.Close();
                }
            }
        }
        catch (Exception ex) {
            Console.WriteLine("Spotify search result error: " + ex.Message + " ["+artist_name+"]-["+song_name+"]" );
        
        }
        if (json != null)
        {

            result = JsonConvert.DeserializeObject<Spotify_Search_Result>(json);
        }
        return result;
    }

Problem: for certain values of artist_name and song_name this code returns no matching items. Example: artist_name=Delta V song_name=Il primo giorno del mondo

variable json value will be: { "tracks" : { "href" : "https://api.spotify.com/v1/search?query=Delta+V+Il+Primo+Giorno+Del+Mondo&type=track&offset=0&limit=20", "items" : [ ], "limit" : 20, "next" : null, "offset" : 0, "previous" : null, "total" : 0 } }

if I type same artist_name and song_name in SpotifyForDevelopers console I get a good match.

Now...where is the problem? I think it is the way I format the "text" and pass it to the API. I am not sure. As you see from code I have tried different ways of formatting "text" variable. Any hint on what I am doing wrong?

Thanks!


Solution

  • I think your code is correct and the problem could lie with the "market" parameter, i.e. songs can be available for some countries and unavailable for others. Your example query will return the right result if you set the market to Italy (IT) but not when set to United States (US), for example (notice the added market=IT):

    https://api.spotify.com/v1/search?query=elta%20V%20Il%20primo%20giorno%20del%20mondo&type=track&offset=0&limit=20&market=IT

    Maybe setting the market correctly might help to reduce/avoid the issue.