1
0
Fork 0

Sorting, tests

This commit is contained in:
Gregory Eremin 2011-09-14 16:30:52 +04:00
parent b10abcf67c
commit 78418e9325
7 changed files with 47 additions and 44 deletions

View File

@ -1 +1 @@
0.4.1
0.4.2

View File

@ -1,6 +1,10 @@
module MusicBrainz
@@last_query_time = 0
@@query_interval = 1.1
@@query_interval = 1.5 # Min: 1.0 Safe: 1.5
def self.query_interval sec
@@query_interval = sec.to_f
end
def self.load url
sleep @@query_interval - (Time.now.to_f - @@last_query_time) if Time.now.to_f - @@last_query_time < @@query_interval
@ -10,7 +14,7 @@ module MusicBrainz
response = open(url, "User-Agent" => "gem musicbrainz (https://github.com/magnolia-fan/musicbrainz) @ " + Socket.gethostname)
@@last_query_time = Time.now.to_f
rescue => e
p "MusicBrainz: 503"
# MusicBrainz: 503
end
break unless response.nil?
end

View File

@ -12,7 +12,7 @@ module MusicBrainz
@release_groups << MusicBrainz::ReleaseGroup.parse_xml(rg)
end
end
@release_groups
@release_groups.sort{ |a, b| a.first_release_date <=> b.first_release_date }
end
def self.find mbid

View File

@ -10,7 +10,7 @@ module MusicBrainz
@tracks << MusicBrainz::Track.parse_xml(r)
end
end
@tracks
@tracks.sort{ |a, b| a.position <=> b.position }
end
def self.find mbid
@ -23,17 +23,16 @@ module MusicBrainz
@release.id = xml.attr('id')
@release.title = xml.css('title').text unless xml.css('title').empty?
@release.status = xml.css('status').text unless xml.css('status').empty?
date = nil
date = xml.css('date').text unless xml.css('date').empty?
unless date.nil? or date.empty?
if date.length == 4
date += '-01-01'
elsif date.length == 7
date += '-01'
end
date = Time.parse(date)
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'
end
@release.date = date
date = date.split('-')
@release.date = Time.utc(date[0], date[1], date[2])
@release.country = xml.css('country').text unless xml.css('country').empty?
@release
end

View File

@ -1,6 +1,6 @@
module MusicBrainz
class ReleaseGroup
attr_accessor :id, :type, :title, :first_release_date
attr_accessor :id, :type, :title, :disambiguation, :first_release_date
@releases
def releases
@ -10,7 +10,7 @@ module MusicBrainz
@releases << MusicBrainz::Release.parse_xml(r)
end
end
@releases
@releases.sort{ |a, b| a.date <=> b.date }
end
def self.find mbid
@ -22,18 +22,18 @@ module MusicBrainz
@release_group = MusicBrainz::ReleaseGroup.new
@release_group.id = xml.attr('id')
@release_group.type = xml.attr('type')
@release_group.title = xml.css('title').text unless xml.css('title').empty?
date = nil
date = xml.css('first-release-date').text unless xml.css('first-release-date').empty?
unless date.nil? or date.empty?
if date.length == 4
date += '-01-01'
elsif date.length == 7
date += '-01'
end
date = Time.parse(date)
@release_group.title = xml.css('title').text
@release_group.disambiguation = xml.css('disambiguation').empty? ? '' : xml.css('disambiguation').text
date = xml.css('first-release-date').empty? ? '9999-12-31' : xml.css('first-release-date').text
if date.length == 0
date = '9999-12-31'
elsif date.length == 4
date += '-12-31'
elsif date.length == 7
date += '-31'
end
@release_group.first_release_date = date
date = date.split('-')
@release_group.first_release_date = Time.utc(date[0], date[1], date[2])
@release_group
end
end

View File

@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{musicbrainz}
s.version = "0.4.1"
s.version = "0.4.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = [%q{Gregory Eremin}]

View File

@ -38,10 +38,10 @@ class TestMusicbrainz < Test::Unit::TestCase
should "load release groups" do
release_groups = @artist.release_groups
assert_operator(16, :<=, release_groups.length)
assert_equal('0244b82e-0796-36fc-a629-2507d5441124', release_groups[0].id)
assert_equal('Single', release_groups[0].type)
assert_equal('Shoot the Runner', release_groups[0].title)
assert_equal(Time.parse('2006-01-01'), release_groups[0].first_release_date)
assert_equal('533cbc5f-ec7e-32ab-95f3-8d1f804a5176', release_groups.first.id)
assert_equal('Single', release_groups.first.type)
assert_equal('Club Foot', release_groups.first.title)
assert_equal(Time.utc(2004, 5, 10), release_groups.first.first_release_date)
end
end
@ -64,17 +64,17 @@ class TestMusicbrainz < Test::Unit::TestCase
assert_equal("6f33e0f0-cde2-38f9-9aee-2c60af8d1a61", @release_group.id)
assert_equal("Album", @release_group.type)
assert_equal("Empire", @release_group.title)
assert_equal(Time.parse('2006-08-28'), @release_group.first_release_date)
assert_equal(Time.utc(2006, 8, 28), @release_group.first_release_date)
end
should "load releases" do
releases = @release_group.releases
assert_operator(5, :<=, releases.length)
assert_equal('2225dd4c-ae9a-403b-8ea0-9e05014c778f', releases[0].id)
assert_equal('Official', releases[0].status)
assert_equal('Empire', releases[0].title)
assert_equal(Time.parse('2006-08-28'), releases[0].date)
assert_equal('GB', releases[0].country)
assert_equal('2225dd4c-ae9a-403b-8ea0-9e05014c778f', releases.first.id)
assert_equal('Official', releases.first.status)
assert_equal('Empire', releases.first.title)
assert_equal(Time.utc(2006, 8, 28), releases.first.date)
assert_equal('GB', releases.first.country)
end
end
@ -97,17 +97,17 @@ class TestMusicbrainz < Test::Unit::TestCase
assert_equal("2225dd4c-ae9a-403b-8ea0-9e05014c778f", @release.id)
assert_equal("Empire", @release.title)
assert_equal("Official", @release.status)
assert_equal(Time.parse('2006-08-28'), @release.date)
assert_equal(Time.utc(2006, 8, 28), @release.date)
assert_equal("GB", @release.country)
end
should "load tracks" do
tracks = @release.tracks
assert_equal(11, tracks.length)
assert_equal(1, tracks[0].position)
assert_equal('b3015bab-1540-4d4e-9f30-14872a1525f7', tracks[0].recording_id)
assert_equal('Empire', tracks[0].title)
assert_equal(233013, tracks[0].length)
assert_equal(1, tracks.first.position)
assert_equal('b3015bab-1540-4d4e-9f30-14872a1525f7', tracks.first.recording_id)
assert_equal('Empire', tracks.first.title)
assert_equal(233013, tracks.first.length)
end
end