17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/deezer-lookup.rb', line 17
def self.lookup(*query)
base_url = "http://api.deezer.com/search?access_token=#{@access_token}&q="
base_url += URI.escape( query.join(' ').downcase )
raw_json = open(base_url).read()
results, input_song_title, input_artist_name, filtered_results = JSON.parse(raw_json)['data'], query[0].downcase, query[1].downcase, []
results.each do |r|
song_title, artist_name = r['title'].downcase, r['artist']['name'].downcase
r['levenshtein'] = [ DamerauLevenshtein.distance(song_title, input_song_title), DamerauLevenshtein.distance(artist_name, input_artist_name) ]
end
if @levenshtein_thresholds
results.each do |r|
song_title, artist_name = r['title'].downcase, r['artist']['name'].downcase
if r['levenshtein'][0] <= @levenshtein_thresholds[:song] and r['levenshtein'][1] <= @levenshtein_thresholds[:artist]
filtered_results.push(r)
end
end
filtered_results
else
results
end
end
|