1
0
Fork 0

Refactoring ReleaseGroup model

This commit is contained in:
Gregory Eremin 2012-07-09 01:19:56 +04:00
parent ab78050fec
commit 7de2a66613
8 changed files with 87 additions and 56 deletions

View File

@ -26,3 +26,5 @@ require "tools/proxy"
require "parsers/base"
require "parsers/artist"
require "parsers/release_group"
require "parsers/release"
require "parsers/track"

View File

@ -46,7 +46,6 @@ module MusicBrainz
:query => CGI.escape(name).gsub(/\!/, '\!') + '~',
:limit => 50
})
artists.each { |artist|
if artist[:name].downcase == name.downcase
artist[:weight] += 80

View File

@ -1,8 +1,13 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
class Release < MusicBrainz::Base
attr_accessor :id, :title, :status, :format, :date, :country
@tracks
field :id, String
field :title, String
field :status, String
field :format, String
field :date, Time
field :country, String
def tracks
if @tracks.nil? and not self.id.nil?
@ -14,29 +19,11 @@ module MusicBrainz
@tracks.sort{ |a, b| a.position <=> b.position }
end
def self.find mbid
xml = Nokogiri::XML(self.load(:release, :id => mbid, :inc => [:media])).css('release').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release = MusicBrainz::Release.new
@release.id = self.safe_get_attr(xml, nil, 'id')
@release.title = self.safe_get_value(xml, 'title')
@release.status = self.safe_get_value(xml, 'status')
@release.format = self.safe_get_value(xml, 'medium-list > medium > format')
date = xml.css('date').empty? ? '2030-12-31' : xml.css('date').text
if date.length == 0
date = '2030-12-31'
elsif date.length == 4
date += '-12-31'
elsif date.length == 7
date += '-31'
class << self
def find(mbid)
xml = Nokogiri::XML(self.load(:release, :id => mbid, :inc => [:media])).css('release').first
self.parse_xml(xml) unless xml.nil?
end
date = date.split('-')
@release.date = Time.utc(date[0], date[1], date[2])
@release.country = self.safe_get_value(xml, 'country')
@release
end
end
end

View File

@ -9,37 +9,31 @@ module MusicBrainz
field :first_release_date, Time
def releases
if @releases.nil? and not self.id.nil?
@releases = []
Nokogiri::XML(self.class.load(:release, :release_group => self.id, :inc => [:media], :limit => 100)).css('release').each do |r|
@releases << MusicBrainz::Release.parse_xml(r)
end
@releases ||= nil
if @releases.nil? and !id.nil?
@releases = self.class.load({
:parser => :release_group_releases,
:create_models => MusicBrainz::Release
}, {
:resource => :release,
:release_group => self.id,
:inc => [:media],
:limit => 100
})
end
@releases.sort{ |a, b| a.date <=> b.date }
end
def self.find mbid
xml = Nokogiri::XML(self.load(:release_group, :id => mbid)).css('release-group').first
self.parse_xml(xml) unless xml.nil?
end
def self.parse_xml xml
@release_group = MusicBrainz::ReleaseGroup.new
@release_group.id = self.safe_get_attr(xml, nil, 'id')
@release_group.type = self.safe_get_attr(xml, nil, 'type')
@release_group.title = self.safe_get_value(xml, 'title')
@release_group.disambiguation = self.safe_get_value(xml, 'disambiguation')
date = xml.css('first-release-date').empty? ? '2030-12-31' : xml.css('first-release-date').text
if date.length == 0
date = '2030-12-31'
elsif date.length == 4
date += '-12-31'
elsif date.length == 7
date += '-31'
class << self
def find(mbid)
load({
:parser => :release_group_model,
:create_model => MusicBrainz::ReleaseGroup
}, {
:resource => :release_group,
:id => mbid
})
end
date = date.split('-')
@release_group.first_release_date = Time.utc(date[0], date[1], date[2])
@release_group
end
end
end

View File

@ -5,11 +5,21 @@ module MusicBrainz
def get_by_name(name)
case name
when :artist_model
return { :const => MusicBrainz::Parsers::Artist, :method => :model }
{ :const => MusicBrainz::Parsers::Artist, :method => :model }
when :artist_search
return { :const => MusicBrainz::Parsers::Artist, :method => :search }
{ :const => MusicBrainz::Parsers::Artist, :method => :search }
when :artist_release_groups
return { :const => MusicBrainz::Parsers::Artist, :method => :release_groups }
{ :const => MusicBrainz::Parsers::Artist, :method => :release_groups }
when :release_group_model
{ :const => MusicBrainz::Parsers::ReleaseGroup, :method => :model }
when :release_group_releases
{ :const => MusicBrainz::Parsers::ReleaseGroup, :method => :releases }
when :release_model
{ :const => MusicBrainz::Parsers::Release, :method => :model }
when :release_tracks
{ :const => MusicBrainz::Parsers::Release, :method => :tracks }
when :track_model
{ :const => MusicBrainz::Parsers::Track, :method => :model }
end
end
end

20
lib/parsers/release.rb Normal file
View File

@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
module Parsers
class Release < Base
class << self
def model(xml)
{
:id => safe_get_attr(xml, nil, "id") || safe_get_attr(xml, "release-group", "id"),
:title => safe_get_value(xml, "title"),
:status => safe_get_value(xml, "status"),
:country => safe_get_value(xml, "country"),
:format => safe_get_value(xml, "medium-list > medium > format"),
:date => safe_get_value(xml, "date")
}
end
end
end
end
end

View File

@ -6,13 +6,21 @@ module MusicBrainz
class << self
def model(xml)
{
:id => safe_get_attr(xml, nil, "id"),
:type => safe_get_attr(xml, nil, "type"),
:id => safe_get_attr(xml, nil, "id") || safe_get_attr(xml, "release-group", "id"),
:type => safe_get_attr(xml, nil, "type") || safe_get_attr(xml, "release-group", "type"),
:title => safe_get_value(xml, "title"),
:disambiguation => safe_get_value(xml, "disambiguation"),
:first_release_date => safe_get_value(xml, "first-release-date")
}
end
def releases(xml)
releases = []
xml.css("release").each do |r|
releases << MusicBrainz::Parsers::Release.model(r)
end
releases
end
end
end
end

11
lib/parsers/track.rb Normal file
View File

@ -0,0 +1,11 @@
# -*- encoding: utf-8 -*-
module MusicBrainz
module Parsers
class Track < Base
class << self
end
end
end
end