diff --git a/app/models/genre.rb b/app/models/genre.rb new file mode 100644 index 0000000..69d40fb --- /dev/null +++ b/app/models/genre.rb @@ -0,0 +1,2 @@ +class Genre < ActiveRecord::Base +end diff --git a/app/models/style.rb b/app/models/style.rb new file mode 100644 index 0000000..2ff0f84 --- /dev/null +++ b/app/models/style.rb @@ -0,0 +1,2 @@ +class Style < ActiveRecord::Base +end diff --git a/app/models/track.rb b/app/models/track.rb index 61dd73f..6819b0a 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -1,4 +1,4 @@ class Track < ActiveRecord::Base belongs_to :album - belongs_to :artist, :through => :track_artists + has_many :artists, :through => :track_artists end diff --git a/db/migrate/20110922122326_create_genres.rb b/db/migrate/20110922122326_create_genres.rb new file mode 100644 index 0000000..3664daf --- /dev/null +++ b/db/migrate/20110922122326_create_genres.rb @@ -0,0 +1,9 @@ +class CreateGenres < ActiveRecord::Migration + def change + create_table :genres do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20110922122407_create_styles.rb b/db/migrate/20110922122407_create_styles.rb new file mode 100644 index 0000000..257f42c --- /dev/null +++ b/db/migrate/20110922122407_create_styles.rb @@ -0,0 +1,9 @@ +class CreateStyles < ActiveRecord::Migration + def change + create_table :styles do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9058f9d..f58feff 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 => 20110921182606) do +ActiveRecord::Schema.define(:version => 20110922122407) do create_table "albums", :force => true do |t| t.string "name" @@ -80,6 +80,12 @@ ActiveRecord::Schema.define(:version => 20110921182606) do t.datetime "updated_at" end + create_table "genres", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "local_brainz_releases", :force => true do |t| t.string "mbid" t.string "title" @@ -123,6 +129,12 @@ ActiveRecord::Schema.define(:version => 20110921182606) do t.datetime "updated_at" end + create_table "styles", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "track_artists", :force => true do |t| t.integer "track_id" t.integer "artist_id" diff --git a/lib/discogs.rb b/lib/discogs.rb index 0e442fd..50baafa 100644 --- a/lib/discogs.rb +++ b/lib/discogs.rb @@ -1,7 +1,7 @@ class Discogs def self.artists - self.get_nodes('tmp/data/discogs_artists.xml', 'artist') do + self.get_nodes('tmp/data/discogs_artists.xml', 'artist') do |node| artist = Artist.new( :name => (node.css('name').first.text), :pic_url => (node.css('images > image[type="primary"]').first.attr('uri') unless node.css('images > image[type="primary"]').empty?), @@ -19,10 +19,44 @@ class Discogs def self.releases self.get_nodes('tmp/data/discogs_releases_test.xml', 'release') do |node| - r = { - :pic => (node.css('images > image[type="primary"]').first.attr('uri') unless node.css('images > image[type="primary"]').empty?) + + release = { + :pic => (node.css('images > image[type="primary"]').first.attr('uri') unless node.css('images > image[type="primary"]').empty?), + :main_artist => (node.css('artists > artist > name').first.text unless node.css('artists > artist > name').empty?), + :master => (not node.css('master_id').empty?), + :name => (node.css('title').first.text unless node.css('title').empty?), + :country => (node.css('country').first.text unless node.css('country').empty?), + :year => (node.css('released').first.text.split('-').first.to_i unless node.css('released').empty?), + + :genres => [], + :styles => [], + :tracks => [], } - ap r + + + unless node.css('genres > genre').empty? + node.css('genres > genre').each do |genre| + release[:genres] << Genre.find_or_create_by_name(genre.text) + end + end + + unless node.css('styles > style').empty? + node.css('styles > style').each do |style| + release[:styles] << Style.find_or_create_by_name(style.text) + end + end + + unless node.css('tracklist > track').empty? + node.css('tracklist > track').each do |track| + release[:tracks] << Track.new( + :name => (track.css('title').first.text unless track.css('title').empty?), + :position => (track.css('position').first.text.to_i(36) unless track.css('position').empty?), + :length => (self.duration_to_length(track.css('duration').first.text) unless track.css('duration').empty?) + ) + end + end + + ap release end end @@ -38,4 +72,9 @@ class Discogs end end + def self.duration_to_length duration + duration = duration.split(':') + duration[0].to_i * 60 + duration[1].to_i + end + end \ No newline at end of file diff --git a/test/fixtures/genres.yml b/test/fixtures/genres.yml new file mode 100644 index 0000000..20991d2 --- /dev/null +++ b/test/fixtures/genres.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/fixtures/styles.yml b/test/fixtures/styles.yml new file mode 100644 index 0000000..20991d2 --- /dev/null +++ b/test/fixtures/styles.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/unit/genre_test.rb b/test/unit/genre_test.rb new file mode 100644 index 0000000..95ccc5c --- /dev/null +++ b/test/unit/genre_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class GenreTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/style_test.rb b/test/unit/style_test.rb new file mode 100644 index 0000000..e79925b --- /dev/null +++ b/test/unit/style_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class StyleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end