2011-09-21 06:40:04 +00:00
|
|
|
class Discogs
|
2011-09-22 12:04:59 +00:00
|
|
|
|
2011-09-21 06:40:04 +00:00
|
|
|
def self.artists
|
2011-09-22 15:40:44 +00:00
|
|
|
self.get_nodes('tmp/data/discogs_artists.xml', 'artist') do |node|
|
2011-09-22 12:04:59 +00:00
|
|
|
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?),
|
|
|
|
:status => 1
|
|
|
|
)
|
|
|
|
artist.save
|
|
|
|
node.css('namevariations > name, aliases > name').each do |v|
|
|
|
|
ArtistAlias.new(
|
|
|
|
:artist_id => artist.id,
|
|
|
|
:name => v.text
|
|
|
|
).save
|
2011-09-21 06:40:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-22 12:04:59 +00:00
|
|
|
def self.releases
|
2011-09-22 19:54:09 +00:00
|
|
|
self.get_nodes('tmp/data/discogs_releases.xml', 'release') do |node|
|
2011-09-22 15:40:44 +00:00
|
|
|
|
2011-09-22 19:54:09 +00:00
|
|
|
# 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(
|
2011-09-22 15:40:44 +00:00
|
|
|
:name => (node.css('title').first.text unless node.css('title').empty?),
|
2011-09-22 19:54:09 +00:00
|
|
|
:artist_id => (main_artist.id unless main_artist.nil?),
|
2011-09-22 15:40:44 +00:00
|
|
|
:year => (node.css('released').first.text.split('-').first.to_i unless node.css('released').empty?),
|
2011-09-22 19:54:09 +00:00
|
|
|
: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
|
2011-09-22 15:40:44 +00:00
|
|
|
|
2011-09-22 19:54:09 +00:00
|
|
|
album.save ## ## ##
|
2011-09-22 15:40:44 +00:00
|
|
|
|
2011-09-22 19:54:09 +00:00
|
|
|
# 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
|
2011-09-22 15:40:44 +00:00
|
|
|
unless node.css('genres > genre').empty?
|
2011-09-22 19:54:09 +00:00
|
|
|
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
|
2011-09-22 15:40:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-22 19:54:09 +00:00
|
|
|
# Defining styles
|
2011-09-22 15:40:44 +00:00
|
|
|
unless node.css('styles > style').empty?
|
2011-09-22 19:54:09 +00:00
|
|
|
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
|
2011-09-22 15:40:44 +00:00
|
|
|
end
|
|
|
|
end
|
2011-09-22 19:54:09 +00:00
|
|
|
|
|
|
|
# Writing tracklist
|
2011-09-22 15:40:44 +00:00
|
|
|
unless node.css('tracklist > track').empty?
|
2011-09-22 19:54:09 +00:00
|
|
|
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?)
|
2011-09-22 15:40:44 +00:00
|
|
|
)
|
2011-09-22 19:54:09 +00:00
|
|
|
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
|
|
|
|
|
2011-09-22 15:40:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-21 18:59:39 +00:00
|
|
|
end
|
2011-09-21 06:40:04 +00:00
|
|
|
end
|
|
|
|
|
2011-09-22 12:04:59 +00:00
|
|
|
private
|
2011-09-21 06:40:04 +00:00
|
|
|
|
2011-09-22 12:04:59 +00:00
|
|
|
def self.get_nodes filename, nodename, &block
|
|
|
|
File.open(filename) do |file|
|
|
|
|
Nokogiri::XML::Reader.from_io(file).each do |node|
|
|
|
|
if node.name == nodename and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
|
|
|
|
yield(Nokogiri::XML(node.outer_xml).root)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-09-21 06:40:04 +00:00
|
|
|
end
|
2011-09-22 12:04:59 +00:00
|
|
|
|
2011-09-22 15:40:44 +00:00
|
|
|
def self.duration_to_length duration
|
|
|
|
duration = duration.split(':')
|
|
|
|
duration[0].to_i * 60 + duration[1].to_i
|
|
|
|
end
|
|
|
|
|
2011-09-21 06:40:04 +00:00
|
|
|
end
|