Class: Scrobbler::Scrobble

Inherits:
Object
  • Object
show all
Defined in:
lib/scrobbler/scrobble.rb

Constant Summary collapse

SUBMISSION_HOST =
'http://post.audioscrobbler.com:80/'
SUBMISSION_PORT =
80
SUBMISSION_VERSION =
'1.2.1'
@@client_id =
'tst'
@@client_ver =
'1.0'
@@default_options =
{
  :handshake_on_init => true
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scrobble

Returns a new instance of Scrobble.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
# File 'lib/scrobbler/scrobble.rb', line 28

def initialize(options={})
  params = @@default_options.merge(options)
  warn 'Warning: Default client id used, this is for testing only' if @@client_id == 'tst'
  raise ArgumentError, 'Session is required' if params[:session].nil?
  raise ArgumentError, 'User is required' if params[:user].nil?
  @session = params[:session]
  @user = params[:user]
end

Class Method Details

.client_id=(cid) ⇒ Object



20
21
22
# File 'lib/scrobbler/scrobble.rb', line 20

def Scrobble.client_id=(cid)
  @@client_id = cid
end

.client_ver=(cver) ⇒ Object



24
25
26
# File 'lib/scrobbler/scrobble.rb', line 24

def Scrobble.client_ver=(cver)
  @@client_ver = cver
end

Instance Method Details

#handshakeObject



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/scrobbler/scrobble.rb', line 37

def handshake
  ts = Time.now.to_i.to_s
  auth_token = Digest::MD5.hexdigest(@session.key + ts)
  params = {
    :hs => 'true',
    :ps => SUBMISSION_VERSION,
    :c => @@client_id,
    :v => @@client_ver,
    :u => @user.name,
    :t => ts,
    :a => auth_token,
    :sk => @session.key
  }
  url = URI.join(SUBMISSION_HOST)
  query = []
  params.each do |k,v|
    query << [Base.sanitize(k), Base.sanitize(v)]
  end
  url.query = query.join('&')
  req = Net::HTTP::Get.new(url.request_uri)
  res = Net::HTTP.start(SUBMISSION_HOST, SUBMISSION_PORT) do |conn|
    conn.request(req)
  end
  res = res.body.split("\n")
  if res[0] == 'OK'
    # Handshake done, parse and return information
    @session_id = res[1]
    @now_playing_url = res[2]
    @submission_url = res[3]
  elsif res[0] == 'BANNED'
    # This indicates that this client version has been banned from the
    # server. This usually happens if the client is violating the protocol
    # in a destructive way. Users should be asked to upgrade their client
    # application.
    raise ClientBannedError, 'Please update your client to a newer version.'
  elsif res[0] == 'BADAUTH'
    # This indicates that the authentication details provided were incorrect. 
    # The client should not retry the handshake until the user has changed 
    # their details
    raise BadAuthError
  elsif res[0] == 'BADTIME'
    # The timestamp provided was not close enough to the current time. 
    # The system clock must be corrected before re-handshaking.
    raise BadTimeError, "Not close enough to current time: #{ts}"
  else
    # This indicates a temporary server failure. The reason indicates the
    # cause of the failure. The client should proceed as directed in the
    # failure handling section
    raise RequestFailedError, res[0]
  end
end

#now_playing(track) ⇒ Object

^ handshake



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/scrobbler/scrobble.rb', line 89

def now_playing(track)
  params = {
    's' => @session_id,
    't' => track.name,
    'a' => track.artist.name,
    'b' => '',
    'l' => '',
    'n' => '',
    'm' => ''
  }
  params['b'] = track.album.name unless track.album.nil?
  params['l'] = track.duration.to_s unless track.duration.nil? && track.duration > 30
  # @todo params['n']
  params['m'] = track.mbid unless track.mbid.nil?
  res = Net::HTTP.post_form(URI.parse(SUBMISSION_HOST), params).body.split("\n")
  if res[0] == 'BADSESSION'
    # This indicates that the Session ID sent was somehow invalid, possibly
    # because another client has performed a handshake for this user. On
    # receiving this, the client should re-handshake with the server before
    # continuing. 
    raise BadSessionError
  end
end

#submit(tracks = {}) ⇒ Object

^ now_playing



113
114
# File 'lib/scrobbler/scrobble.rb', line 113

def submit(tracks={})
end