Lastfm init refactored
This commit is contained in:
parent
29147d56d7
commit
62d7d378bd
@ -8,11 +8,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.desc {
|
.desc {
|
||||||
height: 220px;
|
|
||||||
.service-icons {
|
.service-icons {
|
||||||
margin-top: 15px;
|
margin-top: 15px;
|
||||||
a {
|
a {
|
||||||
text_decoration: none;
|
text-decoration: none;
|
||||||
margin-right: 10px;
|
margin-right: 10px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -119,26 +119,4 @@ class ArtistController < ApplicationController
|
|||||||
'0:00'
|
'0:00'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -2,9 +2,7 @@ require 'open-uri'
|
|||||||
require 'net/http'
|
require 'net/http'
|
||||||
|
|
||||||
class LastFmController < ApplicationController
|
class LastFmController < ApplicationController
|
||||||
before_filter :authorize, :except => [:connect]
|
before_filter :authorize, :except => [:connect, :autocomplete]
|
||||||
@@api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
|
||||||
@@secret = '19e70e98b291e9f15d0516925945eb1b'
|
|
||||||
|
|
||||||
def connect
|
def connect
|
||||||
unless params[:sid].nil? or params[:token].nil?
|
unless params[:sid].nil? or params[:token].nil?
|
||||||
@ -16,16 +14,15 @@ class LastFmController < ApplicationController
|
|||||||
session.user.save
|
session.user.save
|
||||||
render :text => '<script>window.close();</script>'
|
render :text => '<script>window.close();</script>'
|
||||||
else
|
else
|
||||||
render :text => 'You Don\'t Fool Me'
|
return render :text => 'You Don\'t Fool Me'
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
render :text => 'So Much Trouble In The World'
|
return render :text => 'So Much Trouble In The World'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def getinfo
|
def getinfo
|
||||||
@res = {}
|
@res = {}
|
||||||
|
|
||||||
user = User.find_by_vkid(params[:mid])
|
user = User.find_by_vkid(params[:mid])
|
||||||
unless user.lastfm_key.nil?
|
unless user.lastfm_key.nil?
|
||||||
render :json => {
|
render :json => {
|
||||||
@ -35,7 +32,7 @@ class LastFmController < ApplicationController
|
|||||||
else
|
else
|
||||||
render :json => {
|
render :json => {
|
||||||
:connected => false,
|
: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
|
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@ -43,58 +40,66 @@ class LastFmController < ApplicationController
|
|||||||
|
|
||||||
def listening
|
def listening
|
||||||
@res = {}
|
@res = {}
|
||||||
|
|
||||||
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
|
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
|
||||||
render :json => { :status => 'bad params' }
|
return render :json => { :status => 'bad params' }
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
user = User.find_by_vkid(params[:mid])
|
user = User.find_by_vkid(params[:mid])
|
||||||
if user.lastfm_key.nil?
|
if user.lastfm_key.nil?
|
||||||
render :json => {:status => 'lastfm account is not connected', :user => user}
|
return render :json => {:status => 'lastfm account is not connected', :user => user}
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
r = LastFM::Track.update_now_playing(
|
r = LastFM::Track.update_now_playing(
|
||||||
:track => params[:name],
|
:track => params[:name],
|
||||||
:artist => params[:artist],
|
:artist => params[:artist],
|
||||||
:album => params[:album],
|
:album => params[:album],
|
||||||
:trackNumber => params[:position].to_i,
|
:trackNumber => params[:position].to_i,
|
||||||
# :mbid => params[:mbid],
|
|
||||||
:duration => params[:length].to_i,
|
:duration => params[:length].to_i,
|
||||||
:sk => user.lastfm_key # Auth session key
|
:sk => user.lastfm_key # Auth session key
|
||||||
)
|
)
|
||||||
|
|
||||||
render :json => { :status => r['error'].nil? ? 'success' : r }
|
render :json => { :status => r['error'].nil? ? 'success' : r }
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def scrobble
|
def scrobble
|
||||||
@res = {}
|
@res = {}
|
||||||
|
|
||||||
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
|
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
|
||||||
render :json => {:status => 'bad params'}
|
return render :json => { :status => 'bad params' }
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
user = User.find_by_vkid(params[:mid])
|
user = User.find_by_vkid(params[:mid])
|
||||||
if user.lastfm_key.nil?
|
if user.lastfm_key.nil?
|
||||||
render :json => {:status => 'lastfm account is not connected', :user => user}
|
return render :json => { :status => 'lastfm account is not connected', :user => user }
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
r = LastFM::Track.scrobble(
|
r = LastFM::Track.scrobble(
|
||||||
:track => params[:name],
|
:track => params[:name],
|
||||||
:timestamp => Time.now.utc.to_i,
|
:timestamp => Time.now.utc.to_i,
|
||||||
:artist => params[:artist],
|
:artist => params[:artist],
|
||||||
:album => params[:album],
|
:album => params[:album],
|
||||||
:trackNumber => params[:position].to_i,
|
:trackNumber => params[:position].to_i,
|
||||||
# :mbid => params[:mbid],
|
|
||||||
:duration => params[:length].to_i,
|
:duration => params[:length].to_i,
|
||||||
:sk => user.lastfm_key # Auth session key
|
:sk => user.lastfm_key # Auth session key
|
||||||
)
|
)
|
||||||
|
|
||||||
render :json => { :status => r['error'].nil? ? 'success' : r }
|
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
|
||||||
end
|
end
|
||||||
|
@ -27,20 +27,6 @@ class TrackController < ApplicationController
|
|||||||
render :json => { :status => :failed }
|
render :json => { :status => :failed }
|
||||||
end
|
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
|
def report_bad
|
||||||
track = Track.find(params[:id])
|
track = Track.find(params[:id])
|
||||||
unless track.nil?
|
unless track.nil?
|
||||||
|
@ -1,3 +1,7 @@
|
|||||||
LastFM.api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
lambda{
|
||||||
LastFM.secret = '19e70e98b291e9f15d0516925945eb1b'
|
require 'yaml'
|
||||||
LastFM.client_name = 'BeatHaven'
|
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
3
config/lastfm.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
api_key: 04fda005dbf61a50af5abc3e90f111f2
|
||||||
|
secret: 19e70e98b291e9f15d0516925945eb1b
|
||||||
|
client_name: BeatHaven
|
@ -1,61 +1,4 @@
|
|||||||
Beathaven::Application.routes.draw do
|
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 '/' => 'application#index'
|
||||||
match '/greetings/' => 'application#greetings'
|
match '/greetings/' => 'application#greetings'
|
||||||
match '/about/' => 'application#about'
|
match '/about/' => 'application#about'
|
||||||
@ -72,6 +15,6 @@ Beathaven::Application.routes.draw do
|
|||||||
|
|
||||||
match 'settings' => 'user#settings'
|
match 'settings' => 'user#settings'
|
||||||
|
|
||||||
match 'artist/autocomplete' => 'artist#autocomplete'
|
match 'artist/autocomplete' => 'last_fm#autocomplete'
|
||||||
match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }
|
match 'artist/(:name)/' => 'artist#data', :constraints => { :name => /[^\/]*/ }
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user