Module: Deezer

Defined in:
lib/deezer-lookup.rb

Class Method Summary collapse

Class Method Details

.access_token(token, levenshtein_thresholds = LEVENSHTEIN_THRESHOLDS) ⇒ Object



9
10
11
# File 'lib/deezer-lookup.rb', line 9

def self.access_token(token, levenshtein_thresholds = LEVENSHTEIN_THRESHOLDS)
  @access_token, @levenshtein_thresholds = token, levenshtein_thresholds
end

.levenshtein_thresholds=(thresholds) ⇒ Object



13
14
15
# File 'lib/deezer-lookup.rb', line 13

def self.levenshtein_thresholds=(thresholds)
  @levenshtein_thresholds = thresholds
end

.lookup(*query) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/deezer-lookup.rb', line 17

def self.lookup(*query)
  if query.size == 2
    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
  else
    # single track
    base_url = "http://api.deezer.com/track/#{query.first}"
    raw_json = open(base_url).read()
    return JSON.parse(raw_json)
  end
end