1
0
Fork 0

Discogs releases parse

This commit is contained in:
Gregory Eremin 2011-09-22 19:40:44 +04:00
parent 7a3444d103
commit e235ad7fc5
11 changed files with 107 additions and 6 deletions

2
app/models/genre.rb Normal file
View File

@ -0,0 +1,2 @@
class Genre < ActiveRecord::Base
end

2
app/models/style.rb Normal file
View File

@ -0,0 +1,2 @@
class Style < ActiveRecord::Base
end

View File

@ -1,4 +1,4 @@
class Track < ActiveRecord::Base
belongs_to :album
belongs_to :artist, :through => :track_artists
has_many :artists, :through => :track_artists
end

View File

@ -0,0 +1,9 @@
class CreateGenres < ActiveRecord::Migration
def change
create_table :genres do |t|
t.string :name
t.timestamps
end
end
end

View File

@ -0,0 +1,9 @@
class CreateStyles < ActiveRecord::Migration
def change
create_table :styles do |t|
t.string :name
t.timestamps
end
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 => 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"

View File

@ -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

7
test/fixtures/genres.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
one:
name: MyString
two:
name: MyString

7
test/fixtures/styles.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/Fixtures.html
one:
name: MyString
two:
name: MyString

7
test/unit/genre_test.rb Normal file
View File

@ -0,0 +1,7 @@
require 'test_helper'
class GenreTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

7
test/unit/style_test.rb Normal file
View File

@ -0,0 +1,7 @@
require 'test_helper'
class StyleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end