1
0
Fork 0
oldhaven/app/controllers/last_fm_controller.rb

116 lines
3.8 KiB
Ruby
Raw Normal View History

2011-06-22 07:23:07 +00:00
require 'open-uri'
2011-09-17 19:00:08 +00:00
require 'net/http'
2011-06-22 07:23:07 +00:00
class LastFmController < ApplicationController
2011-10-19 20:55:46 +00:00
before_filter :authorize, :except => [:connect, :autocomplete]
2011-06-22 07:23:07 +00:00
def connect
unless params[:sid].nil? or params[:token].nil?
session = Session.find_by_key(params[:sid])
unless session.nil?
2011-09-17 19:00:08 +00:00
lastfm_api_session = LastFM::Auth.get_session(:token => params[:token], :api_sig => true)
session.user.lastfm_key = lastfm_api_session["session"]['key']
session.user.lastfm_username = lastfm_api_session["session"]['name']
2011-06-22 07:23:07 +00:00
session.user.save
render :text => '<script>window.close();</script>'
else
2011-10-19 20:55:46 +00:00
return render :text => 'You Don\'t Fool Me'
2011-06-22 07:23:07 +00:00
end
else
2011-10-19 20:55:46 +00:00
return render :text => 'So Much Trouble In The World'
2011-06-22 07:23:07 +00:00
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-10-19 20:55:46 +00:00
:lastfm_login_url => 'http://www.last.fm/api/auth?api_key='+ LastFM.api_key +
2011-06-23 05:39:31 +00:00
'&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-09-17 19:00:08 +00:00
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
2011-10-19 20:55:46 +00:00
return render :json => { :status => 'bad params' }
2011-06-22 23:44:27 +00:00
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?
2011-10-19 20:55:46 +00:00
return render :json => {:status => 'lastfm account is not connected', :user => user}
2011-06-22 23:44:27 +00:00
end
2011-09-17 19:00:08 +00:00
r = LastFM::Track.update_now_playing(
:track => params[:name],
:artist => params[:artist],
:album => params[:album],
2011-10-12 09:33:25 +00:00
:trackNumber => params[:position].to_i,
:duration => params[:length].to_i,
:sk => user.lastfm_key # Auth session key
2011-09-17 19:00:08 +00:00
)
2011-10-12 09:33:25 +00:00
render :json => { :status => r['error'].nil? ? 'success' : r }
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-09-17 19:00:08 +00:00
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
2011-10-19 20:55:46 +00:00
return render :json => { :status => 'bad params' }
2011-06-22 23:44:27 +00:00
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?
2011-10-19 20:55:46 +00:00
return render :json => { :status => 'lastfm account is not connected', :user => user }
2011-06-22 23:44:27 +00:00
end
2011-09-17 19:00:08 +00:00
r = LastFM::Track.scrobble(
:track => params[:name],
2011-09-17 19:00:08 +00:00
:timestamp => Time.now.utc.to_i,
:artist => params[:artist],
:album => params[:album],
2011-10-12 09:46:23 +00:00
:trackNumber => params[:position].to_i,
:duration => params[:length].to_i,
:sk => user.lastfm_key # Auth session key
2011-09-17 19:00:08 +00:00
)
2011-10-12 09:46:23 +00:00
render :json => { :status => r['error'].nil? ? 'success' : r }
2011-10-19 20:55:46 +00:00
end
def self.top_playlist artist
playlist = Playlist.create(name: "#{artist.name}: Last.fm TOP")
LastFM::Artist.get_top_tracks(artist: artist.name)["toptracks"]["track"].each do |track|
tracks = Track.joins(:album, :artists).where(name: track["name"], "track_artists.artist_id" => artist.id)
PlaylistItem.create(playlist_id: playlist.id, track_id: tracks.first.id) unless tracks.empty?
end
end
2011-10-19 20:55:46 +00:00
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
response.headers['Cache-Control'] = 'public, max-age='+1.week.seconds.to_s
2011-10-19 20:55:46 +00:00
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
2011-06-22 07:23:07 +00:00
end
end