1
0
Fork 0

True autocomplete

This commit is contained in:
Gregory Eremin 2012-09-19 02:42:20 +04:00
parent 7fcddbabd5
commit 7ff2dc8b12
5 changed files with 55 additions and 24 deletions

View File

@ -21,8 +21,10 @@ module Robbie
class << self
def setup(params)
const_set(:API_KEY, params[:api_key])
const_set(:API_SECRET, params[:api_secret])
const_set(:META_API_KEY, params[:meta_api_key])
const_set(:META_API_SECRET, params[:meta_api_secret])
const_set(:AUTOCOMPLETE_API_KEY, params[:autocomplete_api_key])
const_set(:AUTOCOMPLETE_API_SECRET, params[:autocomplete_api_secret])
end
def enable_cache

View File

@ -8,6 +8,10 @@ module Robbie
def predict(q)
Parsers::Search.single_stage_search(q)
end
def search(q)
Parsers::Search.search(q)
end
end
end
end

View File

@ -26,6 +26,8 @@ module Robbie
Parsers::Track.parse_meta(track, current_disc, position)
end
else
album.tracks = []
end
album
end

View File

@ -4,20 +4,35 @@ module Robbie
include HTTParty
base_uri "api.rovicorp.com"
format :json
@@calls = []
@@meta_calls = []
@@autocomplete_calls = []
class << self
def sig
Digest::MD5.hexdigest("#{API_KEY}#{API_SECRET}#{Time.now.to_i}")
def sig(type = :meta)
case type
when :meta
Digest::MD5.hexdigest("#{META_API_KEY}#{META_API_SECRET}#{Time.now.to_i}")
when :autocomplete
Digest::MD5.hexdigest("#{AUTOCOMPLETE_API_KEY}#{AUTOCOMPLETE_API_SECRET}#{Time.now.to_i}")
end
end
def query(path, params)
unless Robbie.const_defined?(:API_KEY) and Robbie.const_defined?(:API_SECRET)
raise Exception.new("No API credentials given")
type = (path.match(/^\/search\/v2\/music\/autocomplete/) ? :autocomplete : :meta)
if type == :meta
unless Robbie.const_defined?(:META_API_KEY) and Robbie.const_defined?(:META_API_SECRET)
raise Exception.new("No meta API credentials given")
end
elsif type == :autocomplete
unless Robbie.const_defined?(:AUTOCOMPLETE_API_KEY) and Robbie.const_defined?(:AUTOCOMPLETE_API_SECRET)
raise Exception.new("No autocomplete API credentials given")
end
end
params_str = params
.merge({ apikey: API_KEY, sig: sig, format: "json" })
.merge({ apikey: (type == :meta ? META_API_KEY : AUTOCOMPLETE_API_KEY), sig: sig(type), format: "json" })
.map{ |key, val| "#{key}=#{val}" }.join("&")
if Robbie.cache_enabled?
@ -37,13 +52,21 @@ module Robbie
end
end
def load(uri)
def load(type = :meta, uri)
if type == :meta
calls = @@meta_calls
limit = 5
elsif type == :autocomplete
calls = @@autocomplete_calls
limit = 10
end
if Robbie.free_limits?
@@calls = @@calls.length > 5 ? @@calls.slice(-5, 5) : @@calls
if @@calls.length > 5 && Time.now.to_f - @@calls.first <= 1.0
sleep(1.05 - (Time.now.to_f - @@calls.first))
calls = calls.length > limit ? calls.slice(-limit, limit) : calls
if calls.length > limit && Time.now.to_f - calls.first <= 1.0
sleep(1.05 - (Time.now.to_f - calls.first))
end
@@calls << Time.now.to_f
calls << Time.now.to_f
end
get(uri)

View File

@ -4,7 +4,7 @@ module Robbie
class << self
def search(q)
response = query("/search/v2.1/music/search", {
query: q.gsub(" ", "%20"),
query: q.gsub(/\s| |%20/, "+"),
entitytype: "artist&entitytype=album"
})
parse(response["searchResponse"]["results"])
@ -12,13 +12,22 @@ module Robbie
def single_stage_search(q)
response = query("/search/v2.1/music/singlestagesearch", {
query: q.gsub(" ", "%20"),
query: q.gsub(/\s| |%20/, "+"),
entitytype: "artist&entitytype=album",
size: 10
})
parse(response["searchResponse"]["results"])
end
def autocomplete(q)
response = query("/search/v2/music/autocomplete", {
query: q.gsub(/\s| |%20/, "+"),
entitytype: "artist&entitytype=album",
size: 10
})
response["autocompleteResponse"]["results"]
end
def parse(data)
return if data.nil?
@ -30,15 +39,6 @@ module Robbie
end
end
end
def autocomplete(q)
response = query("/search/v2.1/music/autocomplete", {
query: q.gsub(" ", "%20"),
entitytype: "artist&entitytype=album",
size: 10
})
response["autocompleteResponse"]["results"]
end
end
end
end