From da003cad120af23c83c912c6570c7455c6eaf88b Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Wed, 21 Sep 2011 22:59:39 +0400 Subject: [PATCH] Work on import from discogs. Switching to mysql in local dev --- Gemfile | 2 +- Gemfile.lock | 8 ++++---- app/models/artist_alias.rb | 2 ++ app/models/track_artist.rb | 2 ++ config/database.yml | 9 +++++---- .../20110921182007_add_master_to_album.rb | 5 +++++ .../20110921182113_create_artist_aliases.rb | 10 ++++++++++ ...10921182355_add_original_name_to_artist.rb | 5 +++++ .../20110921182606_create_track_artists.rb | 12 +++++++++++ db/schema.rb | 20 ++++++++++++++++++- lib/discogs.rb | 12 ++++++++--- test/fixtures/artist_aliases.yml | 9 +++++++++ test/fixtures/track_artists.yml | 13 ++++++++++++ test/unit/artist_alias_test.rb | 7 +++++++ test/unit/track_artist_test.rb | 7 +++++++ 15 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 app/models/artist_alias.rb create mode 100644 app/models/track_artist.rb create mode 100644 db/migrate/20110921182007_add_master_to_album.rb create mode 100644 db/migrate/20110921182113_create_artist_aliases.rb create mode 100644 db/migrate/20110921182355_add_original_name_to_artist.rb create mode 100644 db/migrate/20110921182606_create_track_artists.rb create mode 100644 test/fixtures/artist_aliases.yml create mode 100644 test/fixtures/track_artists.yml create mode 100644 test/unit/artist_alias_test.rb create mode 100644 test/unit/track_artist_test.rb diff --git a/Gemfile b/Gemfile index 07cc83c..f10aadc 100644 --- a/Gemfile +++ b/Gemfile @@ -20,7 +20,7 @@ gem 'musicbrainz' gem 'bitmask_attributes' group :development do - gem 'sqlite3' + gem 'mysql2', '0.3.7' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 80af4b9..5ba961e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,7 +54,7 @@ GEM activesupport (~> 3.0) daemons erubis (2.7.0) - execjs (1.2.7) + execjs (1.2.8) multi_json (~> 1.0) haml (3.1.3) hike (1.2.1) @@ -67,8 +67,9 @@ GEM treetop (~> 1.4.8) mime-types (1.16) multi_json (1.0.3) - musicbrainz (0.4.9) + musicbrainz (0.4.10) nokogiri + mysql2 (0.3.7) nokogiri (1.5.0) pg (0.11.0) polyglot (0.3.2) @@ -103,7 +104,6 @@ GEM hike (~> 1.2) rack (~> 1.0) tilt (!= 1.3.0, ~> 1.1) - sqlite3 (1.3.4) therubyracer (0.9.4) libv8 (~> 3.3.10) thor (0.14.6) @@ -129,10 +129,10 @@ DEPENDENCIES json lastfm-client! musicbrainz + mysql2 (= 0.3.7) nokogiri pg rails (= 3.1.0) sass - sqlite3 therubyracer uglifier diff --git a/app/models/artist_alias.rb b/app/models/artist_alias.rb new file mode 100644 index 0000000..70aa571 --- /dev/null +++ b/app/models/artist_alias.rb @@ -0,0 +1,2 @@ +class ArtistAlias < ActiveRecord::Base +end diff --git a/app/models/track_artist.rb b/app/models/track_artist.rb new file mode 100644 index 0000000..168452f --- /dev/null +++ b/app/models/track_artist.rb @@ -0,0 +1,2 @@ +class TrackArtist < ActiveRecord::Base +end diff --git a/config/database.yml b/config/database.yml index fefc9b9..4a7c0b1 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,8 +1,9 @@ development: - adapter: sqlite3 - database: db/development-discogs.sqlite3 - pool: 5 - timeout: 5000 + adapter: mysql2 + database: beathaven + username: root + password: test + host: localhost test: adapter: sqlite3 diff --git a/db/migrate/20110921182007_add_master_to_album.rb b/db/migrate/20110921182007_add_master_to_album.rb new file mode 100644 index 0000000..a1b2b56 --- /dev/null +++ b/db/migrate/20110921182007_add_master_to_album.rb @@ -0,0 +1,5 @@ +class AddMasterToAlbum < ActiveRecord::Migration + def change + add_column :albums, :master, :boolean + end +end diff --git a/db/migrate/20110921182113_create_artist_aliases.rb b/db/migrate/20110921182113_create_artist_aliases.rb new file mode 100644 index 0000000..7fbf1ed --- /dev/null +++ b/db/migrate/20110921182113_create_artist_aliases.rb @@ -0,0 +1,10 @@ +class CreateArtistAliases < ActiveRecord::Migration + def change + create_table :artist_aliases do |t| + t.integer :artist_id + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20110921182355_add_original_name_to_artist.rb b/db/migrate/20110921182355_add_original_name_to_artist.rb new file mode 100644 index 0000000..d11c5c3 --- /dev/null +++ b/db/migrate/20110921182355_add_original_name_to_artist.rb @@ -0,0 +1,5 @@ +class AddOriginalNameToArtist < ActiveRecord::Migration + def change + add_column :artists, :original_name, :string + end +end diff --git a/db/migrate/20110921182606_create_track_artists.rb b/db/migrate/20110921182606_create_track_artists.rb new file mode 100644 index 0000000..0132e32 --- /dev/null +++ b/db/migrate/20110921182606_create_track_artists.rb @@ -0,0 +1,12 @@ +class CreateTrackArtists < ActiveRecord::Migration + def change + create_table :track_artists do |t| + t.integer :track_id + t.integer :artist_id + t.boolean :main + t.string :join + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0970e7c..9058f9d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110920164044) do +ActiveRecord::Schema.define(:version => 20110921182606) do create_table "albums", :force => true do |t| t.string "name" @@ -24,6 +24,14 @@ ActiveRecord::Schema.define(:version => 20110920164044) do t.string "mbid" t.integer "has_pic" t.integer "status" + t.boolean "master" + end + + create_table "artist_aliases", :force => true do |t| + t.integer "artist_id" + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" end create_table "artist_links", :force => true do |t| @@ -45,6 +53,7 @@ ActiveRecord::Schema.define(:version => 20110920164044) do t.integer "status" t.integer "listeners" t.integer "approved" + t.string "original_name" end create_table "delayed_jobs", :force => true do |t| @@ -114,6 +123,15 @@ ActiveRecord::Schema.define(:version => 20110920164044) do t.datetime "updated_at" end + create_table "track_artists", :force => true do |t| + t.integer "track_id" + t.integer "artist_id" + t.boolean "main" + t.string "join" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "tracks", :force => true do |t| t.string "name" t.integer "album_id" diff --git a/lib/discogs.rb b/lib/discogs.rb index 7aa41f8..b19ab3d 100644 --- a/lib/discogs.rb +++ b/lib/discogs.rb @@ -10,12 +10,18 @@ class Discogs end def self.process_artist node - Artist.new( + artist = Artist.new( :name => (node.css('name').first.text), :pic_url => (node.css('images > image[type="primary"]').first.text unless node.css('images > image[type="primary"]').empty?), :status => 1 - ).save - p node.css('name').first.text + ) + artist.save + node.css('namevariations > name, aliases > name').each do |v| + ArtistAlias.new( + :artist_id => artist.id, + :name => v.text + ).save + end end def self.releases diff --git a/test/fixtures/artist_aliases.yml b/test/fixtures/artist_aliases.yml new file mode 100644 index 0000000..db2ab8d --- /dev/null +++ b/test/fixtures/artist_aliases.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + artist_id: 1 + name: MyString + +two: + artist_id: 1 + name: MyString diff --git a/test/fixtures/track_artists.yml b/test/fixtures/track_artists.yml new file mode 100644 index 0000000..904fc0a --- /dev/null +++ b/test/fixtures/track_artists.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + track_id: 1 + artist_id: 1 + main: false + join: MyString + +two: + track_id: 1 + artist_id: 1 + main: false + join: MyString diff --git a/test/unit/artist_alias_test.rb b/test/unit/artist_alias_test.rb new file mode 100644 index 0000000..708266e --- /dev/null +++ b/test/unit/artist_alias_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ArtistAliasTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/track_artist_test.rb b/test/unit/track_artist_test.rb new file mode 100644 index 0000000..28599fe --- /dev/null +++ b/test/unit/track_artist_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TrackArtistTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end