From d9b75279f50d615c66c4a89ef8905ad447ca123b Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Sat, 17 Sep 2011 23:00:08 +0400 Subject: [PATCH] Lastfm workout --- Gemfile | 2 +- Gemfile.lock | 20 ++-------- app/assets/javascripts/player.coffee | 16 ++++---- app/controllers/artist_controller.rb | 3 ++ app/controllers/import_controller.rb | 20 ++++------ app/controllers/last_fm_controller.rb | 56 +++++++++++++++------------ app/controllers/user_controller.rb | 4 +- config/initializers/lastfm.rb | 3 ++ 8 files changed, 61 insertions(+), 63 deletions(-) create mode 100644 config/initializers/lastfm.rb diff --git a/Gemfile b/Gemfile index ebabdd8..17f4122 100644 --- a/Gemfile +++ b/Gemfile @@ -14,7 +14,7 @@ gem 'barista' gem 'awesome_print', :require => 'ap' gem 'delayed_job' -gem 'lastfm', :git => 'git://github.com/magnolia-fan/ruby-lastfm.git' +gem 'lastfm-client', '~> 0.0.2'#, :git => 'http://github.com/magnolia-fan/lastfm-client.git' gem 'musicbrainz', '~> 0.4.3' gem 'bitmask_attributes' diff --git a/Gemfile.lock b/Gemfile.lock index f56228b..33c5733 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,3 @@ -GIT - remote: git://github.com/magnolia-fan/ruby-lastfm.git - revision: 024064d52c9fb3996664436a339a4b2ec7541d9d - specs: - lastfm (0.2.0) - activesupport - httparty - xml-simple - GEM remote: http://rubygems.org/ specs: @@ -60,11 +51,10 @@ GEM multi_json (~> 1.0) haml (3.1.3) hike (1.2.1) - httparty (0.8.0) - multi_json - multi_xml i18n (0.6.0) json (1.6.0) + lastfm-client (0.0.2) + json (>= 1.4.6) libv8 (3.3.10.2) mail (2.3.0) i18n (>= 0.4.0) @@ -72,13 +62,12 @@ GEM treetop (~> 1.4.8) mime-types (1.16) multi_json (1.0.3) - multi_xml (0.4.0) musicbrainz (0.4.3) nokogiri nokogiri (1.5.0) pg (0.11.0) polyglot (0.3.2) - rack (1.3.2) + rack (1.3.3) rack-cache (1.0.3) rack (>= 0.4) rack-mount (0.8.3) @@ -121,7 +110,6 @@ GEM uglifier (1.0.3) execjs (>= 0.3.0) multi_json (>= 1.0.2) - xml-simple (1.1.0) PLATFORMS ruby @@ -134,7 +122,7 @@ DEPENDENCIES delayed_job haml json - lastfm! + lastfm-client (~> 0.0.2) musicbrainz (~> 0.4.3) nokogiri pg diff --git a/app/assets/javascripts/player.coffee b/app/assets/javascripts/player.coffee index d07aa63..a26002e 100644 --- a/app/assets/javascripts/player.coffee +++ b/app/assets/javascripts/player.coffee @@ -32,7 +32,7 @@ class window.Player data = e.jPlayer.status if not _player.scrobbled and data.currentPercentAbsolute > 50 $obj = $('.playlist li.now') - self.scrobble $obj.attr('data-artist'), $obj.attr('data-album'), $obj.attr('data-track') + self.scrobble _player.getTrackInfo $obj.attr('data-id') _player.scrobbled = true $('.player .progress .loaded').width(data.seekPercent * self.bar_width / 100) $('.player .progress .played').width(data.currentPercentAbsolute * self.bar_width / 100) @@ -82,15 +82,15 @@ class window.Player $('.player .now-playing').html query $('.playlist li').removeClass 'now' $obj.addClass 'now' - console.log(track) _vkontakte.loadTracksData track.artist, track.name, track.duration, (url) -> _player.playSource url - this.updateNowListening track.artist, track.album, track.name + this.updateNowListening track false getTrackInfo: (id) -> + console.log(id) for track in _player.playlist - if track.id == parseInt(id, 10) + if parseInt(track.id, 10) == parseInt(id, 10) return track false @@ -152,14 +152,14 @@ class window.Player onRepeat: -> #return $('#repeat').hasClass 'active' - updateNowListening: (artist, album, track) -> + updateNowListening: (track) -> if _session.getUser().lastfm_username - _session.query '/lastfm/listening?r=' +Math.random(), artist: artist, album: album, track: track + _session.query '/lastfm/listening?r=' +Math.random(), track false - scrobble: (artist, album, track) -> + scrobble: (track) -> if _session.getUser().lastfm_username - _session.query '/lastfm/scrobble?r=' +Math.random(), artist: artist, album: album, track: track + _session.query '/lastfm/scrobble?r=' +Math.random(), track false diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 13224f2..38130fe 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -89,7 +89,10 @@ class ArtistController < ApplicationController bonus_tracks = [] album.tracks.each do |track| tmp_track = {id: track.id, name: track.name, live: track.live, acoustic: track.acoustic} + tmp_track[:length] = (track.length / 1000).round tmp_track[:duration] = formatTrackDuration(track.length) + tmp_track[:position] = track.position + tmp_track[:mbid] = track.mbid (track.bonus == 0 ? album_tracks : bonus_tracks) << tmp_track end tmp_album[:tracks] = {album: album_tracks, bonus: bonus_tracks} diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index e3ebff5..9a49afa 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -1,37 +1,33 @@ -require 'lastfm' require 'musicbrainz' require 'nokogiri' class ImportController < ApplicationController def self.importArtist name, dry_run = false - # Initializing gems - lastfm = Lastfm.new(LastFmController.api_key, LastFmController.api_secret) - artist = Artist.find_or_create_by_name(name) return 3 if artist.status == 1 begin - lastfm_artist = lastfm.artist.get_info(name) + lastfm_artist = LastFM::Artist.get_info( :artist => name ) unless artist.mbid.nil? or artist.mbid.empty? brainz_artist = MusicBrainz::Artist.find(artist.mbid) else brainz_artist = MusicBrainz::Artist.find_by_name(name) end rescue => e - lastfm_artist = { + lastfm_artist = { 'artist' => { 'bio' => { 'summary' => '' }, 'image' => [ nil, nil, nil, { 'content' => '' } ], 'stats' => { 'listeners' => 0 } - } + } } ap e.message ap e.backtrace end begin # Save artist - artist.desc = lastfm_artist['bio']['summary'] - artist.pic_url = lastfm_artist['image'][3]['content'] - artist.listeners = lastfm_artist['stats']['listeners'] + artist.desc = lastfm_artist['artist']['bio']['summary'] + artist.pic_url = lastfm_artist['artist']['image'][3]['#text'] + artist.listeners = lastfm_artist['artist']['stats']['listeners'] artist.artist_type = brainz_artist.type artist.mbid = brainz_artist.id dry_run ? ap(artist) : artist.save @@ -39,8 +35,8 @@ class ImportController < ApplicationController brainz_artist.release_groups.each do |brainz_release_group| # Saving album begin - album_lastfm = lastfm.album.get_info(lastfm_artist['name'], brainz_release_group.title) - album_image = album_lastfm['image'][3]['content'] + album_lastfm = LastFM::Album.get_info( :artist => lastfm_artist['name'], :album => brainz_release_group.title ) + album_image = album_lastfm['album']['image'][3]['#text'] rescue album_image = '' end diff --git a/app/controllers/last_fm_controller.rb b/app/controllers/last_fm_controller.rb index 5f311c1..f75bbf8 100644 --- a/app/controllers/last_fm_controller.rb +++ b/app/controllers/last_fm_controller.rb @@ -1,5 +1,5 @@ require 'open-uri' -require 'lastfm' +require 'net/http' class LastFmController < ApplicationController before_filter :authorize @@ -10,10 +10,9 @@ class LastFmController < ApplicationController unless params[:sid].nil? or params[:token].nil? session = Session.find_by_key(params[:sid]) unless session.nil? - 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'] + 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'] session.user.save render :text => '' else @@ -45,8 +44,8 @@ class LastFmController < ApplicationController def listening @res = {} - if params[:artist].nil? or params[:album].nil? or params[:track].nil? - render :json => {:status => 'bad params'} + if params[:artist].nil? or params[:album].nil? or params[:name].nil? + render :json => { :status => 'bad params' } return end @@ -56,18 +55,26 @@ class LastFmController < ApplicationController return end - lastfm = Lastfm.new(@@api_key, @@secret) - lastfm.session = user.lastfm_key - r = lastfm.track.update_now_playing(params[:artist], params[:track]) + r = LastFM::Track.update_now_playing( + :track => params[:name].gsub(/\s/, '+').gsub(/\&/, '&'), + :artist => params[:artist].gsub(/\s/, '+').gsub(/\&/, '&'), + :album => params[:album].gsub(/\s/, '+').gsub(/\&/, '&'), + :trackNumber => params[:position], + :mbid => params[:mbid], + :duration => params[:length], + + :api_sig => true, + :sk => user.lastfm_key + ) - render :json => {:status => r ? 'success' : 'failed'} + render :json => { :status => r['error'].nil? ? 'success' : 'failed' } return end def scrobble @res = {} - if params[:artist].nil? or params[:album].nil? or params[:track].nil? + if params[:artist].nil? or params[:album].nil? or params[:name].nil? render :json => {:status => 'bad params'} return end @@ -78,19 +85,20 @@ class LastFmController < ApplicationController return end - lastfm = Lastfm.new(@@api_key, @@secret) - lastfm.session = user.lastfm_key - r = lastfm.track.scrobble(params[:artist], params[:track], params[:album], Time.now.utc.to_i) + r = LastFM::Track.scrobble( + :track => params[:name].gsub(/\s/, '+').gsub(/\&/, '&'), + :timestamp => Time.now.utc.to_i, + :artist => params[:artist].gsub(/\s/, '+').gsub(/\&/, '&'), + :album => params[:album].gsub(/\s/, '+').gsub(/\&/, '&'), + :trackNumber => params[:position], + :mbid => params[:mbid], + :duration => params[:length], + + :api_sig => true, + :sk => user.lastfm_key + ) - render :json => {:status => r ? 'success' : 'failed', time: Time.now.utc} + render :json => { :status => r['error'].nil? ? 'success' : 'failed' } return end - - def self.api_key - @@api_key - end - - def self.api_secret - @@secret - end end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 44a4b1c..547639a 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -27,7 +27,7 @@ class UserController < ApplicationController :vkid => user.vkid, :lang => user.lang, :lastfm_username => user.lastfm_username, - :lastfm_login_url => 'http://www.last.fm/api/auth?api_key=' << LastFmController.api_key << + :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 } @@ -66,7 +66,7 @@ class UserController < ApplicationController :vkid => user.vkid, :lang => user.lang, :lastfm_username => user.lastfm_username, - :lastfm_login_url => 'http://www.last.fm/api/auth?api_key=' << LastFmController.api_key << + :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 } diff --git a/config/initializers/lastfm.rb b/config/initializers/lastfm.rb new file mode 100644 index 0000000..13f2040 --- /dev/null +++ b/config/initializers/lastfm.rb @@ -0,0 +1,3 @@ +LastFM.api_key = '04fda005dbf61a50af5abc3e90f111f2' +LastFM.secret = '19e70e98b291e9f15d0516925945eb1b' +LastFM.client_name = 'BeatHaven' \ No newline at end of file