Refactoring artitst, added base model
This commit is contained in:
parent
612e90ef4c
commit
2311b491d6
|
@ -2,16 +2,31 @@ module MusicBrainz
|
|||
@@last_query_time = 0
|
||||
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5
|
||||
|
||||
WEB_SERVICE_URL = 'http://musicbrainz.org/ws/2/'
|
||||
USER_AGENT = "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname
|
||||
|
||||
def self.query_interval= sec
|
||||
@@query_interval = sec.to_f
|
||||
end
|
||||
|
||||
def self.load resourse, params = {}
|
||||
url = WEB_SERVICE_URL + resourse.to_s.gsub('_', '-') + '/' + (params[:id].to_s || '')
|
||||
params.delete(:id) unless params[:id].nil?
|
||||
url << '?' + params.map{ |k, v|
|
||||
k.to_s + '=' + (v.is_a?(Array) ? v.map{ |_| _.to_s.gsub('_', '-') }.join(',') : v.to_s)
|
||||
}.join('&') unless params.empty?
|
||||
self.get_contents url
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.load url
|
||||
sleep @@query_interval - (Time.now.to_f - @@last_query_time) if Time.now.to_f - @@last_query_time < @@query_interval
|
||||
def self.get_contents url
|
||||
time_passed = Time.now.to_f - @@last_query_time
|
||||
sleep @@query_interval - time_passed if time_passed < @@query_interval
|
||||
response = nil
|
||||
5.times do
|
||||
begin
|
||||
response = open(url, "User-Agent" => "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname)
|
||||
response = open(url, "User-Agent" => USER_AGENT)
|
||||
@@last_query_time = Time.now.to_f
|
||||
rescue => e
|
||||
return nil if e.io.status[0].to_i == 404
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
module MusicBrainz
|
||||
class Artist
|
||||
class Artist < MusicBrainz::Base
|
||||
attr_accessor :id, :type, :name, :country, :date_begin, :date_end
|
||||
@release_groups
|
||||
|
||||
def release_groups
|
||||
if @release_groups.nil? and not self.id.nil?
|
||||
@release_groups = []
|
||||
Nokogiri::XML(MusicBrainz.load(
|
||||
'http://musicbrainz.org/ws/2/release-group/?artist=' + self.id
|
||||
)).css('release-group').each do |rg|
|
||||
Nokogiri::XML(MusicBrainz.load(:release_group, :artist => self.id)).css('release-group').each do |rg|
|
||||
@release_groups << MusicBrainz::ReleaseGroup.parse_xml(rg)
|
||||
end
|
||||
end
|
||||
|
@ -16,19 +14,19 @@ module MusicBrainz
|
|||
end
|
||||
|
||||
def self.find mbid
|
||||
res = MusicBrainz.load('http://musicbrainz.org/ws/2/artist/' + mbid)
|
||||
res = MusicBrainz.load :artist, :id => mbid
|
||||
return nil if res.nil?
|
||||
@artist = self.parse_xml(Nokogiri::XML(res))
|
||||
end
|
||||
|
||||
def self.parse_xml xml
|
||||
@artist = MusicBrainz::Artist.new
|
||||
@artist.id = xml.css('artist').attr('id').value
|
||||
@artist.type = xml.css('artist').attr('type').value unless xml.css('artist').nil? or xml.css('artist').attr('type').nil?
|
||||
@artist.name = xml.css('artist > name').text
|
||||
@artist.country = xml.css('artist > country').text unless xml.css('artist > country').empty?
|
||||
@artist.date_begin = xml.css('artist > life-span > begin').text unless xml.css('artist > life-span > begin').empty?
|
||||
@artist.date_end = xml.css('artist > life-span > end').text unless xml.css('artist > life-span > end').empty?
|
||||
@artist.id = self.safe_get_attr(xml, 'artist', 'id')
|
||||
@artist.type = self.safe_get_attr(xml, 'artist', 'type')
|
||||
@artist.name = self.safe_get_value(xml, 'artist > name')
|
||||
@artist.country = self.safe_get_value(xml, 'artist > country')
|
||||
@artist.date_begin = self.safe_get_value(xml, 'artist > life-span > begin')
|
||||
@artist.date_end = self.safe_get_value(xml, 'artist > life-span > end')
|
||||
@artist
|
||||
end
|
||||
|
||||
|
@ -45,16 +43,14 @@ module MusicBrainz
|
|||
|
||||
def self.search name
|
||||
artists = []
|
||||
xml = Nokogiri::XML(MusicBrainz.load(
|
||||
'http://musicbrainz.org/ws/2/artist/?query='+ CGI.escape(name).gsub(/\!/, '') +'~&limit=50'
|
||||
))
|
||||
xml = Nokogiri::XML(MusicBrainz.load(:artist, :query => CGI.escape(name).gsub(/\!/, '') + '~', :limit => 50))
|
||||
xml.css('artist-list > artist').each do |a|
|
||||
artist = {
|
||||
:name => a.first_element_child.text,
|
||||
:weight => 0,
|
||||
:desc => (a.css('disambiguation').text unless a.css('disambiguation').empty?),
|
||||
:type => a.attr('type'),
|
||||
:mbid => a.attr('id')
|
||||
:desc => self.safe_get_value(a, 'disambiguation'),
|
||||
:type => self.safe_get_attr(a, 'type'),
|
||||
:mbid => self.safe_get_attr(a, 'id')
|
||||
}
|
||||
aliases = a.css('alias-list > alias').map{ |item| item.text }
|
||||
if aliases.include? name
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
module MusicBrainz
|
||||
class Base
|
||||
def self.safe_get_attr xml, path, name
|
||||
xml.css(path).first.attr(name) unless xml.css(path).empty? or xml.css(path).first.attr(name).nil?
|
||||
end
|
||||
|
||||
def self.safe_get_value xml, path
|
||||
xml.css(path).first.text unless xml.css(path).empty?
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,6 +4,7 @@ require "nokogiri"
|
|||
require "cgi"
|
||||
|
||||
require "models/music_brainz"
|
||||
require "models/music_brainz/base"
|
||||
require "models/music_brainz/artist"
|
||||
require "models/music_brainz/release_group"
|
||||
require "models/music_brainz/release"
|
||||
|
|
Loading…
Reference in New Issue