spotifyr icon indicating copy to clipboard operation
spotifyr copied to clipboard

Searching for tracks returns matches on artist/album names

Open KyleGoldberg opened this issue 7 years ago • 0 comments

The search_spotify function will currently return matches for artist/album names on occasion before track names when using the type = 'track' parameter.

For example,

search_spotify('Post', type = 'track')

will return when cleaned up -

"artist" "album" "track" "Post Malone" "rockstar" "rockstar" "Post Malone" "Stoney (Deluxe)" "I Fall Apart" "Post Malone" "Candy Paint" "Candy Paint" "Post Malone" "Stoney (Deluxe)" "Congratulations" "Post Malone" "Stoney (Deluxe)" "Go Flex" "Post Malone" "Stoney (Deluxe)" "White Iverson" "Lorde" "Homemade Dynamite (Feat. Khalid, Post Malone & SZA) [REMIX]" "Homemade Dynamite (Feat. Khalid, Post Malone & SZA) - REMIX" "Post Malone" "Stoney (Deluxe)" "No Option" "Post Malone" "Stoney (Deluxe)" "Yours Truly, Austin Post" "Post Malone" "Stoney (Deluxe)" "Too Young" "Post Malone" "Stoney (Deluxe)" "Feeling Whitney" "Post Malone" "The Fate of the Furious: The Album" "Candy Paint" "Post Malone" "Stoney (Deluxe)" "Leave" "Post Malone" "Stoney (Deluxe)" "Big Lie" "Post Malone" "Stoney (Deluxe)" "Patient" "Post Malone" "Stoney (Deluxe)" "Up There" "Post Malone" "Stoney (Deluxe)" "Hit This Hard" "Post Malone" "Stoney (Deluxe)" "Feel" "Post Malone" "Stoney (Deluxe)" "Broken Whiskey Glass" "Post Malone" "Stoney (Deluxe)" "Cold"

The following code adjustment to the function seems to work to fix the issue and prioritize returning track name matches rather than artist/album matches.

search_spotify <- function(q, type = c('artist', 'album', 'playlist', 'track'), ...){
  type <- match.arg(type)

  #
  q_fix <- ifelse(type == 'track', paste0('track:',q),q)
  #

  response <- GET(url = "https://api.spotify.com/v1/search",
                  add_headers(Authorization = glue('Bearer {access_token}')),
                  query = list(q = q_fix, type = type,...))
  get_response_content(response)
}

Reference: Comment section on https://developer.spotify.com/web-api/search-item/

KyleGoldberg avatar Jan 12 '18 14:01 KyleGoldberg