require 'open-uri' require 'net/http' class LastFmController < ApplicationController before_filter :authorize, except: [:connect, :autocomplete] def connect unless params[:sid].nil? or params[:token].nil? if user_present? 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: "" else return render text: "You Don't Fool Me", status: 403 end else return render text: "So Much Trouble In The World", status: 403 end end def getinfo @res = {} unless current_user.lastfm_key.nil? render json: { connected: true, username: current_user.lastfm_username } else 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}" } end end def listening @res = {} if params[:artist].nil? or params[:album].nil? or params[:name].nil? return render json: { status: 'bad params' }, status: 403 end if !user_present? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "") return render json: {status: 'lastfm account is not connected' } end r = LastFM::Track.update_now_playing( 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 ) render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403 end def scrobble @res = {} if params[:artist].nil? or params[:album].nil? or params[:name].nil? return render json: { status: 'bad params' }, status: 403 end if user_present? and track = Track.find_by_id(params[:id]) Scrobble.create(user: current_user, track: track, scrobbled_at: DateTime.now) end if !user_present? or (current_user.lastfm_key.nil? or current_user.lastfm_key == "") return render json: { status: 'lastfm account is not connected' } end r = LastFM::Track.scrobble( 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 ) render json: { status: r['error'].nil? ? 'success' : r }, status: r['error'].nil? ? 200 : 403 end def self.top_playlist artist 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 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 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