Class: LastFM

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

Defined Under Namespace

Modules: AudioOutput Classes: Client, Song

Constant Summary collapse

Handshake =
%Q[http://ws.audioscrobbler.com/radio/handshake.php?version=1.1.1&platform=linux&username=%s&passwordmd5=%s&debug=0&partner=]
Handshake2 =
%Q[http://post.audioscrobbler.com/?hs=true&p=1.2&c=tst&v=1.0&u=%s&t=%s&a=%s]
Tuner =
%Q[http://ws.audioscrobbler.com/radio/adjust.php?session=%s&url=lastfm://%s&lang=en]
CurrentSongInfo =
%Q[http://ws.audioscrobbler.com/radio/np.php?session=%s&debug=0]
Command =
%Q[http://ws.audioscrobbler.com/radio/control.php?session=%s&command=%s&debug=0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ LastFM

Returns a new instance of LastFM.



42
43
44
45
46
# File 'lib/lastfm.rb', line 42

def initialize(options)
  @username, @password = options[:username], options[:password]
  @md5_password = Digest::MD5.hexdigest(@password)
  handshake
end

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



41
42
43
# File 'lib/lastfm.rb', line 41

def session
  @session
end

#station_urlObject

Returns the value of attribute station_url.



41
42
43
# File 'lib/lastfm.rb', line 41

def station_url
  @station_url
end

#stream_urlObject

Returns the value of attribute stream_url.



41
42
43
# File 'lib/lastfm.rb', line 41

def stream_url
  @stream_url
end

#usernameObject

Returns the value of attribute username.



41
42
43
# File 'lib/lastfm.rb', line 41

def username
  @username
end

Instance Method Details

#action(command) ⇒ Object

NOTE skip doesn’t work for some reason, so we will simply restart the stream from the telnet client skip, love, ban



105
106
107
108
109
110
111
# File 'lib/lastfm.rb', line 105

def action(command)
  request = Command % [@session, command]
  puts request
  @response = send_request(request)
  puts @response.body
  return get_property("response", @response.body) == "OK" # return false if failed
end

#current_songObject

Info for the current song



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

def current_song
  @response = response = send_request(CurrentSongInfo % [@session])
  # puts response.body # for debugging
  streaming = get_property("streaming", response.body) == "true"
  return nil unless streaming
  song = LastFM::Song.new
  LastFM::Song::SongAttributes.each do |attribute|
    value = get_property(attribute.to_s, response.body)
    if value.nil?
      # something went wrong, reset song 
      return nil
    end
    song.send(attribute.to_s + "=", value.strip)
  end
  song
rescue 
  nil
end

#get_property(key, responsebody) ⇒ Object

convenience method for parsing the key/value response of the LastFM protocol



114
115
116
117
118
119
# File 'lib/lastfm.rb', line 114

def get_property(key, responsebody)
  re = /^#{key}=(.*)/
  matchdata = re.match(responsebody)
  return unless matchdata
  matchdata[1]
end

#handshakeObject

version 1.1 protocol



53
54
55
56
57
# File 'lib/lastfm.rb', line 53

def handshake
  response = send_request(Handshake % [@username, @md5_password])
  @session = get_property("session", response.body)
  @stream_url= get_property("stream_url", response.body)
end

#handshake2Object

version 1.2 protocol Currently, we’ll use the old protocol.



61
62
63
64
65
66
67
68
# File 'lib/lastfm.rb', line 61

def handshake2
  timestamp = Time.now.to_i
  auth_token = Digest::MD5.hexdigest(@md5_password.to_s + timestamp.to_s)
  response = send_request(Handshake2 % [@username, timestamp, auth_token])
  puts response.body
  @session = response.body.split("\n")[1]
  response
end

#last_responseObject



48
49
50
# File 'lib/lastfm.rb', line 48

def last_response
  @response
end

#send_request(url) ⇒ Object



121
122
123
124
# File 'lib/lastfm.rb', line 121

def send_request(url)
  uri = URI.parse(url)
  Net::HTTP.start(uri.host, uri.port) {|http| http.get(uri.request_uri)}
end

#tune(path = nil) ⇒ Object

tuner stations: lastfm://user/<yourlastfmusername>/neighbours lastfm://artist/<Artist>/similarartists lastfm://globaltags/<Tag> For this method, just pass in the path part that varies, e.g. artist/u2/similarartists



76
77
78
79
80
# File 'lib/lastfm.rb', line 76

def tune(path=nil)
  @station_url = path || @station_url
  @response = send_request(Tuner % [@session, @station_url])
  return get_property("response", @response.body) == "OK" # return false if failed
end