discogs 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +9 -0
- data/README.txt +41 -38
- data/Rakefile +2 -2
- data/lib/artist.rb +16 -5
- data/lib/base.rb +19 -8
- data/lib/contributor.rb +2 -2
- data/lib/discogs.rb +2 -1
- data/lib/format.rb +1 -1
- data/lib/image.rb +1 -1
- data/lib/label.rb +14 -2
- data/lib/release.rb +14 -2
- data/lib/search.rb +11 -6
- data/lib/track.rb +1 -1
- data/spec/artist_spec.rb +10 -0
- data/spec/search_spec.rb +8 -0
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
=== 1.1.0 / 2008-09-03
|
2
|
+
|
3
|
+
* Fixes by Andreas Roedl <andreas.roedl@googlemail.com>
|
4
|
+
|
5
|
+
* Images works as expected. Returns the main URI.
|
6
|
+
* Fixed Artist.releases.
|
7
|
+
* Fixed Artist.urls. Now we get an array of URLs. We're also chomping line feeds.
|
8
|
+
* Minor documentation fixes.
|
9
|
+
|
1
10
|
=== 1.0.0 / 2008-07-08
|
2
11
|
|
3
12
|
* 1 major enhancement
|
data/README.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
=
|
1
|
+
= discogs
|
2
2
|
|
3
3
|
* http://discogs.rubyforge.org
|
4
4
|
|
@@ -26,46 +26,49 @@ And a bin util
|
|
26
26
|
|
27
27
|
== SYNOPSIS:
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
29
|
+
The first time discogs tries to make an api call, it will look for a YAML file '~/.discogs' or 'RAILS_ROOT/config/discogs.yml'.
|
30
|
+
If it doesn't exist, it will be created, and will need editing. Paste your discogs api key in place of 'CHANGE ME'.
|
31
|
+
|
32
|
+
artist = Discog::Artist.new('Broken Social Scene')
|
33
|
+
puts "
|
34
|
+
Artist: #{artist.name}
|
35
|
+
Members: #{artist.members}
|
36
|
+
Images: #{artist.images}
|
37
|
+
URLs: #{artist.urls}
|
38
|
+
Releases: #{artist.releases.map{|r| r.title }}
|
39
|
+
"
|
40
|
+
|
41
|
+
release = Discog::Release.new('188365')
|
42
|
+
puts "
|
43
|
+
Title: #{release.title}
|
44
|
+
Labels: #{release.labels}
|
45
|
+
Format: #{release.formats}
|
46
|
+
Status: #{release.discog_status}
|
47
|
+
Genres: #{release.genres}
|
48
|
+
Styles: #{release.styles}
|
49
|
+
Country: #{release.country}
|
50
|
+
ReleaseDate: #{release.released}
|
51
|
+
Tracks #{release.tracks}
|
52
|
+
Images: #{release.images}
|
53
|
+
Extras: #{release.contributors.join(', ')}
|
54
|
+
"
|
55
|
+
|
56
|
+
label = Discog::Label.new(ARGV[0])
|
57
|
+
puts "
|
58
|
+
Name: #{label.name}
|
59
|
+
Images: #{label.images}
|
60
|
+
Contact: #{label.contact_info}
|
61
|
+
Profile: #{label.profile}
|
62
|
+
URLs: #{label.urls}
|
63
|
+
Parent: #{label.parent}
|
64
|
+
SubLabels: #{label.sublabels}
|
65
|
+
Releases: #{label.releases}
|
66
|
+
"
|
64
67
|
|
65
68
|
== REQUIREMENTS:
|
66
69
|
|
67
|
-
rubygems
|
68
|
-
hpricot
|
70
|
+
- rubygems
|
71
|
+
- hpricot
|
69
72
|
|
70
73
|
== INSTALL:
|
71
74
|
|
data/Rakefile
CHANGED
@@ -14,8 +14,8 @@ end
|
|
14
14
|
|
15
15
|
desc "Run all specs in spec directory"
|
16
16
|
Spec::Rake::SpecTask.new(:spec) do |t|
|
17
|
-
|
18
|
-
|
17
|
+
t.rcov = true
|
18
|
+
t.rcov_opts = ["--output=coverage"]
|
19
19
|
t.spec_opts = ["--color"]
|
20
20
|
t.spec_files = FileList['spec/*_spec.rb']
|
21
21
|
end
|
data/lib/artist.rb
CHANGED
@@ -8,21 +8,28 @@ module Discogs
|
|
8
8
|
:images,
|
9
9
|
:realname
|
10
10
|
|
11
|
+
# Discogs Data
|
11
12
|
def name
|
12
13
|
@name ||= (parsed/'artist'/'name').first.inner_html
|
13
14
|
end
|
14
15
|
|
16
|
+
# Discogs Data
|
15
17
|
def realname
|
16
|
-
@realname ||=
|
18
|
+
@realname ||= begin
|
19
|
+
realname = (parsed/'artist'/'realname').first
|
20
|
+
realname.nil? ? '' : realname.inner_html
|
21
|
+
end
|
17
22
|
end
|
18
23
|
|
24
|
+
# Discogs Data
|
19
25
|
def members
|
20
26
|
@members ||= (parsed/'artist'/'members'/'name').collect { |artist| artist.inner_html }
|
21
27
|
end
|
22
28
|
|
29
|
+
# an array images
|
23
30
|
def images
|
24
31
|
@images ||= (parsed/'artist'/'images'/'image').collect do |image|
|
25
|
-
Image.new(image.attributes['
|
32
|
+
Image.new(image.attributes['uri'],
|
26
33
|
image.attributes['type'],
|
27
34
|
image.attributes['uri150'],
|
28
35
|
image.attributes['width'],
|
@@ -30,17 +37,21 @@ module Discogs
|
|
30
37
|
end
|
31
38
|
end
|
32
39
|
|
40
|
+
# an array of urls
|
33
41
|
def urls
|
34
|
-
@urls ||= (parsed/'artist'/'urls').collect { |url|
|
42
|
+
@urls ||= (parsed/'artist'/'urls'/'url').collect { |url| url.inner_html.chomp }
|
35
43
|
end
|
36
44
|
|
45
|
+
# for each release on the artist
|
46
|
+
# this will make a new API request
|
47
|
+
# for each release. Be careful!!!
|
37
48
|
def releases
|
38
49
|
@release ||= (parsed/'artist'/'releases'/'release').collect do |release|
|
39
|
-
Release.new(
|
50
|
+
Release.new(release.attributes['id'])
|
40
51
|
end
|
41
52
|
end
|
42
53
|
|
43
|
-
def to_s
|
54
|
+
def to_s #:nodoc:
|
44
55
|
name
|
45
56
|
end
|
46
57
|
end
|
data/lib/base.rb
CHANGED
@@ -17,49 +17,60 @@ module Discogs
|
|
17
17
|
@request_type = request_type.to_s
|
18
18
|
end
|
19
19
|
|
20
|
+
# Hpricot parsed XML
|
20
21
|
def parsed
|
21
22
|
@parsed ||= Hpricot.XML(fetched)
|
22
23
|
end
|
23
24
|
|
24
|
-
def fetched
|
25
|
+
def fetched #:nodoc:
|
25
26
|
@fetched ||= begin
|
27
|
+
begin
|
26
28
|
gz = Zlib::GzipReader.new(open(request_url, 'Accept-encoding' => 'gzip, deflate'))
|
27
29
|
fetched = gz.read
|
28
30
|
gz.close
|
31
|
+
rescue
|
32
|
+
fetched = open(request_url).read
|
33
|
+
end
|
29
34
|
fetched
|
30
35
|
end
|
31
36
|
end
|
32
37
|
|
33
38
|
protected
|
34
|
-
def request_url
|
39
|
+
def request_url #:nodoc:
|
35
40
|
"#{DISCOGS_BASE_URL}/#{request_type}/#{CGI::escape(request_string)}?f=xml&api_key=#{api_key}"
|
36
41
|
end
|
37
42
|
|
38
|
-
|
43
|
+
# try to load api key from yaml file
|
44
|
+
# If it doesn't exist, then create it
|
45
|
+
def load_api_key #:nodoc:
|
39
46
|
begin
|
40
47
|
YAML::load(open(config_file))[:api_key]
|
41
48
|
rescue Errno::ENOENT
|
42
49
|
File.open(config_file, 'w') do |file|
|
43
50
|
file.write({:api_key => "CHANGE ME"}.to_yaml)
|
44
|
-
puts "Please edit #{config_file}"
|
45
51
|
end
|
52
|
+
logger.warn "DISCOGS:: Please edit #{config_file}"
|
46
53
|
end
|
47
54
|
end
|
48
55
|
|
49
|
-
def home_dir
|
56
|
+
def home_dir #:nodoc:
|
50
57
|
ENV['HOME'] || ENV['USERPROFILE'] || ENV['HOMEPATH']
|
51
58
|
end
|
52
59
|
|
53
|
-
def config_filename(rails = false)
|
60
|
+
def config_filename(rails = false) #:nodoc:
|
54
61
|
rails ? 'discogs.yml' : '.discogs'
|
55
62
|
end
|
56
63
|
|
57
|
-
def config_file
|
64
|
+
def config_file #:nodoc:
|
58
65
|
if defined?(RAILS_ROOT)
|
59
|
-
File.join(RAILS_ROOT, 'config', config_filename)
|
66
|
+
File.join(RAILS_ROOT, 'config', config_filename(true))
|
60
67
|
else
|
61
68
|
File.join(home_dir, config_filename)
|
62
69
|
end
|
63
70
|
end
|
71
|
+
|
72
|
+
def logger #:nodoc:
|
73
|
+
@logger ||= (ActiveRecord::Base.logger || Logger.new)
|
74
|
+
end
|
64
75
|
end
|
65
76
|
end
|
data/lib/contributor.rb
CHANGED
data/lib/discogs.rb
CHANGED
@@ -5,6 +5,7 @@ require 'hpricot'
|
|
5
5
|
require 'zlib'
|
6
6
|
require 'cgi'
|
7
7
|
|
8
|
+
# discgs gem libraries
|
8
9
|
require 'base'
|
9
10
|
require 'artist'
|
10
11
|
require 'label'
|
@@ -17,6 +18,6 @@ require 'search'
|
|
17
18
|
|
18
19
|
|
19
20
|
module Discogs
|
20
|
-
VERSION = '1.
|
21
|
+
VERSION = '1.1.0'
|
21
22
|
API_VERSION = '1.0'
|
22
23
|
end
|
data/lib/format.rb
CHANGED
data/lib/image.rb
CHANGED
data/lib/label.rb
CHANGED
@@ -4,17 +4,20 @@ module Discogs
|
|
4
4
|
|
5
5
|
def initialize(request_string)
|
6
6
|
@request_string = request_string
|
7
|
-
super(@request_string,
|
7
|
+
super(@request_string, :label)
|
8
8
|
end
|
9
9
|
|
10
|
+
# Discogs Data
|
10
11
|
def label
|
11
12
|
@label ||= (parsed/'label')
|
12
13
|
end
|
13
14
|
|
15
|
+
# Discogs Data
|
14
16
|
def name
|
15
17
|
@name ||= (label/'name').inner_html
|
16
18
|
end
|
17
19
|
|
20
|
+
# An array of image objects
|
18
21
|
def images
|
19
22
|
@images ||= (label/'images'/'image').collect do |image|
|
20
23
|
Image.new(image.attributes['url'],
|
@@ -25,18 +28,23 @@ module Discogs
|
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
31
|
+
# Discogs Data
|
28
32
|
def contact_info
|
29
33
|
@contact_info ||= (label/'contactinfo').inner_html
|
30
34
|
end
|
31
35
|
|
36
|
+
# Discogs Data
|
32
37
|
def profile
|
33
38
|
@profile ||= (label/'profile').inner_html
|
34
39
|
end
|
35
40
|
|
41
|
+
# Discogs Data
|
42
|
+
# An array of strings
|
36
43
|
def urls
|
37
44
|
@url ||= (label/'urls'/'url').collect { |url | url.inner_html }
|
38
45
|
end
|
39
46
|
|
47
|
+
# parent label if applicable
|
40
48
|
def parent
|
41
49
|
@parent_label ||= begin
|
42
50
|
Label.new((label/'parentLabel').inner_html) unless (label/'parentLabel').empty?
|
@@ -44,6 +52,7 @@ module Discogs
|
|
44
52
|
end
|
45
53
|
end
|
46
54
|
|
55
|
+
# sublabels if applicable
|
47
56
|
def sublabels
|
48
57
|
@sub_labels ||= begin
|
49
58
|
(label/'sublabels'/'label').collect do |label|
|
@@ -54,6 +63,9 @@ module Discogs
|
|
54
63
|
end
|
55
64
|
end
|
56
65
|
|
66
|
+
# Be careful, this creates a Release object for each release.
|
67
|
+
# Data is lazy loaded, so an API call will be created as soon
|
68
|
+
# as you access the attributes
|
57
69
|
def releases
|
58
70
|
@releases ||= begin
|
59
71
|
(label/'releases'/'release').collect do |release|
|
@@ -63,7 +75,7 @@ module Discogs
|
|
63
75
|
end
|
64
76
|
end
|
65
77
|
|
66
|
-
def to_s
|
78
|
+
def to_s #:nodoc:
|
67
79
|
name
|
68
80
|
end
|
69
81
|
end
|
data/lib/release.rb
CHANGED
@@ -21,40 +21,50 @@ module Discogs
|
|
21
21
|
super
|
22
22
|
end
|
23
23
|
|
24
|
+
# status as determined by discogs
|
25
|
+
# 'Approved', etc..
|
24
26
|
def discog_status
|
25
27
|
@discog_status ||= (parsed/'release').first.attributes['status']
|
26
28
|
end
|
27
29
|
|
30
|
+
# Discogs Data
|
28
31
|
def title
|
29
32
|
@title ||= (parsed/'title').first.inner_html
|
30
33
|
end
|
31
34
|
|
35
|
+
# Discogs Data
|
32
36
|
def labels
|
33
37
|
@labels ||= (parsed/'labels'/'label').collect { |label| label.attributes['name'] }
|
34
38
|
end
|
35
39
|
|
40
|
+
# Discogs Data
|
36
41
|
def genres
|
37
42
|
@genres ||= (parsed/'genres'/'genre').collect { |genre| genre.inner_html }
|
38
43
|
end
|
39
44
|
|
45
|
+
# Discogs Data
|
40
46
|
def styles
|
41
47
|
@styles ||= (parsed/'styles'/'style').collect{ |label| label.inner_html }
|
42
48
|
end
|
43
49
|
|
50
|
+
# Discogs Data
|
44
51
|
def country
|
45
52
|
@country ||= (parsed/'country').inner_html
|
46
53
|
end
|
47
54
|
|
55
|
+
# Discogs Data
|
48
56
|
def released
|
49
57
|
@released ||= (parsed/'released').inner_html
|
50
58
|
end
|
51
59
|
|
60
|
+
# An array of Format objects
|
52
61
|
def formats
|
53
62
|
@formats ||= (parsed/'formats'/'format').collect do |format|
|
54
63
|
Format.new(format.attributes['name'], format.attributes['qty'])
|
55
64
|
end
|
56
65
|
end
|
57
66
|
|
67
|
+
# An array of Track objects
|
58
68
|
def tracks
|
59
69
|
@tracks ||= (parsed/'tracklist'/'track').collect do |track|
|
60
70
|
Track.new((track/'position').inner_html,
|
@@ -64,6 +74,7 @@ module Discogs
|
|
64
74
|
end
|
65
75
|
end
|
66
76
|
|
77
|
+
# An array of Image objects
|
67
78
|
def images
|
68
79
|
@images ||= (parsed/'images'/'image').collect do |image|
|
69
80
|
Image.new(image.attributes['url'],
|
@@ -74,18 +85,19 @@ module Discogs
|
|
74
85
|
end
|
75
86
|
end
|
76
87
|
|
88
|
+
# An array of Contributor objects
|
77
89
|
def contributors
|
78
90
|
@contributors ||= (parsed/'extraartists'/'artist').collect do |artist|
|
79
91
|
Contributor.new((artist/'name').inner_html, (artist/'role').inner_html)
|
80
92
|
end
|
81
93
|
end
|
82
94
|
|
83
|
-
def to_s
|
95
|
+
def to_s #:nodoc:
|
84
96
|
"#{title} - #{label} - #{released} - #{format}"
|
85
97
|
end
|
86
98
|
|
87
99
|
protected
|
88
|
-
def request_url
|
100
|
+
def request_url #:nodoc:
|
89
101
|
"#{DISCOGS_BASE_URL}/release/#{discog_id}?f=xml&api_key=#{api_key}"
|
90
102
|
end
|
91
103
|
end
|
data/lib/search.rb
CHANGED
@@ -13,14 +13,17 @@ module Discogs
|
|
13
13
|
super
|
14
14
|
end
|
15
15
|
|
16
|
+
# size of exact search phrase matches
|
16
17
|
def exactresult_size
|
17
18
|
@exactresult_size ||= exactresults.size
|
18
19
|
end
|
19
20
|
|
21
|
+
# size of fuzzy matches
|
20
22
|
def result_size
|
21
|
-
(parsed/'searchresults').first.attributes['end'].to_i
|
23
|
+
@result_size ||= (parsed/'searchresults').first.attributes['end'].to_i
|
22
24
|
end
|
23
25
|
|
26
|
+
# results that match the search phrase exactly
|
24
27
|
def exactresults
|
25
28
|
(parsed/'exactresults'/'result').collect do |result|
|
26
29
|
SearchResult.new(
|
@@ -30,9 +33,9 @@ module Discogs
|
|
30
33
|
(result/'summary').inner_html
|
31
34
|
)
|
32
35
|
end
|
33
|
-
|
34
36
|
end
|
35
37
|
|
38
|
+
# fuzzy results
|
36
39
|
def results
|
37
40
|
(parsed/'searchresults'/'result').collect do |result|
|
38
41
|
SearchResult.new(
|
@@ -44,6 +47,8 @@ module Discogs
|
|
44
47
|
end
|
45
48
|
end
|
46
49
|
|
50
|
+
# use the exact results to get more data using another
|
51
|
+
# api call based on the result type
|
47
52
|
def best_match
|
48
53
|
first_result = exactresults.first
|
49
54
|
if first_result.artist?
|
@@ -53,8 +58,8 @@ module Discogs
|
|
53
58
|
end
|
54
59
|
|
55
60
|
protected
|
56
|
-
def request_url
|
57
|
-
"#{
|
61
|
+
def request_url #:nodoc:
|
62
|
+
"#{DISCOGS_BASE_URL}/search?type=#{search_type}&q=#{CGI::escape(request_string)}&f=xml&api_key=#{api_key}"
|
58
63
|
end
|
59
64
|
end
|
60
65
|
|
@@ -71,14 +76,14 @@ module Discogs
|
|
71
76
|
@summary = summary
|
72
77
|
end
|
73
78
|
|
74
|
-
def method_missing(method, *args, &block)
|
79
|
+
def method_missing(method, *args, &block) #:nodoc:
|
75
80
|
type = method.to_s.match(/([a-zA-Z0-9_-]+)\?/)
|
76
81
|
# if type.size < 2 the method did not have a question mark at the end
|
77
82
|
super if type.size < 2
|
78
83
|
@type == type[1].to_s
|
79
84
|
end
|
80
85
|
|
81
|
-
def to_s
|
86
|
+
def to_s #:nodoc:
|
82
87
|
"#{title}"
|
83
88
|
end
|
84
89
|
|
data/lib/track.rb
CHANGED
data/spec/artist_spec.rb
CHANGED
@@ -32,3 +32,13 @@ describe Discogs::Artist do
|
|
32
32
|
@artist.releases.should eql([mock_release]*59)
|
33
33
|
end
|
34
34
|
end
|
35
|
+
describe Discogs::Artist do
|
36
|
+
describe "network connections" do
|
37
|
+
it "does not make more connections than it needs" do
|
38
|
+
@artist = Discogs::Artist.new('Broken Social Scene')
|
39
|
+
@artist.name
|
40
|
+
sleep 2
|
41
|
+
@artist.realname
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/search_spec.rb
CHANGED
@@ -27,4 +27,12 @@ describe Discogs::Search do
|
|
27
27
|
it "returns fuzzy result size" do
|
28
28
|
@search.result_size.should eql(20)
|
29
29
|
end
|
30
|
+
|
31
|
+
# this is not mocked. use for real-deal testing
|
32
|
+
# describe "advanced searching" do
|
33
|
+
# it "returns some junk" do
|
34
|
+
# search = discogs::search.new('broken social scene')
|
35
|
+
# puts search.exactresults
|
36
|
+
# end
|
37
|
+
# end
|
30
38
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: discogs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Stephenson
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-09-04 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|