1
0
Fork 0

Lastfm init refactored

This commit is contained in:
magnolia-fan 2011-10-20 00:55:46 +04:00
parent 29147d56d7
commit 62d7d378bd
7 changed files with 44 additions and 126 deletions

View File

@ -8,11 +8,10 @@
}
}
.desc {
height: 220px;
.service-icons {
margin-top: 15px;
a {
text_decoration: none;
text-decoration: none;
margin-right: 10px;
}
}

View File

@ -119,26 +119,4 @@ class ArtistController < ApplicationController
'0:00'
end
end
def autocomplete
autocomplete = getLastFmAutocomplete(params[:query])
return render :nothing => true if autocomplete.nil?
suggestions = []
autocomplete["response"]["docs"].each do |doc|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
end
render :json => {
:query => params[:query],
:suggestions => suggestions.take(5)
}
end
def getLastFmAutocomplete query
return nil if query.nil? or query.strip.empty?
json = ActiveSupport::JSON.decode(open(
'http://www.last.fm/search/autocomplete' <<
'?rows=30&q=' << URI.escape(query)
).read)
json.empty? ? nil : json
end
end

View File

@ -2,9 +2,7 @@ require 'open-uri'
require 'net/http'
class LastFmController < ApplicationController
before_filter :authorize, :except => [:connect]
@@api_key = '04fda005dbf61a50af5abc3e90f111f2'
@@secret = '19e70e98b291e9f15d0516925945eb1b'
before_filter :authorize, :except => [:connect, :autocomplete]
def connect
unless params[:sid].nil? or params[:token].nil?
@ -16,16 +14,15 @@ class LastFmController < ApplicationController
session.user.save
render :text => '<script>window.close();</script>'
else
render :text => 'You Don\'t Fool Me'
return render :text => 'You Don\'t Fool Me'
end
else
render :text => 'So Much Trouble In The World'
return render :text => 'So Much Trouble In The World'
end
end
def getinfo
@res = {}
user = User.find_by_vkid(params[:mid])
unless user.lastfm_key.nil?
render :json => {
@ -35,7 +32,7 @@ class LastFmController < ApplicationController
else
render :json => {
:connected => false,
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ @@api_key +
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFM.api_key +
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
}
end
@ -43,58 +40,66 @@ class LastFmController < ApplicationController
def listening
@res = {}
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
render :json => { :status => 'bad params' }
return
return render :json => { :status => 'bad params' }
end
user = User.find_by_vkid(params[:mid])
if user.lastfm_key.nil?
render :json => {:status => 'lastfm account is not connected', :user => user}
return
return render :json => {:status => 'lastfm account is not connected', :user => user}
end
r = LastFM::Track.update_now_playing(
:track => params[:name],
:artist => params[:artist],
:album => params[:album],
:trackNumber => params[:position].to_i,
# :mbid => params[:mbid],
:duration => params[:length].to_i,
:sk => user.lastfm_key # Auth session key
)
render :json => { :status => r['error'].nil? ? 'success' : r }
return
end
def scrobble
@res = {}
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
render :json => {:status => 'bad params'}
return
return render :json => { :status => 'bad params' }
end
user = User.find_by_vkid(params[:mid])
if user.lastfm_key.nil?
render :json => {:status => 'lastfm account is not connected', :user => user}
return
return render :json => { :status => 'lastfm account is not connected', :user => user }
end
r = LastFM::Track.scrobble(
:track => params[:name],
:timestamp => Time.now.utc.to_i,
:artist => params[:artist],
:album => params[:album],
:trackNumber => params[:position].to_i,
# :mbid => params[:mbid],
:duration => params[:length].to_i,
:sk => user.lastfm_key # Auth session key
)
render :json => { :status => r['error'].nil? ? 'success' : r }
return
end
def autocomplete
autocomplete = getSuggestions(params[:query])
return render :nothing => true if autocomplete.nil?
suggestions = []
autocomplete["response"]["docs"].each do |doc|
suggestions << doc["artist"] unless suggestions.include?(doc["artist"]) or doc["artist"].nil? or doc['restype'] != 6
end
render :json => {
:query => params[:query],
:suggestions => suggestions.take(5)
}
end
private
def getSuggestions query
return nil if query.nil? or query.strip.empty?
json = ActiveSupport::JSON.decode(open(
'http://www.last.fm/search/autocomplete' <<
'?rows=30&q=' << URI.escape(query)
).read)
json.empty? ? nil : json
end
end

View File

@ -27,20 +27,6 @@ class TrackController < ApplicationController
render :json => { :status => :failed }
end
def report_good
track = Track.find(params[:id])
unless track.nil?
# Track is now definitely available
track.available = true
track.save
# Saving track file
vote(params[:track_id], params[:owner_id], params[:audio_id], 1)
render :json => { :status => :success }
return
end
render :json => { :status => :failed }
end
def report_bad
track = Track.find(params[:id])
unless track.nil?

View File

@ -1,3 +1,7 @@
LastFM.api_key = '04fda005dbf61a50af5abc3e90f111f2'
LastFM.secret = '19e70e98b291e9f15d0516925945eb1b'
LastFM.client_name = 'BeatHaven'
lambda{
require 'yaml'
c = YAML.load(File.read("#{Rails.root}/config/lastfm.yml"))
LastFM.api_key = c['api_key']
LastFM.secret = c['secret']
LastFM.client_name = c['client_name']
}.call

3
config/lastfm.yml Normal file
View File

@ -0,0 +1,3 @@
api_key: 04fda005dbf61a50af5abc3e90f111f2
secret: 19e70e98b291e9f15d0516925945eb1b
client_name: BeatHaven

View File

@ -1,61 +1,4 @@
Beathaven::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
match '/' => 'application#index'
match '/greetings/' => 'application#greetings'
match '/about/' => 'application#about'
@ -72,6 +15,6 @@ Beathaven::Application.routes.draw do
match 'settings' => 'user#settings'
match 'artist/autocomplete' => 'artist#autocomplete'
match 'artist/autocomplete' => 'last_fm#autocomplete'
match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }
end