From e69a12ded52d62bf1ca40a78a00ccbb2b6169da9 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Sun, 18 Sep 2011 18:25:47 +0400 Subject: [PATCH] Finally fixed lastfm post requests, couple of rake tasks --- Gemfile | 2 +- Gemfile.lock | 13 +++++++++---- app/controllers/artist_controller.rb | 2 +- app/controllers/import_controller.rb | 4 ++-- app/controllers/last_fm_controller.rb | 20 ++++++++------------ lib/tasks/db_artist.rake | 22 ++++++++++++++++++++++ lib/tasks/lastfm_import.rake | 17 +++++++++++++++++ 7 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 lib/tasks/db_artist.rake diff --git a/Gemfile b/Gemfile index 17f4122..6863a70 100644 --- a/Gemfile +++ b/Gemfile @@ -14,7 +14,7 @@ gem 'barista' gem 'awesome_print', :require => 'ap' gem 'delayed_job' -gem 'lastfm-client', '~> 0.0.2'#, :git => 'http://github.com/magnolia-fan/lastfm-client.git' +gem 'lastfm-client', :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 33c5733..ce74805 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: http://github.com/magnolia-fan/lastfm-client.git + revision: fe5614565eebb5b0b019c103211bba3698848daa + specs: + lastfm-client (0.0.2) + json (>= 1.4.6) + GEM remote: http://rubygems.org/ specs: @@ -53,8 +60,6 @@ GEM hike (1.2.1) 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) @@ -62,7 +67,7 @@ GEM treetop (~> 1.4.8) mime-types (1.16) multi_json (1.0.3) - musicbrainz (0.4.3) + musicbrainz (0.4.5) nokogiri nokogiri (1.5.0) pg (0.11.0) @@ -122,7 +127,7 @@ DEPENDENCIES delayed_job haml json - lastfm-client (~> 0.0.2) + lastfm-client! musicbrainz (~> 0.4.3) nokogiri pg diff --git a/app/controllers/artist_controller.rb b/app/controllers/artist_controller.rb index 38130fe..8ce98df 100644 --- a/app/controllers/artist_controller.rb +++ b/app/controllers/artist_controller.rb @@ -89,7 +89,7 @@ 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[:length] = (track.length / 1000).round unless track.length.nil? tmp_track[:duration] = formatTrackDuration(track.length) tmp_track[:position] = track.position tmp_track[:mbid] = track.mbid diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index 9a49afa..9dd633a 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -16,7 +16,7 @@ class ImportController < ApplicationController rescue => e lastfm_artist = { 'artist' => { 'bio' => { 'summary' => '' }, - 'image' => [ nil, nil, nil, { 'content' => '' } ], + 'image' => [ nil, nil, nil, { '#text' => '' } ], 'stats' => { 'listeners' => 0 } } } ap e.message @@ -35,7 +35,7 @@ class ImportController < ApplicationController brainz_artist.release_groups.each do |brainz_release_group| # Saving album begin - album_lastfm = LastFM::Album.get_info( :artist => lastfm_artist['name'], :album => brainz_release_group.title ) + album_lastfm = LastFM::Album.get_info( :artist => lastfm_artist['artist']['name'], :album => brainz_release_group.title ) album_image = album_lastfm['album']['image'][3]['#text'] rescue album_image = '' diff --git a/app/controllers/last_fm_controller.rb b/app/controllers/last_fm_controller.rb index f75bbf8..9ed4d6a 100644 --- a/app/controllers/last_fm_controller.rb +++ b/app/controllers/last_fm_controller.rb @@ -56,15 +56,13 @@ class LastFmController < ApplicationController end r = LastFM::Track.update_now_playing( - :track => params[:name].gsub(/\s/, '+').gsub(/\&/, '&'), - :artist => params[:artist].gsub(/\s/, '+').gsub(/\&/, '&'), - :album => params[:album].gsub(/\s/, '+').gsub(/\&/, '&'), + :track => params[:name], + :artist => params[:artist], + :album => params[:album], :trackNumber => params[:position], :mbid => params[:mbid], :duration => params[:length], - - :api_sig => true, - :sk => user.lastfm_key + :sk => user.lastfm_key # Auth session key ) render :json => { :status => r['error'].nil? ? 'success' : 'failed' } @@ -86,16 +84,14 @@ class LastFmController < ApplicationController end r = LastFM::Track.scrobble( - :track => params[:name].gsub(/\s/, '+').gsub(/\&/, '&'), + :track => params[:name], :timestamp => Time.now.utc.to_i, - :artist => params[:artist].gsub(/\s/, '+').gsub(/\&/, '&'), - :album => params[:album].gsub(/\s/, '+').gsub(/\&/, '&'), + :artist => params[:artist], + :album => params[:album], :trackNumber => params[:position], :mbid => params[:mbid], :duration => params[:length], - - :api_sig => true, - :sk => user.lastfm_key + :sk => user.lastfm_key # Auth session key ) render :json => { :status => r['error'].nil? ? 'success' : 'failed' } diff --git a/lib/tasks/db_artist.rake b/lib/tasks/db_artist.rake new file mode 100644 index 0000000..aa3b68b --- /dev/null +++ b/lib/tasks/db_artist.rake @@ -0,0 +1,22 @@ +namespace :db do + namespace :artist do + desc 'Fix broken imports' + task :fix => :environment do + Artist.where(:status => [0, 2]).each do |artist| + artist.albums.each do |album| + album.destroy + end + puts artist.name+(artist.mbid.nil? or artist.mbid.empty? ? '' : ' ('+artist.mbid+')') + res = ImportController.importArtist(artist.name) + if res == 1 + puts 'OK' + elsif res == 2 + puts 'FAIL' + elsif res == 3 + puts 'SKIP' + end + puts '' + end + end + end +end \ No newline at end of file diff --git a/lib/tasks/lastfm_import.rake b/lib/tasks/lastfm_import.rake index 98b71e1..47aac24 100644 --- a/lib/tasks/lastfm_import.rake +++ b/lib/tasks/lastfm_import.rake @@ -18,5 +18,22 @@ namespace :lastfm do puts '' end end + task :hyped => :environment do + ImportController.parseLastfmXML('tmp/data/hyped500.xml').each do |artist| + puts artist[:name] + (artist[:mbid].empty? ? '' : ' (' + artist[:mbid] + ')') + record = Artist.find_or_create_by_name(artist[:name]) + record.mbid = artist[:mbid] + record.save + res = ImportController.importArtist(artist[:name]) + if res == 1 + puts 'OK' + elsif res == 2 + puts 'FAIL' + elsif res == 3 + puts 'SKIP' + end + puts '' + end + end end end \ No newline at end of file