1
0
Fork 0

Artist check within local, mb and lastfm databases

This commit is contained in:
magnolia-fan 2011-09-19 08:07:45 +04:00
parent dffc5bf735
commit 3cd6c111bd
6 changed files with 69 additions and 8 deletions

View File

@ -1,6 +1,6 @@
GIT
remote: http://github.com/magnolia-fan/lastfm-client.git
revision: fe5614565eebb5b0b019c103211bba3698848daa
revision: 26fec825943d0bb77606bf058386e786ee97572b
specs:
lastfm-client (0.0.2)
json (>= 1.4.6)
@ -59,7 +59,7 @@ GEM
haml (3.1.3)
hike (1.2.1)
i18n (0.6.0)
json (1.6.0)
json (1.6.1)
libv8 (3.3.10.2)
mail (2.3.0)
i18n (>= 0.4.0)
@ -67,7 +67,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.16)
multi_json (1.0.3)
musicbrainz (0.4.5)
musicbrainz (0.4.7)
nokogiri
nokogiri (1.5.0)
pg (0.11.0)

View File

@ -22,6 +22,13 @@ class ImportController < ApplicationController
ap e.message
ap e.backtrace
end
if brainz_artist.nil?
artist.mbid = nil
artist.status = 2
artist.save
return 2
end
begin
# Save artist

View File

@ -0,0 +1,5 @@
class AddApprovedFlagToArtist < ActiveRecord::Migration
def change
add_column :artists, :approved, :integer
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110915220228) do
ActiveRecord::Schema.define(:version => 20110919003651) do
create_table "albums", :force => true do |t|
t.string "name"
@ -36,6 +36,7 @@ ActiveRecord::Schema.define(:version => 20110915220228) do
t.string "mbid"
t.integer "status"
t.integer "listeners"
t.integer "approved"
end
create_table "delayed_jobs", :force => true do |t|

View File

@ -0,0 +1,47 @@
require 'cgi'
namespace :artist do
desc 'Find and fix uncorrect mbid'
task :correct => :environment do
Artist.where( :approved => nil ).each do |artist|
puts "Artist: "+artist.name
bh_albums = artist.albums.map{ |a| a.name.scan(/[a-zA-Z0-9]/).join() }
tmp = LastFM::Artist.get_top_albums( :artist => artist.name )['topalbums']
if tmp['album'].nil?
tmp['album'] = []
elsif tmp['album'].is_a? Hash
tmp['album'] = [tmp['album']]
end
lastfm_albums = tmp['album'].map do |a|
a['name'].scan(/[a-zA-Z0-9]/).join()
end
if (bh_albums & lastfm_albums).empty?
puts "Result: No intersection"
best_candidate = nil
best_intersection = 0
MusicBrainz::Artist.search(artist.name.gsub(/\!/, '')).each do |candidate|
candidate = MusicBrainz::Artist.find(candidate[:mbid])
candidate_albums = candidate.release_groups.map{ |a| a.title.scan(/[a-zA-Z0-9]/).join() }
if (candidate_albums & lastfm_albums).length > best_intersection
best_intersection = (candidate_albums & lastfm_albums).length
best_candidate = candidate.id
end
end
if best_candidate.nil?
puts "No better candidate found (now: "+artist.mbid.to_s+")\n\n"
artist.approved = 0
artist.save
else
puts "Better candidate found: "+best_candidate+" (now: "+artist.mbid.to_s+")\n\n"
artist.approved = 2
artist.save
end
else
artist.approved = 1
artist.save
puts "Result: OK\n\n"
end
sleep 1
end
end
end

View File

@ -6,14 +6,15 @@ namespace :db do
artist.albums.each do |album|
album.destroy
end
puts artist.name+(artist.mbid.nil? or artist.mbid.empty? ? '' : ' ('+artist.mbid+')')
puts 'Artist:'+artist.name
puts 'MBID: '+artist.mbid unless artist.mbid.nil?
res = ImportController.importArtist(artist.name)
if res == 1
puts 'OK'
puts 'Status: OK'
elsif res == 2
puts 'FAIL'
puts 'Status: FAILED'
elsif res == 3
puts 'SKIP'
puts 'Status: SKIP'
end
puts ''
end