Saturday 5 October 2019

spotify - Spotipy: How do I search by both artist and song



Given a song title and an artist name, I am trying to find the correct song using Spotipy. However, I do not see a way to search by both song title and artist: it's either one or the other:



   sp.search(q="Money", type="track", limit=10)
sp.search(q="Pink Floyd", type="artist", limit=10)



The problem with this is that I get a bunch of irrelevant results, especially if I search by track(example: top result for money is "Rent Money" by Future not "Money" by Pink Floyd). I could extend the limit and filter out irrelevant results, but considering I'll be doing this on a large scale, I'd rather just query Spotify correctly, take the first result, and move on. Is there any way to query on both track name and artist at the same time using Spotipy?


Answer



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



I think that you're misunderstanding type. I always want to return a track list so type is "track". In other words this defines the type of entities to be returned.



The query filter can be completely generic like "Money" or can be focussed to certain parameters like "artist:Floyd track:Money". This can be immensely powerful as you can look at albums, date fields, popularity and all sorts.



I commonly use

let q = String.init(format:"artist:%@ track:%@",artistName,trackName)



Don't forget to %-encode the string!


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...