2011-09-13 10:06:52 +00:00
|
|
|
module MusicBrainz
|
|
|
|
class Release
|
|
|
|
attr_accessor :id, :title, :status, :date, :country
|
|
|
|
@tracks
|
|
|
|
|
|
|
|
def tracks
|
|
|
|
if @tracks.nil? and not self.id.nil?
|
|
|
|
@tracks = []
|
2011-09-14 07:45:42 +00:00
|
|
|
Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release/' + self.id + '?inc=recordings&limit=100')).css('medium-list > medium > track-list > track').each do |r|
|
2011-09-13 10:06:52 +00:00
|
|
|
@tracks << MusicBrainz::Track.parse_xml(r)
|
|
|
|
end
|
|
|
|
end
|
2011-09-14 12:30:52 +00:00
|
|
|
@tracks.sort{ |a, b| a.position <=> b.position }
|
2011-09-13 10:06:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find mbid
|
|
|
|
xml = Nokogiri::XML(MusicBrainz.load('http://musicbrainz.org/ws/2/release/' + mbid)).css('release').first
|
|
|
|
self.parse_xml(xml) unless xml.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse_xml xml
|
|
|
|
@release = MusicBrainz::Release.new
|
|
|
|
@release.id = xml.attr('id')
|
2011-09-13 10:54:28 +00:00
|
|
|
@release.title = xml.css('title').text unless xml.css('title').empty?
|
|
|
|
@release.status = xml.css('status').text unless xml.css('status').empty?
|
2011-09-14 12:30:52 +00:00
|
|
|
date = xml.css('date').empty? ? '9999-12-31' : xml.css('date').text
|
|
|
|
if date.length == 0
|
|
|
|
date = '9999-12-31'
|
|
|
|
elsif date.length == 4
|
|
|
|
date += '-12-31'
|
|
|
|
elsif date.length == 7
|
|
|
|
date += '-31'
|
2011-09-13 10:06:52 +00:00
|
|
|
end
|
2011-09-14 12:30:52 +00:00
|
|
|
date = date.split('-')
|
|
|
|
@release.date = Time.utc(date[0], date[1], date[2])
|
2011-09-13 10:54:28 +00:00
|
|
|
@release.country = xml.css('country').text unless xml.css('country').empty?
|
2011-09-13 10:06:52 +00:00
|
|
|
@release
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|