module Deezer

Public Class Methods

access_token(token, levenshtein_thresholds = LEVENSHTEIN_THRESHOLDS) click to toggle source
# 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) click to toggle source
# File lib/deezer-lookup.rb, line 13
def self.levenshtein_thresholds=(thresholds)
  @levenshtein_thresholds = thresholds
end
lookup(*query) click to toggle source
# 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