oldhaven/app/controllers/last_fm_controller.rb

117 lines
4.0 KiB
Ruby
Raw Normal View History

2011-06-22 11:23:07 +04:00
require 'open-uri'
2011-09-17 23:00:08 +04:00
require 'net/http'
2011-06-22 11:23:07 +04:00
class LastFmController < ApplicationController
2011-11-29 23:42:21 +04:00
before_filter :authorize, except: [:connect, :autocomplete]
2011-06-22 11:23:07 +04:00
def connect
unless params[:sid].nil? or params[:token].nil?
2011-11-29 23:42:21 +04:00
if current_user?
lastfm_api_session = LastFM::Auth.get_session(token: params[:token], api_sig: true)
user = current_user
user.lastfm_key = lastfm_api_session["session"]['key']
user.lastfm_username = lastfm_api_session["session"]['name']
user.save
return render text: "<script>window.close();</script>"
2011-06-22 11:23:07 +04:00
else
2011-11-29 23:42:21 +04:00
return render text: "You Don't Fool Me", status: 403
2011-06-22 11:23:07 +04:00
end
else
2011-11-29 23:42:21 +04:00
return render text: "So Much Trouble In The World", status: 403
2011-06-22 11:23:07 +04:00
end
end
def getinfo
@res = {}
2011-06-23 02:56:32 +04:00
user = User.find_by_vkid(params[:mid])
unless user.lastfm_key.nil?
2011-11-29 23:42:21 +04:00
render json: {
connected: true,
username: user.lastfm_username
2011-06-23 02:56:32 +04:00
}
else
2011-11-29 23:42:21 +04:00
render json: {
connected: false,
lastfm_login_url: "http://www.last.fm/api/auth?api_key=#{LastFM.api_key}"+
"&cb=http://#{request.host}:#{request.port.to_s}/lastfm/connect/?sid=#{user.session.key}"
2011-06-23 02:56:32 +04:00
}
2011-06-22 11:23:07 +04:00
end
end
2011-06-23 02:56:32 +04:00
def listening
2011-06-23 03:44:27 +04:00
@res = {}
2011-09-17 23:00:08 +04:00
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
2011-11-29 23:42:21 +04:00
return render json: { status: 'bad params' }, status: 403
2011-06-23 03:44:27 +04:00
end
2011-11-29 23:42:21 +04:00
if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "")
return render json: {status: 'lastfm account is not connected' }
2011-06-23 03:44:27 +04:00
end
2011-09-17 23:00:08 +04:00
r = LastFM::Track.update_now_playing(
2011-11-29 23:42:21 +04:00
track: params[:name],
artist: params[:artist],
album: params[:album],
trackNumber: params[:position].gsub(/[a-z]/, "").to_i,
duration: params[:length].to_i,
sk: current_user.lastfm_key # Auth session key
2011-09-17 23:00:08 +04:00
)
2011-11-29 23:42:21 +04:00
render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403
2011-06-22 11:23:07 +04:00
end
2011-06-23 02:56:32 +04:00
def scrobble
2011-06-23 03:44:27 +04:00
@res = {}
2011-09-17 23:00:08 +04:00
if params[:artist].nil? or params[:album].nil? or params[:name].nil?
2011-11-29 23:42:21 +04:00
return render json: { status: 'bad params' }, status: 403
2011-06-23 03:44:27 +04:00
end
2011-11-29 23:42:21 +04:00
if current_user? and track = Track.find_by_id(params[:id])
Scrobble.create(user: current_user, track: track, scrobbled_at: DateTime.now)
end
if !current_user? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "")
return render json: { status: 'lastfm account is not connected' }
2011-06-23 03:44:27 +04:00
end
2011-09-17 23:00:08 +04:00
r = LastFM::Track.scrobble(
2011-11-29 23:42:21 +04:00
track: params[:name],
timestamp: Time.now.utc.to_i,
artist: params[:artist],
album: params[:album],
trackNumber: params[:position].gsub(/[a-z]/, "").to_i,
duration: params[:length].to_i,
sk: current_user.lastfm_key # Auth session key
2011-09-17 23:00:08 +04:00
)
2011-11-29 23:42:21 +04:00
render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403
2011-10-20 00:55:46 +04:00
end
def self.top_playlist artist
2011-11-27 00:56:09 +04:00
playlist = Playlist.create(name: "#{artist.name}: Last.fm TOP", artist: artist)
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-20 00:55:46 +04:00
def autocomplete
autocomplete = getSuggestions(params[:query])
2011-11-29 23:42:21 +04:00
return render nothing: true if autocomplete.nil?
2011-10-20 00:55:46 +04:00
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-11-29 23:42:21 +04:00
render json: {
query: params[:query],
suggestions: suggestions.take(5)
2011-10-20 00:55:46 +04:00
}
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 11:23:07 +04:00
end
end