Lastfm init refactored
This commit is contained in:
@@ -8,11 +8,10 @@
|
||||
}
|
||||
}
|
||||
.desc {
|
||||
height: 220px;
|
||||
.service-icons {
|
||||
margin-top: 15px;
|
||||
a {
|
||||
text_decoration: none;
|
||||
text-decoration: none;
|
||||
margin-right: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user