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