From dbdff13cd0edc5a32b66ad0d62a1b8b41d4c7427 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 22 Sep 2011 14:36:10 +0400 Subject: [PATCH 1/2] poo --- Gemfile.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 45aee7c..0d969aa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,7 +54,7 @@ GEM activesupport (~> 3.0) daemons erubis (2.7.0) - execjs (1.2.6) + execjs (1.2.8) multi_json (~> 1.0) haml (3.1.3) hike (1.2.1) @@ -67,7 +67,7 @@ GEM treetop (~> 1.4.8) mime-types (1.16) multi_json (1.0.3) - musicbrainz (0.4.7) + musicbrainz (0.4.10) nokogiri nokogiri (1.5.0) pg (0.11.0) From 3ea944fbb0ce145c53f6145e970a52f432ec49b6 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 22 Sep 2011 14:37:53 +0400 Subject: [PATCH 2/2] Merge --- app/controllers/import_controller.rb | 32 +++++++++++++++++-- app/models/album.rb | 1 + app/models/artist.rb | 1 + app/models/artist_link.rb | 3 ++ app/models/local_brainz.rb | 5 +++ app/models/local_brainz/release.rb | 4 +++ app/models/local_brainz/track.rb | 3 ++ ...0920152432_create_local_brainz_releases.rb | 13 ++++++++ ...110920152641_create_local_brainz_tracks.rb | 12 +++++++ .../20110920154338_create_artist_links.rb | 11 +++++++ ...1_add_artist_id_to_local_brainz_release.rb | 5 +++ ...3133_add_format_to_local_brainz_release.rb | 5 +++ ...57_add_release_id_to_local_brainz_track.rb | 5 +++ ...4_rename_field_in_local_brainz_releases.rb | 9 ++++++ db/schema.rb | 32 ++++++++++++++++++- lib/tasks/artist_wipe.rake | 7 ++++ lib/tasks/lastfm_import.rake | 4 +-- test/fixtures/artist_links.yml | 11 +++++++ test/fixtures/local_brainz/releases.yml | 15 +++++++++ test/fixtures/local_brainz/tracks.yml | 13 ++++++++ test/unit/artist_link_test.rb | 7 ++++ test/unit/local_brainz/release_test.rb | 7 ++++ test/unit/local_brainz/track_test.rb | 7 ++++ 23 files changed, 206 insertions(+), 6 deletions(-) create mode 100644 app/models/artist_link.rb create mode 100644 app/models/local_brainz.rb create mode 100644 app/models/local_brainz/release.rb create mode 100644 app/models/local_brainz/track.rb create mode 100644 db/migrate/20110920152432_create_local_brainz_releases.rb create mode 100644 db/migrate/20110920152641_create_local_brainz_tracks.rb create mode 100644 db/migrate/20110920154338_create_artist_links.rb create mode 100644 db/migrate/20110920160431_add_artist_id_to_local_brainz_release.rb create mode 100644 db/migrate/20110920163133_add_format_to_local_brainz_release.rb create mode 100644 db/migrate/20110920163957_add_release_id_to_local_brainz_track.rb create mode 100644 db/migrate/20110920164044_rename_field_in_local_brainz_releases.rb create mode 100644 lib/tasks/artist_wipe.rake create mode 100644 test/fixtures/artist_links.yml create mode 100644 test/fixtures/local_brainz/releases.yml create mode 100644 test/fixtures/local_brainz/tracks.yml create mode 100644 test/unit/artist_link_test.rb create mode 100644 test/unit/local_brainz/release_test.rb create mode 100644 test/unit/local_brainz/track_test.rb diff --git a/app/controllers/import_controller.rb b/app/controllers/import_controller.rb index 4a7743e..8125fee 100644 --- a/app/controllers/import_controller.rb +++ b/app/controllers/import_controller.rb @@ -37,7 +37,15 @@ class ImportController < ApplicationController artist.listeners = lastfm_artist['artist']['stats']['listeners'] artist.artist_type = brainz_artist.type artist.mbid = brainz_artist.id - dry_run ? ap(artist) : artist.save + artist.save + + brainz_artist.urls.each do |service, url| + ArtistLink.new( + :artist_id => artist.id, + :service => service.to_s, + :url => url + ).save + end brainz_artist.release_groups.each do |brainz_release_group| # Saving album @@ -56,12 +64,30 @@ class ImportController < ApplicationController album.pic_url = album_image album.has_pic = (album_image != '' and not album_image.nil?) album.status = 0 - dry_run ? ap(album) : album.save + album.save # Tracks from the first release tracks_hashed = [] brainz_release_group.releases.each_with_index do |brainz_release, i| + local_brainz_release = LocalBrainz::Release.new( + :mbid => brainz_release.id, + :title => brainz_release.title, + :status => brainz_release.status, + :date => brainz_release.date, + :country => brainz_release.country, + :format => brainz_release.format, + :album_id => album.id + ) + local_brainz_release.save # Processing tracks brainz_release.tracks.each do |brainz_track| + local_brainz_track = LocalBrainz::Track.new( + :position => brainz_track.position, + :recording_id => brainz_track.recording_id, + :title => brainz_track.title, + :length => brainz_track.length, + :release_id => local_brainz_release.id + ) + local_brainz_track.save track_title = brainz_track.title.gsub(/\s\/\s\[.*?\]/, '') if tracks_hashed.include? track_title.downcase.scan(/[a-z0-9]*/).join next @@ -77,7 +103,7 @@ class ImportController < ApplicationController track.live = track_title.downcase.include? 'live' track.acoustic = track_title.downcase.include? 'acoustic' track.mbid = brainz_track.recording_id - dry_run ? ap(track) : track.save + track.save end end album.status = 1 diff --git a/app/models/album.rb b/app/models/album.rb index 2f1fae9..42b8c24 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -1,4 +1,5 @@ class Album < ActiveRecord::Base belongs_to :artist has_many :tracks, :order => 'bonus ASC, position ASC', :dependent => :destroy + has_many :local_brainz_releases, :class_name => 'LocalBrainz::Release', :dependent => :destroy end diff --git a/app/models/artist.rb b/app/models/artist.rb index 8211a70..b370b18 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -1,3 +1,4 @@ class Artist < ActiveRecord::Base has_many :albums, :order => 'has_pic DESC, year ASC, id ASC', :dependent => :destroy + has_many :artist_links, :dependent => :destroy end diff --git a/app/models/artist_link.rb b/app/models/artist_link.rb new file mode 100644 index 0000000..6fa724c --- /dev/null +++ b/app/models/artist_link.rb @@ -0,0 +1,3 @@ +class ArtistLink < ActiveRecord::Base + belongs_to :artist +end diff --git a/app/models/local_brainz.rb b/app/models/local_brainz.rb new file mode 100644 index 0000000..5188f5f --- /dev/null +++ b/app/models/local_brainz.rb @@ -0,0 +1,5 @@ +module LocalBrainz + def self.table_name_prefix + 'local_brainz_' + end +end diff --git a/app/models/local_brainz/release.rb b/app/models/local_brainz/release.rb new file mode 100644 index 0000000..b7aa0be --- /dev/null +++ b/app/models/local_brainz/release.rb @@ -0,0 +1,4 @@ +class LocalBrainz::Release < ActiveRecord::Base + belongs_to :album, :class_name => 'Album' + has_many :local_brainz_tracks, :class_name => 'LocalBrainz::Track', :dependent => :destroy +end diff --git a/app/models/local_brainz/track.rb b/app/models/local_brainz/track.rb new file mode 100644 index 0000000..b835673 --- /dev/null +++ b/app/models/local_brainz/track.rb @@ -0,0 +1,3 @@ +class LocalBrainz::Track < ActiveRecord::Base + belongs_to :local_brainz_release, :class_name => 'LocalBrainz::Release' +end diff --git a/db/migrate/20110920152432_create_local_brainz_releases.rb b/db/migrate/20110920152432_create_local_brainz_releases.rb new file mode 100644 index 0000000..ada7e89 --- /dev/null +++ b/db/migrate/20110920152432_create_local_brainz_releases.rb @@ -0,0 +1,13 @@ +class CreateLocalBrainzReleases < ActiveRecord::Migration + def change + create_table :local_brainz_releases do |t| + t.string :mbid + t.string :title + t.string :status + t.date :date + t.string :country + + t.timestamps + end + end +end diff --git a/db/migrate/20110920152641_create_local_brainz_tracks.rb b/db/migrate/20110920152641_create_local_brainz_tracks.rb new file mode 100644 index 0000000..c847a38 --- /dev/null +++ b/db/migrate/20110920152641_create_local_brainz_tracks.rb @@ -0,0 +1,12 @@ +class CreateLocalBrainzTracks < ActiveRecord::Migration + def change + create_table :local_brainz_tracks do |t| + t.integer :position + t.string :recording_id + t.string :title + t.integer :length + + t.timestamps + end + end +end diff --git a/db/migrate/20110920154338_create_artist_links.rb b/db/migrate/20110920154338_create_artist_links.rb new file mode 100644 index 0000000..6eeb87f --- /dev/null +++ b/db/migrate/20110920154338_create_artist_links.rb @@ -0,0 +1,11 @@ +class CreateArtistLinks < ActiveRecord::Migration + def change + create_table :artist_links do |t| + t.integer :artist_id + t.string :service + t.string :url + + t.timestamps + end + end +end diff --git a/db/migrate/20110920160431_add_artist_id_to_local_brainz_release.rb b/db/migrate/20110920160431_add_artist_id_to_local_brainz_release.rb new file mode 100644 index 0000000..b638c9e --- /dev/null +++ b/db/migrate/20110920160431_add_artist_id_to_local_brainz_release.rb @@ -0,0 +1,5 @@ +class AddArtistIdToLocalBrainzRelease < ActiveRecord::Migration + def change + add_column :local_brainz_releases, :artist_id, :integer + end +end diff --git a/db/migrate/20110920163133_add_format_to_local_brainz_release.rb b/db/migrate/20110920163133_add_format_to_local_brainz_release.rb new file mode 100644 index 0000000..e765bb7 --- /dev/null +++ b/db/migrate/20110920163133_add_format_to_local_brainz_release.rb @@ -0,0 +1,5 @@ +class AddFormatToLocalBrainzRelease < ActiveRecord::Migration + def change + add_column :local_brainz_releases, :format, :string + end +end diff --git a/db/migrate/20110920163957_add_release_id_to_local_brainz_track.rb b/db/migrate/20110920163957_add_release_id_to_local_brainz_track.rb new file mode 100644 index 0000000..c67b505 --- /dev/null +++ b/db/migrate/20110920163957_add_release_id_to_local_brainz_track.rb @@ -0,0 +1,5 @@ +class AddReleaseIdToLocalBrainzTrack < ActiveRecord::Migration + def change + add_column :local_brainz_tracks, :release_id, :integer + end +end diff --git a/db/migrate/20110920164044_rename_field_in_local_brainz_releases.rb b/db/migrate/20110920164044_rename_field_in_local_brainz_releases.rb new file mode 100644 index 0000000..4578fdd --- /dev/null +++ b/db/migrate/20110920164044_rename_field_in_local_brainz_releases.rb @@ -0,0 +1,9 @@ +class RenameFieldInLocalBrainzReleases < ActiveRecord::Migration + def up + rename_column :local_brainz_releases, :artist_id, :album_id + end + + def down + rename_column :local_brainz_releases, :album_id, :artist_id + end +end diff --git a/db/schema.rb b/db/schema.rb index f4e0a03..0970e7c 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 => 20110919003651) do +ActiveRecord::Schema.define(:version => 20110920164044) do create_table "albums", :force => true do |t| t.string "name" @@ -26,6 +26,14 @@ ActiveRecord::Schema.define(:version => 20110919003651) do t.integer "status" end + create_table "artist_links", :force => true do |t| + t.integer "artist_id" + t.string "service" + t.string "url" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "artists", :force => true do |t| t.string "name" t.text "desc" @@ -63,6 +71,28 @@ ActiveRecord::Schema.define(:version => 20110919003651) do t.datetime "updated_at" end + create_table "local_brainz_releases", :force => true do |t| + t.string "mbid" + t.string "title" + t.string "status" + t.date "date" + t.string "country" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "album_id" + t.string "format" + end + + create_table "local_brainz_tracks", :force => true do |t| + t.integer "position" + t.string "recording_id" + t.string "title" + t.integer "length" + t.datetime "created_at" + t.datetime "updated_at" + t.integer "release_id" + end + create_table "playlist_items", :force => true do |t| t.integer "playlist_id" t.integer "track_id" diff --git a/lib/tasks/artist_wipe.rake b/lib/tasks/artist_wipe.rake new file mode 100644 index 0000000..fde4488 --- /dev/null +++ b/lib/tasks/artist_wipe.rake @@ -0,0 +1,7 @@ +namespace :artist do + desc 'Empty artist database' + task :wipe => :environment do + Album.destroy_all + ArtistLink.destroy_all + end +end diff --git a/lib/tasks/lastfm_import.rake b/lib/tasks/lastfm_import.rake index 47aac24..d380285 100644 --- a/lib/tasks/lastfm_import.rake +++ b/lib/tasks/lastfm_import.rake @@ -5,7 +5,7 @@ namespace :lastfm do ImportController.parseLastfmXML('tmp/data/top1000.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.mbid = artist[:mbid] record.save res = ImportController.importArtist(artist[:name]) if res == 1 @@ -22,7 +22,7 @@ namespace :lastfm 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.mbid = artist[:mbid] record.save res = ImportController.importArtist(artist[:name]) if res == 1 diff --git a/test/fixtures/artist_links.yml b/test/fixtures/artist_links.yml new file mode 100644 index 0000000..47abe0e --- /dev/null +++ b/test/fixtures/artist_links.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + artist_id: 1 + service: MyString + url: MyString + +two: + artist_id: 1 + service: MyString + url: MyString diff --git a/test/fixtures/local_brainz/releases.yml b/test/fixtures/local_brainz/releases.yml new file mode 100644 index 0000000..2b2bfe9 --- /dev/null +++ b/test/fixtures/local_brainz/releases.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + mbid: MyString + title: MyString + status: MyString + date: 2011-09-20 + country: MyString + +two: + mbid: MyString + title: MyString + status: MyString + date: 2011-09-20 + country: MyString diff --git a/test/fixtures/local_brainz/tracks.yml b/test/fixtures/local_brainz/tracks.yml new file mode 100644 index 0000000..f2023f5 --- /dev/null +++ b/test/fixtures/local_brainz/tracks.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + position: 1 + recording_id: MyString + title: MyString + length: 1 + +two: + position: 1 + recording_id: MyString + title: MyString + length: 1 diff --git a/test/unit/artist_link_test.rb b/test/unit/artist_link_test.rb new file mode 100644 index 0000000..221d114 --- /dev/null +++ b/test/unit/artist_link_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ArtistLinkTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/local_brainz/release_test.rb b/test/unit/local_brainz/release_test.rb new file mode 100644 index 0000000..e8d5df5 --- /dev/null +++ b/test/unit/local_brainz/release_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LocalBrainz::ReleaseTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/local_brainz/track_test.rb b/test/unit/local_brainz/track_test.rb new file mode 100644 index 0000000..d7ce795 --- /dev/null +++ b/test/unit/local_brainz/track_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LocalBrainz::TrackTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end