Tuesday, 23 January 2018

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...