scrobbler-ng 2.0.5 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/VERSION.yml +1 -1
- data/lib/scrobbler/base.rb +43 -5
- data/lib/scrobbler/library.rb +7 -35
- data/lib/scrobbler/user.rb +56 -44
- data/scrobbler-ng.gemspec +2 -2
- data/test/mocks/rest.rb +1 -1
- metadata +4 -4
data/.gitignore
CHANGED
data/VERSION.yml
CHANGED
data/lib/scrobbler/base.rb
CHANGED
@@ -19,6 +19,10 @@ module Scrobbler
|
|
19
19
|
# By default, there is no cache
|
20
20
|
@@cache = []
|
21
21
|
|
22
|
+
# Add a cache provider to the caching system
|
23
|
+
#
|
24
|
+
# @param [CacheProvider] cache A instance of a cache provider.
|
25
|
+
# @return [void]
|
22
26
|
def Base.add_cache(cache)
|
23
27
|
@@cache << cache
|
24
28
|
end
|
@@ -28,7 +32,7 @@ module Scrobbler
|
|
28
32
|
# This key will be used by all Scrobbler classes and objects.
|
29
33
|
#
|
30
34
|
# @param [String] api_key The default API key.
|
31
|
-
# @return [
|
35
|
+
# @return [void]
|
32
36
|
def Base.api_key=(api_key)
|
33
37
|
@@api_key = api_key
|
34
38
|
end
|
@@ -38,7 +42,7 @@ module Scrobbler
|
|
38
42
|
# This secret will be used by all Scrobbler classes and objects.
|
39
43
|
#
|
40
44
|
# @param [String] secret The default API secret.
|
41
|
-
# @return [
|
45
|
+
# @return [void]
|
42
46
|
def Base.secret=(secret)
|
43
47
|
@@secret = secret
|
44
48
|
end
|
@@ -85,7 +89,7 @@ module Scrobbler
|
|
85
89
|
# Load a request from cache if possible
|
86
90
|
#
|
87
91
|
# @param [Hash] parameters The parameters passed as URL params.
|
88
|
-
# @return [String
|
92
|
+
# @return [String]
|
89
93
|
def Base.load_from_cache(parameters)
|
90
94
|
@@cache.each do |cache|
|
91
95
|
if cache.has?(parameters)
|
@@ -99,7 +103,7 @@ module Scrobbler
|
|
99
103
|
#
|
100
104
|
# @param [String] xml The answer from the Last.fm API
|
101
105
|
# @param [Hash] parameters The parameters passed as URL params.
|
102
|
-
# @return [
|
106
|
+
# @return [void]
|
103
107
|
def Base.save_to_cache(xml, parameters)
|
104
108
|
@@cache.each do |cache|
|
105
109
|
if cache.writable? then
|
@@ -184,7 +188,7 @@ module Scrobbler
|
|
184
188
|
# Load information into instance variables.
|
185
189
|
#
|
186
190
|
# @param [Hash<String,Symbol>] data Each entry will be stored as a variable.
|
187
|
-
# @return [
|
191
|
+
# @return [void]
|
188
192
|
def populate_data(data = {})
|
189
193
|
data.each do |key, value|
|
190
194
|
instance_variable_set("@#{key.to_s}", value)
|
@@ -199,6 +203,40 @@ module Scrobbler
|
|
199
203
|
def request(api_method, parameters = {}, request_method = 'get')
|
200
204
|
Base.request(api_method, parameters, request_method)
|
201
205
|
end
|
206
|
+
|
207
|
+
# Generic request method for the most Library funtions
|
208
|
+
#
|
209
|
+
# @param [String,Symbol] api_method The method which shall be called.
|
210
|
+
# @param [Hash] options The parameters passed as URL params.
|
211
|
+
# @param [String,Symbol] parent the parent XML node to look for.
|
212
|
+
# @param [Class] element The xml node name which shall be converted
|
213
|
+
# into an object.
|
214
|
+
# @return [Array]
|
215
|
+
def call_pageable(method, parent, element, options={})
|
216
|
+
options = {:all => true}.merge options
|
217
|
+
result = []
|
218
|
+
if options.delete(:all)
|
219
|
+
doc = Base.request(method, options)
|
220
|
+
root = nil
|
221
|
+
doc.root.children.each do |child|
|
222
|
+
next unless child.name == parent.to_s
|
223
|
+
root = child
|
224
|
+
end
|
225
|
+
total_pages = root['totalPages'].to_i
|
226
|
+
root.children.each do |child|
|
227
|
+
next unless child.name == element.to_s.sub("Scrobbler::","").downcase
|
228
|
+
result << element.new_from_libxml(child)
|
229
|
+
end
|
230
|
+
(2..total_pages).each do |i|
|
231
|
+
options[:page] = i
|
232
|
+
result.concat call(method, parent, element, options)
|
233
|
+
end
|
234
|
+
else
|
235
|
+
result = call(method, parent, element, options)
|
236
|
+
end
|
237
|
+
result
|
238
|
+
end
|
239
|
+
|
202
240
|
|
203
241
|
# Call a API method
|
204
242
|
#
|
data/lib/scrobbler/library.rb
CHANGED
@@ -40,8 +40,10 @@ module Scrobbler
|
|
40
40
|
|
41
41
|
# A list of all the albums in a user's library, with play counts and tag
|
42
42
|
# counts.
|
43
|
+
# @param [Hash<Symbol>] options The options to configure this API call.
|
44
|
+
# @return [Array<Scrobbler::Tracks>] The artists included in this library.
|
43
45
|
def albums(options={})
|
44
|
-
|
46
|
+
call_pageable('library.getalbums', :albums, Album, {:user => @user.name}.merge(options))
|
45
47
|
end
|
46
48
|
|
47
49
|
# A list of all the artists in a user's library, with play counts and tag
|
@@ -50,47 +52,17 @@ module Scrobbler
|
|
50
52
|
# @param [Hash<Symbol>] options The options to configure this API call.
|
51
53
|
# @return [Array<Scrobbler::Artist>] The artists included in this library.
|
52
54
|
def artists(options={})
|
53
|
-
|
55
|
+
call_pageable('library.getartists', :artists, Artist, {:user => @user.name}.merge(options))
|
54
56
|
end
|
55
57
|
|
56
58
|
# A list of all the tracks in a user's library, with play counts and tag
|
57
59
|
# counts.
|
60
|
+
# @param [Hash<Symbol>] options The options to configure this API call.
|
61
|
+
# @return [Array<Scrobbler::Track>] The tracks included in this library.
|
58
62
|
def tracks(options={})
|
59
|
-
|
63
|
+
call_pageable('library.gettracks', :tracks, Track, {:user => @user.name}.merge(options))
|
60
64
|
end
|
61
65
|
|
62
|
-
# Generic request method for the most Library funtions
|
63
|
-
#
|
64
|
-
# @param [String,Symbol] api_method The method which shall be called.
|
65
|
-
# @param [Hash] options The parameters passed as URL params.
|
66
|
-
# @param [String,Symbol] parent the parent XML node to look for.
|
67
|
-
# @param [Class] element The xml node name which shall be converted
|
68
|
-
# into an object.
|
69
|
-
# @return [Array]
|
70
|
-
def request_library(method, parent, element, options={})
|
71
|
-
options = {:all => true, :user => @user.name}.merge options
|
72
|
-
result = []
|
73
|
-
if options.delete(:all)
|
74
|
-
doc = Base.request(method, options)
|
75
|
-
root = nil
|
76
|
-
doc.root.children.each do |child|
|
77
|
-
next unless child.name == parent.to_s
|
78
|
-
root = child
|
79
|
-
end
|
80
|
-
total_pages = root['totalPages'].to_i
|
81
|
-
root.children.each do |child|
|
82
|
-
next unless child.name == element.to_s.sub("Scrobbler::","").downcase
|
83
|
-
result << element.new_from_libxml(child)
|
84
|
-
end
|
85
|
-
(2..total_pages).each do |i|
|
86
|
-
options[:page] = i
|
87
|
-
result.concat call(method, parent, element, options)
|
88
|
-
end
|
89
|
-
else
|
90
|
-
result = call(method, parent, element, options)
|
91
|
-
end
|
92
|
-
result
|
93
|
-
end
|
94
66
|
|
95
67
|
end
|
96
68
|
end
|
data/lib/scrobbler/user.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require File.expand_path('basexmlinfo.rb', File.dirname(__FILE__))
|
4
4
|
|
5
|
-
module Scrobbler
|
5
|
+
module Scrobbler
|
6
6
|
class User < BaseXmlInfo
|
7
7
|
# Load Helper modules
|
8
8
|
include ImageObjectFuncs
|
@@ -26,7 +26,7 @@ module Scrobbler
|
|
26
26
|
# Load the data for this object out of a XML-Node
|
27
27
|
#
|
28
28
|
# @param [LibXML::XML::Node] node The XML node containing the information
|
29
|
-
# @return [
|
29
|
+
# @return [void]
|
30
30
|
def load_from_xml(node)
|
31
31
|
# Get all information from the root's children nodes
|
32
32
|
node.children.each do |child|
|
@@ -53,6 +53,9 @@ module Scrobbler
|
|
53
53
|
end #^ do |child|
|
54
54
|
end
|
55
55
|
|
56
|
+
# Create a new Scrobbler:User instance
|
57
|
+
#
|
58
|
+
# @param [Hash] data The options to initialize the class
|
56
59
|
def initialize(data={})
|
57
60
|
raise ArgumentError unless data.kind_of?(Hash)
|
58
61
|
super(data)
|
@@ -67,32 +70,12 @@ module Scrobbler
|
|
67
70
|
call('user.getevents', :events, Event, {:user => @name})
|
68
71
|
end
|
69
72
|
|
70
|
-
# Get a list of the user's friends on Last.fm.
|
73
|
+
# Get a list of the user's friends on Last.fm.
|
74
|
+
#
|
75
|
+
# @param [Hash] opts Parameters for the API request
|
76
|
+
# @return [Array<Scrobbler::User>]
|
71
77
|
def friends(opts={})
|
72
|
-
|
73
|
-
if opts.delete :all
|
74
|
-
params = {:page => 1, :limit => 50, :user => @name}.merge(opts)
|
75
|
-
doc = Base.request('user.getfriends', params)
|
76
|
-
root = nil
|
77
|
-
doc.root.children.each do |child|
|
78
|
-
next unless child.name == 'friends'
|
79
|
-
root = child
|
80
|
-
end
|
81
|
-
total_pages = root['totalPages'].to_i
|
82
|
-
root.children.each do |child|
|
83
|
-
next unless child.name == 'user'
|
84
|
-
result << Scrobbler::User.new_from_libxml(child)
|
85
|
-
end
|
86
|
-
puts total_pages
|
87
|
-
(2..total_pages).each do |i|
|
88
|
-
params[:page] = i
|
89
|
-
result.concat call('user.getfriends', :friends, User, params)
|
90
|
-
end
|
91
|
-
else
|
92
|
-
params = {:page => 1, :limit => 50, :user => @name}.merge(opts)
|
93
|
-
result = call('user.getfriends', :friends, User, params)
|
94
|
-
end
|
95
|
-
result
|
78
|
+
call_pageable('user.getfriends', :friends, User, {:user => @name}.merge(opts))
|
96
79
|
end
|
97
80
|
|
98
81
|
# Get information about a user profile.
|
@@ -102,22 +85,28 @@ module Scrobbler
|
|
102
85
|
end
|
103
86
|
|
104
87
|
# Get the last 50 tracks loved by a user.
|
88
|
+
#
|
89
|
+
# @return [Array<Scrobbler::Track>]
|
105
90
|
def loved_tracks
|
106
91
|
call('user.getlovedtracks', :lovedtracks, Track, {:user => @name})
|
107
92
|
end
|
108
93
|
|
109
94
|
# Get a list of a user's neighbours on Last.fm.
|
95
|
+
#
|
96
|
+
# @return [Array<Scrobbler::Track>]
|
110
97
|
def neighbours
|
111
98
|
call('user.getneighbours', :neighbours, User, {:user => @name})
|
112
99
|
end
|
113
100
|
|
114
|
-
# Get a paginated list of all events a user has attended in the past.
|
101
|
+
# Get a paginated list of all events a user has attended in the past.
|
115
102
|
def past_events(format=:ics)
|
116
103
|
# This needs a Event class, which is yet not available
|
117
104
|
raise NotImplementedError
|
118
105
|
end
|
119
106
|
|
120
107
|
# Get a list of a user's playlists on Last.fm.
|
108
|
+
#
|
109
|
+
# @return [Array<Scrobbler::Playlist>]
|
121
110
|
def playlists
|
122
111
|
call('user.getplaylists', :playlists, Playlist, {:user => @name})
|
123
112
|
end
|
@@ -125,14 +114,15 @@ module Scrobbler
|
|
125
114
|
# Get a list of the recent tracks listened to by this user. Indicates now
|
126
115
|
# playing track if the user is currently listening.
|
127
116
|
#
|
128
|
-
#
|
129
|
-
#
|
117
|
+
# @param [Hash] parameters
|
118
|
+
# @return [Array<Scrobbler::Track>]
|
130
119
|
def recent_tracks(parameters={})
|
131
|
-
|
132
|
-
call('user.getrecenttracks', :recenttracks, Track, parameters)
|
120
|
+
call('user.getrecenttracks', :recenttracks, Track, {:user => @name}.merge(parameters))
|
133
121
|
end
|
134
122
|
|
135
123
|
# Get Last.fm artist recommendations for a user
|
124
|
+
#
|
125
|
+
# @return [Array<Scrobbler::Artist>]
|
136
126
|
def recommended_artists
|
137
127
|
# This function require authentication, but SimpleAuth is not yet 2.0
|
138
128
|
raise NotImplementedError
|
@@ -152,50 +142,69 @@ module Scrobbler
|
|
152
142
|
end
|
153
143
|
|
154
144
|
# Get the top albums listened to by a user. You can stipulate a time period.
|
155
|
-
# Sends the overall chart by default.
|
145
|
+
# Sends the overall chart by default.
|
146
|
+
#
|
147
|
+
# @return [Array<Scrobbler::Album>]
|
156
148
|
def top_albums(period=:overall)
|
157
149
|
call('user.gettopalbums', :topalbums, Album, {:user => @name, :period => period})
|
158
150
|
end
|
159
151
|
|
160
152
|
# Get the top artists listened to by a user. You can stipulate a time
|
161
|
-
# period. Sends the overall chart by default.
|
153
|
+
# period. Sends the overall chart by default.
|
154
|
+
#
|
155
|
+
# @return [Array<Scrobbler::Artist>]
|
162
156
|
def top_artists(period=:overall)
|
163
157
|
call('user.gettopartists', :topartists, Artist, {:user => @name, :period => period})
|
164
158
|
end
|
165
159
|
|
166
|
-
#
|
160
|
+
# Get the top tags used by this user.
|
161
|
+
#
|
162
|
+
# @return [Array<Scrobbler::Tag>]
|
167
163
|
def top_tags
|
168
164
|
call('user.gettoptags', :toptags, Tag, {:user => @name})
|
169
165
|
end
|
170
166
|
|
171
167
|
# Get the top tracks listened to by a user. You can stipulate a time period.
|
172
|
-
# Sends the overall chart by default.
|
168
|
+
# Sends the overall chart by default.
|
169
|
+
#
|
170
|
+
# @return [Array<Scrobbler::Track>]
|
173
171
|
def top_tracks(period=:overall)
|
174
172
|
call('user.gettoptracks', :toptracks, Track, {:user => @name, :period => period})
|
175
173
|
end
|
176
174
|
|
177
175
|
# Setup the parameters for a *chart API call
|
178
|
-
|
176
|
+
#
|
177
|
+
# @param [Class] type
|
178
|
+
# @return [Array]
|
179
|
+
def get_chart(type, from, to)
|
179
180
|
parameters = {:user => @name}
|
180
181
|
parameters[:from] = from unless from.nil?
|
181
182
|
parameters[:to] = to unless to.nil?
|
182
|
-
|
183
|
+
downType = type.to_s.sub("Scrobbler::","").downcase
|
184
|
+
call('user.getweekly'+ downType + 'chart',
|
185
|
+
'weekly' + downType + 'chart', type, parameters)
|
183
186
|
end
|
184
187
|
|
185
188
|
# Get an album chart for a user profile, for a given date range. If no date
|
186
189
|
# range is supplied, it will return the most recent album chart for this
|
187
190
|
# user.
|
191
|
+
#
|
192
|
+
# @param [int] from Starttime
|
193
|
+
# @param [int] to Endtime
|
194
|
+
# @return [Array<Scrobbler::Album>]
|
188
195
|
def weekly_album_chart(from=nil, to=nil)
|
189
|
-
|
190
|
-
call('user.getweeklyalbumchart', :weeklyalbumchart, Album, parameters)
|
196
|
+
get_chart(Album, from, to)
|
191
197
|
end
|
192
198
|
|
193
199
|
# Get an artist chart for a user profile, for a given date range. If no date
|
194
200
|
# range is supplied, it will return the most recent artist chart for this
|
195
201
|
# user.
|
202
|
+
#
|
203
|
+
# @param [int] from Starttime
|
204
|
+
# @param [int] to Endtime
|
205
|
+
# @return [Array<Scrobbler::Artist>]
|
196
206
|
def weekly_artist_chart(from=nil, to=nil)
|
197
|
-
|
198
|
-
call('user.getweeklyartistchart', :weeklyartistchart, Artist, parameters)
|
207
|
+
get_chart(Artist, from, to)
|
199
208
|
end
|
200
209
|
|
201
210
|
# Get a list of available charts for this user, expressed as date ranges
|
@@ -208,9 +217,12 @@ module Scrobbler
|
|
208
217
|
# Get a track chart for a user profile, for a given date range. If no date
|
209
218
|
# range is supplied, it will return the most recent track chart for this
|
210
219
|
# user.
|
220
|
+
#
|
221
|
+
# @param [int] from Starttime
|
222
|
+
# @param [int] to Endtime
|
223
|
+
# @return [Array<Scrobbler::Track>]
|
211
224
|
def weekly_track_chart(from=nil, to=nil)
|
212
|
-
|
213
|
-
call('user.getweeklytrackchart', :weeklytrackchart, Track, parameters)
|
225
|
+
get_chart(Track, from, to)
|
214
226
|
end
|
215
227
|
|
216
228
|
# Shout on this user's shoutbox
|
data/scrobbler-ng.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{scrobbler-ng}
|
8
|
-
s.version = "2.0.
|
8
|
+
s.version = "2.0.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Nunemaker", "Jonathan Rudenberg", "Uwe L. Korn"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-08-05}
|
13
13
|
s.description = %q{A ruby library for accessing the Last.fm 2.0 API. It is higly optimized so that it uses less memory and parses XML (through Nokogiri) than other implementations.}
|
14
14
|
s.email = %q{uwelk@xhochy.org}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/mocks/rest.rb
CHANGED
@@ -103,7 +103,7 @@ register_fw('user=jnunemaker&period=overall&api_key=foo123&method=user.gettopalb
|
|
103
103
|
'user', 'topalbums.xml')
|
104
104
|
register_fw('user=jnunemaker&api_key=foo123&method=user.getneighbours',
|
105
105
|
'user', 'neighbours.xml')
|
106
|
-
register_fw('user=jnunemaker&
|
106
|
+
register_fw('user=jnunemaker&api_key=foo123&method=user.getfriends',
|
107
107
|
'user', 'friends.xml')
|
108
108
|
register_fw('user=jnunemaker&api_key=foo123&method=user.getrecenttracks',
|
109
109
|
'user', 'recenttracks.xml')
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scrobbler-ng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 2.0.
|
9
|
+
- 6
|
10
|
+
version: 2.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Nunemaker
|
@@ -17,7 +17,7 @@ autorequire:
|
|
17
17
|
bindir: bin
|
18
18
|
cert_chain: []
|
19
19
|
|
20
|
-
date: 2010-
|
20
|
+
date: 2010-08-05 00:00:00 +02:00
|
21
21
|
default_executable:
|
22
22
|
dependencies:
|
23
23
|
- !ruby/object:Gem::Dependency
|