rockstar 0.6.3 → 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,12 +14,16 @@ Please initialize your api key and secret before using the api:
14
14
  Here is an example lastfm.yml:
15
15
 
16
16
  api_key: "API"
17
- api_secret: "SECRET"
17
+ api_secret: "SECRET"
18
18
 
19
19
  If you want to use the api in an rails app, you could add an initializer in config/initializers/lastm.rb and load a config/lastfm.yml file.
20
20
 
21
21
  rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
22
22
  Rockstar.lastfm = YAML.load_file(rails_root + '/config/lastfm.yml')
23
+
24
+ Alternatively you can provide a hash with the api key and secret:
25
+
26
+ Rockstar.lastfm = {:api_key => "API", :api_secret => "SECRET"}
23
27
 
24
28
  ## Users
25
29
 
@@ -35,9 +39,9 @@ If you want to use the api in an rails app, you could add an initializer in conf
35
39
  puts "#{user.username}'s Top Tracks"
36
40
  puts "=" * (user.username.length + 13)
37
41
  user.top_tracks.each { |t| puts "(#{t.playcount}) #{t.name}" }
38
-
42
+
39
43
  ## Albums
40
-
44
+
41
45
  album = Rockstar::Album.new('Carrie Underwood', 'Some Hearts', :include_info => true)
42
46
 
43
47
  puts "Album: #{album.name}"
@@ -46,8 +50,13 @@ If you want to use the api in an rails app, you could add an initializer in conf
46
50
  puts "Release Date: #{album.release_date.strftime('%m/%d/%Y')}"
47
51
 
48
52
  ## Artists
49
-
50
- artist = Rockstar::Artist.new('Carrie Underwood')
53
+
54
+ If you want to load all informations about an artist or any other
55
+ object, you have to add `:include_info => true` .
56
+
57
+ artist = Rockstar::Artist.new('Carrie Underwood', :include_info => true)
58
+
59
+ puts artist.url
51
60
 
52
61
  puts 'Top Tracks'
53
62
  puts "=" * 10
@@ -58,9 +67,9 @@ If you want to use the api in an rails app, you could add an initializer in conf
58
67
  puts 'Similar Artists'
59
68
  puts "=" * 15
60
69
  artist.similar.each { |a| puts "(#{a.match}%) #{a.name}" }
61
-
62
- ## Tags
63
-
70
+
71
+ ## Tags
72
+
64
73
  tag = Rockstar::Tag.new('country')
65
74
 
66
75
  puts 'Top Albums'
@@ -70,28 +79,17 @@ If you want to use the api in an rails app, you could add an initializer in conf
70
79
 
71
80
  puts 'Top Tracks'
72
81
  tag.top_tracks.each { |t| puts "(#{t.count}) #{t.name} by #{t.artist}" }
73
-
74
- ## Tracks
75
-
76
- track = Rockstar::Track.new('Carrie Underwood', 'Before He Cheats')
77
- puts 'Fans'
78
- puts "=" * 4
79
- track.fans.each { |u| puts "(#{u.weight}) #{u.username}" }
80
-
81
- # Love a song, session_key is returned by Rockstar::Auth. See Rockstar::TokenAuth or
82
- # examples/scrobble.rb for a complete example
83
- track.love(session_key)
84
82
 
85
83
  ## Geo
86
84
 
87
85
  geo = Rockstar::Geo.new
88
-
89
- # Get events for a lat/long
86
+
87
+ # Get events for a lat/long
90
88
  geo.events(:lat => 50.0, :long => 12.3).each{|e| p "#{e.title} at #{e.venue.name}"}
91
-
89
+
92
90
  # Get events for a location
93
91
  geo.events(:location => 'london').each{|e| p "#{e.title} at #{e.venue.name}"}
94
-
92
+
95
93
  # To get a list of possible locations use
96
94
  geo.metros("germany").each{|m| p m.name}
97
95
 
@@ -100,17 +98,27 @@ If you want to use the api in an rails app, you could add an initializer in conf
100
98
 
101
99
  a = Rockstar::Auth.new
102
100
  token = a.token
103
-
101
+
104
102
  puts
105
103
  puts "Please open http://www.last.fm/api/auth/?api_key=#{Rockstar.lastfm_api_key}&token=#{token}"
106
104
  puts
107
105
  puts "Press enter when done."
108
-
106
+
109
107
  gets
110
-
108
+
111
109
  session = a.session(token)
112
110
 
113
- You can store the session.key somewhere and use it from now on to identify the user.
111
+ You can store the session.key somewhere and use it from now on to identify the user. More
112
+ details can be found in Rockstar::Auth or examples/scrobble.rb
113
+
114
+ ## Tracks
115
+
116
+ track = Rockstar::Track.new('Carrie Underwood', 'Before He Cheats')
117
+ puts 'Fans'
118
+ puts "=" * 4
119
+ track.fans.each { |u| puts "(#{u.weight}) #{u.username}" }
120
+
121
+ track.love(session.key)
114
122
 
115
123
  ## Scrobbling
116
124
 
@@ -123,7 +131,7 @@ You can store the session.key somewhere and use it from now on to identify the u
123
131
  track.updateNowPlaying(Time.now, session.key)
124
132
 
125
133
  ## Note on Patches/Pull Requests
126
-
134
+
127
135
  * Fork the project.
128
136
  * Make your feature addition or bug fix.
129
137
  * Add tests for it. This is important so I don't break it in a
@@ -135,3 +143,7 @@ You can store the session.key somewhere and use it from now on to identify the u
135
143
  ## Copyright
136
144
 
137
145
  Copyright (c) 2007-2011 John Nunemaker, Bodo "Bitboxer" Tasche, Nick "zapnap" Plante. See LICENSE for details.
146
+
147
+ ## Acknowledgements
148
+
149
+ Thanks to [putpat.tv](http://www.putpat.tv) for sponsering the work on the rockstar gem.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.3
1
+ 0.6.4
data/examples/artist.rb CHANGED
@@ -4,7 +4,9 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'rocksta
4
4
  # You can find them here : http://www.lastfm.de/api/account
5
5
  Rockstar.lastfm = YAML.load_file(File.join(File.dirname(__FILE__), 'lastfm.yml'))
6
6
 
7
- artist = Rockstar::Artist.new('Metallica')
7
+ artist = Rockstar::Artist.new('Metallica', :include_info => true)
8
+
9
+ puts artist.url
8
10
 
9
11
  puts 'Top Tracks'
10
12
  puts "=" * 10
data/lib/rockstar.rb CHANGED
@@ -23,27 +23,22 @@ require 'rockstar/tag'
23
23
  require 'rockstar/track'
24
24
  require 'rockstar/venue'
25
25
 
26
- require 'rockstar/simpleauth'
27
26
  require 'rockstar/auth'
28
27
  require 'rockstar/session'
29
- require 'rockstar/tokenauth'
30
- require 'rockstar/scrobble'
31
- require 'rockstar/playing'
32
-
33
28
  require 'rockstar/rest'
34
29
 
35
30
  module Rockstar
36
31
  extend self
37
-
32
+
38
33
  def lastfm=(args)
39
- @api_key = args["api_key"]
40
- @api_secret = args["api_secret"]
34
+ @api_key = args["api_key"] || args[:api_key]
35
+ @api_secret = args["api_secret"] || args[:api_secret]
41
36
  end
42
-
37
+
43
38
  def lastfm_api_key
44
39
  @api_key
45
40
  end
46
-
41
+
47
42
  def lastfm_api_secret
48
43
  @api_secret
49
44
  end
@@ -1,35 +1,35 @@
1
1
  # Getting information about an album such as release date and the summary or description on it is very easy.
2
- #
2
+ #
3
3
  # album = Rockstar::Album.new('Carrie Underwood', 'Some Hearts', :include_info => true)
4
- #
4
+ #
5
5
  # puts "Album: #{album.name}"
6
6
  # puts "Artist: #{album.artist}"
7
7
  # puts "URL: #{album.url}"
8
8
  # puts "Release Date: #{album.release_date.strftime('%m/%d/%Y')}"
9
- #
9
+ #
10
10
  # Would output:
11
11
  #
12
12
  # Album: Some Hearts
13
13
  # Artist: Carrie Underwood
14
14
  # URL: http://www.last.fm/music/Carrie+Underwood/Some+Hearts
15
15
  # Release Date: 11/15/2005
16
- #
16
+ #
17
17
  module Rockstar
18
18
  class Album < Base
19
19
  attr_accessor :artist, :artist_mbid, :name, :mbid, :playcount, :rank, :url, :release_date
20
20
  attr_accessor :image_large, :image_medium, :image_small, :summary, :content, :images
21
-
21
+
22
22
  # needed on top albums for tag
23
23
  attr_accessor :count, :streamable
24
-
24
+
25
25
  # needed for weekly album charts
26
26
  attr_accessor :chartposition
27
-
27
+
28
28
  class << self
29
29
  def find(artist, name, o={})
30
30
  new(artist, name, o)
31
31
  end
32
-
32
+
33
33
  def new_from_xml(xml, doc=nil)
34
34
  name = (xml).at(:name).inner_html if (xml).at(:name)
35
35
  name = xml['name'] if name.nil? && xml['name']
@@ -53,7 +53,7 @@ module Rockstar
53
53
  options = {:include_info => false}.merge(o)
54
54
  load_info if options[:include_info]
55
55
  end
56
-
56
+
57
57
  def load_info(xml=nil)
58
58
  unless xml
59
59
  doc = self.class.fetch_and_parse("album.getInfo", {:artist => @artist, :album =>@name})
@@ -61,7 +61,7 @@ module Rockstar
61
61
  end
62
62
 
63
63
  return self if xml.nil?
64
-
64
+
65
65
  self.artist_mbid = (xml).at(:artist)['mbid'] if (xml).at(:artist) && (xml).at(:artist)['mbid']
66
66
  self.artist_mbid = (xml).at(:artist).at(:mbid).inner_html if artist_mbid.nil? && (xml).at(:artist) && (xml).at(:artist).at(:mbid)
67
67
  self.mbid = (xml).at(:mbid).inner_html if (xml).at(:mbid)
@@ -75,16 +75,16 @@ module Rockstar
75
75
 
76
76
  self.release_date = Base.parse_time((xml).at(:releasedate).inner_html.strip) if (xml).at(:releasedate)
77
77
  self.chartposition = rank
78
-
78
+
79
79
  self.images = {}
80
80
  (xml/'image').each {|image|
81
81
  self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
82
82
  }
83
-
83
+
84
84
  self.image_large = images['large']
85
85
  self.image_medium = images['medium']
86
86
  self.image_small = images['small']
87
-
87
+
88
88
  # needed on top albums for tag
89
89
  self.count = xml['count'] if xml['count']
90
90
  self.streamable = xml['streamable'] if xml['streamable']
@@ -92,17 +92,12 @@ module Rockstar
92
92
  self
93
93
  end
94
94
 
95
- def tracks
96
- warn "[DEPRECATION] `tracks` is deprecated. The current api doesn't offer this function"
97
- []
98
- end
99
-
100
95
  def image(which=:small)
101
96
  which = which.to_s
102
- raise ArgumentError unless ['small', 'medium', 'large', 'extralarge'].include?(which)
97
+ raise ArgumentError unless ['small', 'medium', 'large', 'extralarge'].include?(which)
103
98
  if (self.images.nil?)
104
99
  load_info
105
- end
100
+ end
106
101
  self.images[which]
107
102
  end
108
103
  end
@@ -1,19 +1,19 @@
1
1
  # Below are examples of how to find an artists top tracks and similar artists.
2
- #
2
+ #
3
3
  # artist = Rockstar::Artist.new('Carrie Underwood')
4
- #
4
+ #
5
5
  # puts 'Top Tracks'
6
6
  # puts "=" * 10
7
7
  # artist.top_tracks.each { |t| puts "#{t.name}" }
8
- #
8
+ #
9
9
  # puts
10
- #
10
+ #
11
11
  # puts 'Similar Artists'
12
12
  # puts "=" * 15
13
13
  # artist.similar.each { |a| puts "(#{a.match}%) #{a.name}" }
14
- #
14
+ #
15
15
  # Would output something similar to:
16
- #
16
+ #
17
17
  # Top Tracks
18
18
  # ==========
19
19
  # (8797) Before He Cheats
@@ -31,7 +31,7 @@
31
31
  # (1854) That's Where It Is
32
32
  # (1786) I Ain't in Checotah Anymore
33
33
  # (1596) The Night Before (Life Goes On)
34
- #
34
+ #
35
35
  # Similar Artists
36
36
  # ===============
37
37
  # (100%) Rascal Flatts
@@ -61,10 +61,10 @@ module Rockstar
61
61
  attr_accessor :name, :mbid, :listenercount, :playcount, :rank, :url, :thumbnail
62
62
  attr_accessor :summary, :content, :images, :count, :streamable
63
63
  attr_accessor :chartposition
64
-
64
+
65
65
  # used for similar artists
66
66
  attr_accessor :match
67
-
67
+
68
68
  class << self
69
69
  def new_from_xml(xml, doc=nil)
70
70
  # occasionally name can be found in root of artist element (<artist name="">) rather than as an element (<name>)
@@ -76,7 +76,7 @@ module Rockstar
76
76
  artist
77
77
  end
78
78
  end
79
-
79
+
80
80
  def initialize(name, o={})
81
81
  raise ArgumentError, "Name or mbid is required" if name.blank? && o[:mbid].blank?
82
82
  @name = name
@@ -89,7 +89,7 @@ module Rockstar
89
89
  def load_info(xml=nil)
90
90
  unless xml
91
91
  params = @mbid.blank? ? {:artist => @name} : {:mbid => @mbid}
92
-
92
+
93
93
  doc = self.class.fetch_and_parse("artist.getInfo", params)
94
94
  xml = (doc / :artist).first
95
95
  end
@@ -112,10 +112,10 @@ module Rockstar
112
112
  (xml/'image').each {|image|
113
113
  self.images[image['size']] = image.inner_html if self.images[image['size']].nil?
114
114
  }
115
-
115
+
116
116
  self.thumbnail = self.images['small']
117
117
  self.match = (xml).at(:match).inner_html if (xml).at(:match)
118
-
118
+
119
119
  # in top artists for tag
120
120
  self.count = xml['count'] if xml['count']
121
121
  self.streamable = xml['streamable'] if xml['streamable']
@@ -124,32 +124,27 @@ module Rockstar
124
124
 
125
125
  self
126
126
  end
127
-
128
- def current_events(format=:ics)
129
- warn "[DEPRECATION] the `current_events` method is deprecated. Please use artist.events"
130
- events
131
- end
132
127
 
133
128
  def events(force=false)
134
129
  get_instance("artist.getEvents", :events, :event, {:artist => @name}, force)
135
130
  end
136
-
131
+
137
132
  def similar(force=false)
138
133
  get_instance("artist.getSimilar", :similar, :artist, {:artist => @name}, force)
139
134
  end
140
-
135
+
141
136
  def top_fans(force=false)
142
137
  get_instance("artist.getTopFans", :top_fans, :user, {:artist => @name}, force)
143
138
  end
144
-
139
+
145
140
  def top_tracks(force=false)
146
141
  get_instance("artist.getTopTracks", :top_tracks, :track, {:artist => @name}, force)
147
142
  end
148
-
143
+
149
144
  def top_albums(force=false)
150
145
  get_instance("artist.getTopAlbums", :top_albums, :album, {:artist => @name}, force)
151
146
  end
152
-
147
+
153
148
  def top_tags(force=false)
154
149
  get_instance("artist.getTopTags", :top_tags, :tag, {:artist => @name}, force)
155
150
  end
data/lib/rockstar/auth.rb CHANGED
@@ -1,17 +1,34 @@
1
1
  module Rockstar
2
-
2
+
3
+ # = Authentification
4
+ #
5
+ # There are 2 ways to get an auth token :
6
+ #
7
+ # == Desktop-App
8
+ # 1. Get a new token to request authorisation:
9
+ # token = Rockstar::Auth.new.token
10
+ # 2. Open a webbrowser with http://www.last.fm/api/auth/?api_key=xxxxxxxxxxx&token=xxxxxxxx
11
+ # 3. Wait for the User to confirm that he accepted your request.
12
+ # 4. Continue with "Get the session token"
13
+ #
14
+ # == Web-App
15
+ # 1. Redirect the user to http://www.last.fm/api/auth/?api_key={YOUR_API_KEY}&amp;cb={YOUR_RETURN_URL}
16
+ # 2. If the user accepts, lastfm will redirect to YOUR_RETURN_URL?token=TOKEN
17
+ # token = params[:token]
18
+ # 3. Continue with "Get the session token"
19
+ #
20
+ # == Get the session token
21
+ # 1. Use the previous token and call
22
+ # session = Rockstar::Auth.new.session(token)
23
+ # 2. Store the session.key and session.username returned. The session.key will not
24
+ # expire. It is save to store it into your database.
3
25
  class Auth < Base
4
- # Returns the token for a session. You have to use
5
- # TokenAuth first and then use this class with the token
6
- # that is given by last.fm to create a session token
7
- # This session token can be stored in your database, it is
8
- # not expiring.
9
- # See Rockstar::TokenAuth for a detailed example
26
+
10
27
  def session(token)
11
28
  doc = self.class.fetch_and_parse("auth.getSession", {:token => token}, true)
12
29
  Rockstar::Session.new_from_xml(doc)
13
30
  end
14
-
31
+
15
32
  def token
16
33
  doc = self.class.fetch_and_parse("auth.getToken", {}, true)
17
34
  token = (doc).at(:token).inner_html if (doc).at(:token)
data/lib/rockstar/base.rb CHANGED
@@ -1,30 +1,30 @@
1
- module Rockstar
2
-
1
+ module Rockstar
2
+
3
3
  API_URL = 'http://ws.audioscrobbler.com/'
4
4
  API_VERSION = '2.0'
5
-
5
+
6
6
  class Base
7
7
  class << self
8
8
  def connection
9
9
  @connection ||= REST::Connection.new(API_URL + API_VERSION + "/")
10
10
  end
11
-
11
+
12
12
  def fetch_and_parse(resource, params = {}, sign_request = false)
13
13
  Hpricot::XML(connection.get(resource, sign_request, params))
14
14
  end
15
-
15
+
16
16
  def get_instance(api_method, instance_name, element, params, sign_request = false)
17
17
  rockstar_class = "rockstar/#{element.to_s}".camelize.constantize
18
18
  doc = self.fetch_and_parse(api_method, params, sign_request)
19
19
  (doc/element).inject([]) { |elements, el| elements << rockstar_class.new_from_xml(el, doc); elements }
20
20
  end
21
21
  end
22
-
22
+
23
23
  private
24
24
  # in order for subclass to use, it must have api_path method
25
25
  def get_instance(api_method, instance_name, element, params, force = false, sign_request = false)
26
26
  rockstar_class = "rockstar/#{element.to_s}".camelize.constantize
27
-
27
+
28
28
  if instance_variable_get("@#{instance_name}").nil? || force
29
29
  doc = self.class.fetch_and_parse(api_method, params, sign_request)
30
30
  elements = (doc/element).inject([]) { |elements, el| elements << rockstar_class.new_from_xml(el, doc); elements }
@@ -32,7 +32,7 @@ module Rockstar
32
32
  end
33
33
  instance_variable_get("@#{instance_name}")
34
34
  end
35
-
35
+
36
36
  def self.fix_url(url)
37
37
  if (!url.blank? && !url.match("^http://"))
38
38
  url = "http://#{url}"