Class: Mumbletune::Resolvers::SpotifySearchResolver

Inherits:
Resolver
  • Object
show all
Defined in:
lib/mumbletune/resolver.rb

Instance Method Summary collapse

Methods inherited from Resolver

inherited

Instance Method Details

#matches?(query) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# File 'lib/mumbletune/resolver.rb', line 70

def matches?(query)
  # basically we will search for anything that's not a URL
  if URI.extract(query).any?
    return false
  else
    return true
  end
end

#resolve(query) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mumbletune/resolver.rb', line 78

def resolve(query)
  first_word = query.split.first

  # if first word is a type to search for, it needs to be stripped
  #    from the query so we don't search for it (e.g. "track starships")
  if first_word =~ /^(artist|album|track)$/i
    query_a = query.split
    query_a.delete_at 0
    query = query_a.join(" ")
  end

  # used to check if tracks are playable in in region
  region = Mumbletune.config["spotify"]["region"]

  # determine result based on a type in the first word
  search = Hallon::Search.new(query, artists: 1, albums: 1, tracks: 1).load
  if first_word =~ /^artist$/i
    result = search.artists.first

  elsif first_word =~ /^album$/i
    result = search.albums.first

  elsif first_word =~ /^track$/i
    result = search.tracks.first

  else # determine intended result by similarity to the query
    compare = []
    compare.push search.tracks.first if search.tracks.any?
    compare.push search.albums.first if search.albums.any?
    compare.push search.artists.first if search.artists.any?
    
    white = Text::WhiteSimilarity.new
    compare.sort! do |a, b|
      a_sim = white.similarity(query, a.name)
      b_sim = white.similarity(query, b.name)
      if a_sim > b_sim
        -1
      elsif b_sim > a_sim
        1
      else
        0
      end
    end
    result = compare.first
  end

  if result.class == Hallon::Artist
    SpotifyResolver.tracks_from_artist(result)
  elsif result.class == Hallon::Album
    SpotifyResolver.tracks_from_album(result)
  elsif result.class == Hallon::Track
    SpotifyResolver.track(result)
  end

end