Class: SimpleScrobbler

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_scrobbler.rb,
lib/simple_scrobbler/version.rb

Defined Under Namespace

Modules: DocHelper

Constant Summary collapse

CLIENT_ID =
"tst"
CLIENT_VERSION =
"1.0"
HandshakeError =
Class.new(RuntimeError)
SubmissionError =
Class.new(RuntimeError)
DataError =
Class.new(RuntimeError)
SessionError =
Class.new(RuntimeError)
VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret, user, session_key = nil) ⇒ SimpleScrobbler

Instantiate a new SimpleScrobbler instance. If the session key is not supplied, it must be fetched using fetch_session_key before scrobbling is attempted.

Your own API key and secret can be obtained from www.last.fm/api/account



25
26
27
28
29
30
31
# File 'lib/simple_scrobbler.rb', line 25

def initialize(api_key, secret, user, session_key=nil)
  @api_key     = api_key
  @secret      = secret
  @user        = user
  @session_key = session_key
  @source      = "P"
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



33
34
35
# File 'lib/simple_scrobbler.rb', line 33

def user
  @user
end

Instance Method Details

#fetch_session_key {|"http://www.last.fm/api/auth/?api_key=#{api_key}&token=#{request_token}"| ... } ⇒ Object

Fetch the auth key needed for the application. This can be stored and supplied in the constructor on future occasions.

Yields a URL which the user must visit. The block should not return until this is done.

Yields:

  • ("http://www.last.fm/api/auth/?api_key=#{api_key}&token=#{request_token}")


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_scrobbler.rb', line 59

def fetch_session_key(&blk)
  doc = last_fm_web_service("method" => "auth.gettoken")
  request_token = doc.value_at("//token")

  yield "http://www.last.fm/api/auth/?api_key=#{api_key}&token=#{request_token}"

  doc = last_fm_web_service("method" => "auth.getsession", "token" => request_token)

  @session_key = doc.value_at("//key")
  @user        = doc.value_at("//name")

  @session_key
end

#handshakeObject

Perform handshake with the API.

There is usually no need to call this, as it will be called automatically the first time a track is scrobbled.

Raises:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/simple_scrobbler.rb', line 129

def handshake
  return if @scrobble_session_id
  timestamp = Time.now.utc.to_i.to_s
  authentication_token = md5(secret + timestamp)
  parameters = {
    "hs"      => "true",
    "p"       => "1.2.1",
    "c"       => CLIENT_ID,
    "v"       => CLIENT_VERSION,
    "u"       => user,
    "t"       => timestamp,
    "a"       => authentication_token,
    "api_key" => api_key,
    "sk"      => session_key }
  body = get("http://post.audioscrobbler.com/", parameters)
  status,
  @scrobble_session_id,
  @now_playing_url,
  @submission_url, =
    split_plain_text_response(body)
  raise HandshakeError, status unless status == "OK"
end

#now_playing(artist, track, options = {}) ⇒ Object

“The Now-Playing notification is a lightweight mechanism for notifying Last.fm that a track has started playing. This is used for realtime display of a user’s currently playing track, and does not affect a user’s musical profile.”

The artist and track are required parameters. Other parameters can be added as options:

:length

Length of the track in seconds

:album

Album title

:track_number

Track number

:mb_trackid

MusicBrainz Track ID

Raises:



114
115
116
117
118
119
120
121
122
# File 'lib/simple_scrobbler.rb', line 114

def now_playing(artist, track, options={})
  enforce_keys options, :length, :album, :track_number, :mb_trackid
  handshake
  parameters =
    generate_scrobbling_parameters(false,
      options.merge(:artist => artist, :track => track))
  status, = split_plain_text_response(post(@now_playing_url, parameters))
  raise SubmissionError, status unless status == "OK"
end

#session_keyObject



36
37
38
# File 'lib/simple_scrobbler.rb', line 36

def session_key
  @session_key or raise SessionError, "The session key must be set or fetched"
end

#source=(a) ⇒ Object

The source of the track. Required, must be one of the following codes:

P

Chosen by the user.

R

Non-personalised broadcast (e.g. Shoutcast, BBC Radio 1).

E

Personalised recommendation except Last.fm (e.g. Pandora, Launchcast).

L

Last.fm (any mode).



46
47
48
49
50
51
# File 'lib/simple_scrobbler.rb', line 46

def source=(a)
  unless %w[ P R E L ].include?(a)
    raise DataError, "source must be one of P, R, E, L (see http://www.last.fm/api/submissions)"
  end
  @source = a
end

#submit(artist, track, options = {}) ⇒ Object Also known as: scrobble

Scrobble a track.

The artist and track are required parameters. Other parameters can be added as options:

:time

Time at which the track started playing. Defaults to now

:length

Length of the track in seconds (required if the source is “P”, the default)

:album

Album title

:track_number

Track number

:mb_trackid

MusicBrainz Track ID

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simple_scrobbler.rb', line 84

def submit(artist, track, options={})
  enforce_keys options, :time, :length, :album, :track_number, :mb_trackid
  if @source == "P" && !options[:length]
    raise DataError, "Track length must be specified if source is P"
  end
  handshake
  parameters =
    generate_scrobbling_parameters(true,
      options.merge(:artist => artist,
                    :track  => track,
                    :source => @source,
                    :time   => (options[:time] || Time.now).utc.to_i))
  status, = split_plain_text_response(post(@submission_url, parameters))
  raise SubmissionError, status unless status == "OK"
end