diff --git a/app/models/album_format.rb b/app/models/album_format.rb new file mode 100644 index 0000000..6dacf18 --- /dev/null +++ b/app/models/album_format.rb @@ -0,0 +1,4 @@ +class AlbumFormat < ActiveRecord::Base + belongs_to :album + belongs_to :format +end diff --git a/app/models/album_genre.rb b/app/models/album_genre.rb new file mode 100644 index 0000000..12b91ef --- /dev/null +++ b/app/models/album_genre.rb @@ -0,0 +1,4 @@ +class AlbumGenre < ActiveRecord::Base + belongs_to :album + belongs_to :genre +end diff --git a/app/models/album_style.rb b/app/models/album_style.rb new file mode 100644 index 0000000..df0a13e --- /dev/null +++ b/app/models/album_style.rb @@ -0,0 +1,4 @@ +class AlbumStyle < ActiveRecord::Base + belongs_to :album + belongs_to :style +end diff --git a/app/models/release_format.rb b/app/models/release_format.rb new file mode 100644 index 0000000..f4db089 --- /dev/null +++ b/app/models/release_format.rb @@ -0,0 +1,2 @@ +class ReleaseFormat < ActiveRecord::Base +end diff --git a/db/migrate/20110922185550_create_release_formats.rb b/db/migrate/20110922185550_create_release_formats.rb new file mode 100644 index 0000000..d0cd2d5 --- /dev/null +++ b/db/migrate/20110922185550_create_release_formats.rb @@ -0,0 +1,10 @@ +class CreateReleaseFormats < ActiveRecord::Migration + def change + create_table :release_formats do |t| + t.string :name + t.string :hash + + t.timestamps + end + end +end diff --git a/db/migrate/20110922185632_create_album_formats.rb b/db/migrate/20110922185632_create_album_formats.rb new file mode 100644 index 0000000..e793dd3 --- /dev/null +++ b/db/migrate/20110922185632_create_album_formats.rb @@ -0,0 +1,10 @@ +class CreateAlbumFormats < ActiveRecord::Migration + def change + create_table :album_formats do |t| + t.integer :album_id + t.integer :release_format_id + + t.timestamps + end + end +end diff --git a/db/migrate/20110922190724_create_album_genres.rb b/db/migrate/20110922190724_create_album_genres.rb new file mode 100644 index 0000000..62276e7 --- /dev/null +++ b/db/migrate/20110922190724_create_album_genres.rb @@ -0,0 +1,10 @@ +class CreateAlbumGenres < ActiveRecord::Migration + def change + create_table :album_genres do |t| + t.integer :album_id + t.integer :genre_id + + t.timestamps + end + end +end diff --git a/db/migrate/20110922190755_create_album_styles.rb b/db/migrate/20110922190755_create_album_styles.rb new file mode 100644 index 0000000..b97fe18 --- /dev/null +++ b/db/migrate/20110922190755_create_album_styles.rb @@ -0,0 +1,10 @@ +class CreateAlbumStyles < ActiveRecord::Migration + def change + create_table :album_styles do |t| + t.integer :album_id + t.integer :style_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index f58feff..e4e9ba2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,28 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110922122407) do +ActiveRecord::Schema.define(:version => 20110922190755) do + + create_table "album_formats", :force => true do |t| + t.integer "album_id" + t.integer "release_format_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "album_genres", :force => true do |t| + t.integer "album_id" + t.integer "genre_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "album_styles", :force => true do |t| + t.integer "album_id" + t.integer "style_id" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "albums", :force => true do |t| t.string "name" @@ -122,6 +143,13 @@ ActiveRecord::Schema.define(:version => 20110922122407) do t.datetime "updated_at" end + create_table "release_formats", :force => true do |t| + t.string "name" + t.string "hash" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "sessions", :force => true do |t| t.integer "user_id" t.string "key" diff --git a/lib/discogs.rb b/lib/discogs.rb index 50baafa..80529d0 100644 --- a/lib/discogs.rb +++ b/lib/discogs.rb @@ -18,45 +18,101 @@ class Discogs end def self.releases - self.get_nodes('tmp/data/discogs_releases_test.xml', 'release') do |node| + self.get_nodes('tmp/data/discogs_releases.xml', 'release') do |node| - 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?), + # Defining artist + main_artist = nil + unless node.css('artists > artist > name').empty? + main_artist = Artist.find_or_create_by_name(node.css('artists > artist > name').first.text) + end + + # Creating album + album = Album.new( :name => (node.css('title').first.text unless node.css('title').empty?), - :country => (node.css('country').first.text unless node.css('country').empty?), + :artist_id => (main_artist.id unless main_artist.nil?), :year => (node.css('released').first.text.split('-').first.to_i unless node.css('released').empty?), - - :genres => [], - :styles => [], - :tracks => [], - } + :status => 1, + :master => (not node.css('master_id').empty?) + ) + + # Parsing image + unless node.css('images > image[type="primary"]').empty? + album.pic_url = node.css('images > image[type="primary"]').first.attr('uri') + album.has_pic = 1 + else + album.has_pic = 0 + end + album.save ## ## ## + # Defining formats + formats = node.css('formats > format > descriptions > description').each do |f| + format = ReleaseFormat.find_or_create_by_name(f.text) + if format.hash.nil? + format.hash = f.text.scan(/\w/).join().downcase + format.save + end + AlbumFormat.new( + :album_id => album.id, + :release_format_id => format.id + ).save + end + + # Defining genres unless node.css('genres > genre').empty? - node.css('genres > genre').each do |genre| - release[:genres] << Genre.find_or_create_by_name(genre.text) + node.css('genres > genre').each do |g| + genre = Genre.find_or_create_by_name(g.text) + AlbumGenre.new( + :album_id => album.id, + :genre_id => genre.id + ).save end end + # Defining styles unless node.css('styles > style').empty? - node.css('styles > style').each do |style| - release[:styles] << Style.find_or_create_by_name(style.text) + node.css('styles > style').each do |s| + style = Style.find_or_create_by_name(s.text) + AlbumStyle.new( + :album_id => album.id, + :style_id => style.id + ).save end end - + + # Writing tracklist 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?) + node.css('tracklist > track').each do |t| + track = Track.new( + :album_id => album.id, + :name => (t.css('title').first.text unless t.css('title').empty?), + :position => (t.css('position').first.text.to_i(36) unless t.css('position').empty?), + :country => (node.css('country').first.text unless node.css('country').empty?), + :length => (self.duration_to_length(t.css('duration').first.text) unless t.css('duration').empty?) ) + track.save + + if t.css('artists > artist').empty? + TrackArtist.new( + :track_id => track.id, + :artist_id => (main_artist.id unless main_artist.nil?), + :main => true + ).save + else + t.css('artists > artist').each_with_index do |a, i| + t_artist = Artist.find_or_create_by_name(a.css('name').first.text) + TrackArtist.new( + :track_id => track.id, + :artist_id => t_artist.id, + :main => (i == 0), + :join => a.css('join').first.text + ).save + end + end + end end - ap release end end diff --git a/test/fixtures/album_formats.yml b/test/fixtures/album_formats.yml new file mode 100644 index 0000000..c8137ab --- /dev/null +++ b/test/fixtures/album_formats.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + album_id: 1 + release_format_id: 1 + +two: + album_id: 1 + release_format_id: 1 diff --git a/test/fixtures/album_genres.yml b/test/fixtures/album_genres.yml new file mode 100644 index 0000000..b0e4d82 --- /dev/null +++ b/test/fixtures/album_genres.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + album_id: 1 + genre_id: 1 + +two: + album_id: 1 + genre_id: 1 diff --git a/test/fixtures/album_styles.yml b/test/fixtures/album_styles.yml new file mode 100644 index 0000000..f13d654 --- /dev/null +++ b/test/fixtures/album_styles.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + album_id: 1 + style_id: 1 + +two: + album_id: 1 + style_id: 1 diff --git a/test/fixtures/release_formats.yml b/test/fixtures/release_formats.yml new file mode 100644 index 0000000..ec5d8f3 --- /dev/null +++ b/test/fixtures/release_formats.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html + +one: + name: MyString + hash: MyString + +two: + name: MyString + hash: MyString diff --git a/test/unit/album_format_test.rb b/test/unit/album_format_test.rb new file mode 100644 index 0000000..494eb97 --- /dev/null +++ b/test/unit/album_format_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class AlbumFormatTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/album_genre_test.rb b/test/unit/album_genre_test.rb new file mode 100644 index 0000000..aab348d --- /dev/null +++ b/test/unit/album_genre_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class AlbumGenreTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/album_style_test.rb b/test/unit/album_style_test.rb new file mode 100644 index 0000000..42581b1 --- /dev/null +++ b/test/unit/album_style_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class AlbumStyleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/release_format_test.rb b/test/unit/release_format_test.rb new file mode 100644 index 0000000..260f3ba --- /dev/null +++ b/test/unit/release_format_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ReleaseFormatTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end