2011-06-22 07:23:07 +00:00
|
|
|
require 'open-uri'
|
2011-06-22 22:56:32 +00:00
|
|
|
require 'lastfm'
|
2011-06-22 07:23:07 +00:00
|
|
|
|
|
|
|
class LastFmController < ApplicationController
|
2011-09-17 11:38:49 +00:00
|
|
|
before_filter :authorize
|
2011-06-22 07:23:07 +00:00
|
|
|
@@api_key = '04fda005dbf61a50af5abc3e90f111f2'
|
|
|
|
@@secret = '19e70e98b291e9f15d0516925945eb1b'
|
|
|
|
|
|
|
|
def connect
|
|
|
|
unless params[:sid].nil? or params[:token].nil?
|
|
|
|
session = Session.find_by_key(params[:sid])
|
|
|
|
unless session.nil?
|
2011-06-22 22:56:32 +00:00
|
|
|
lastfm = Lastfm.new(@@api_key, @@secret)
|
|
|
|
lastfm.session = lastfm.auth.get_session(params[:token])
|
|
|
|
session.user.lastfm_key = lastfm.session['key']
|
|
|
|
session.user.lastfm_username = lastfm.session['name']
|
2011-06-22 07:23:07 +00:00
|
|
|
session.user.save
|
|
|
|
render :text => '<script>window.close();</script>'
|
|
|
|
else
|
|
|
|
render :text => 'You Don\'t Fool Me'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render :text => 'So Much Trouble In The World'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def getinfo
|
|
|
|
@res = {}
|
|
|
|
|
2011-06-22 22:56:32 +00:00
|
|
|
user = User.find_by_vkid(params[:mid])
|
|
|
|
unless user.lastfm_key.nil?
|
|
|
|
render :json => {
|
|
|
|
:connected => true,
|
|
|
|
:username => user.lastfm_username
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render :json => {
|
|
|
|
:connected => false,
|
2011-06-23 05:39:31 +00:00
|
|
|
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ @@api_key +
|
|
|
|
'&cb=http://'+ request.host << '/lastfm/connect/?sid='+ user.session.key
|
2011-06-22 22:56:32 +00:00
|
|
|
}
|
2011-06-22 07:23:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-22 22:56:32 +00:00
|
|
|
def listening
|
2011-06-22 23:44:27 +00:00
|
|
|
@res = {}
|
|
|
|
|
2011-06-26 06:13:16 +00:00
|
|
|
if params[:artist].nil? or params[:album].nil? or params[:track].nil?
|
2011-06-22 23:44:27 +00:00
|
|
|
render :json => {:status => 'bad params'}
|
|
|
|
return
|
|
|
|
end
|
2011-06-22 22:56:32 +00:00
|
|
|
|
|
|
|
user = User.find_by_vkid(params[:mid])
|
2011-06-22 23:44:27 +00:00
|
|
|
if user.lastfm_key.nil?
|
|
|
|
render :json => {:status => 'lastfm account is not connected', :user => user}
|
|
|
|
return
|
|
|
|
end
|
2011-06-22 22:56:32 +00:00
|
|
|
|
|
|
|
lastfm = Lastfm.new(@@api_key, @@secret)
|
|
|
|
lastfm.session = user.lastfm_key
|
2011-06-22 23:44:27 +00:00
|
|
|
r = lastfm.track.update_now_playing(params[:artist], params[:track])
|
|
|
|
|
|
|
|
render :json => {:status => r ? 'success' : 'failed'}
|
|
|
|
return
|
2011-06-22 07:23:07 +00:00
|
|
|
end
|
|
|
|
|
2011-06-22 22:56:32 +00:00
|
|
|
def scrobble
|
2011-06-22 23:44:27 +00:00
|
|
|
@res = {}
|
|
|
|
|
2011-06-26 06:13:16 +00:00
|
|
|
if params[:artist].nil? or params[:album].nil? or params[:track].nil?
|
2011-06-22 23:44:27 +00:00
|
|
|
render :json => {:status => 'bad params'}
|
|
|
|
return
|
|
|
|
end
|
2011-06-22 22:56:32 +00:00
|
|
|
|
|
|
|
user = User.find_by_vkid(params[:mid])
|
2011-06-22 23:44:27 +00:00
|
|
|
if user.lastfm_key.nil?
|
|
|
|
render :json => {:status => 'lastfm account is not connected', :user => user}
|
|
|
|
return
|
|
|
|
end
|
2011-06-22 22:56:32 +00:00
|
|
|
|
|
|
|
lastfm = Lastfm.new(@@api_key, @@secret)
|
|
|
|
lastfm.session = user.lastfm_key
|
2011-06-26 06:13:16 +00:00
|
|
|
r = lastfm.track.scrobble(params[:artist], params[:track], params[:album], Time.now.utc.to_i)
|
2011-06-22 23:44:27 +00:00
|
|
|
|
2011-06-23 00:59:11 +00:00
|
|
|
render :json => {:status => r ? 'success' : 'failed', time: Time.now.utc}
|
2011-06-22 23:44:27 +00:00
|
|
|
return
|
2011-06-22 07:23:07 +00:00
|
|
|
end
|
|
|
|
|
2011-06-26 11:45:51 +00:00
|
|
|
def self.api_key
|
|
|
|
@@api_key
|
|
|
|
end
|
|
|
|
|
2011-09-17 11:38:49 +00:00
|
|
|
def self.api_secret
|
|
|
|
@@secret
|
|
|
|
end
|
2011-06-22 07:23:07 +00:00
|
|
|
end
|