Class: LastFM12

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

Overview

main class

Defined Under Namespace

Classes: AdjustingError, Error, GettingTracksError, HandshakeError, Track

Constant Summary collapse

HANDSHAKE_HOST =
'ws.audioscrobbler.com'
HANDSHAKE_PATH =
'/radio'
HANDSHAKE_SCRIPT =
'handshake.php'
ADJUST_SCRIPT =
'adjust.php'
PLAYLIST_SCRIPT =
'xspf.php'
DEFAULT_USER_AGENT =
'Ruby-Lastfm12'
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, opt = nil) ⇒ LastFM12

Handshake with ws.audioscrobbler.com [username] an account name on last.fm [password] an password for the account [opt] a hash as Fig.A

===Fig.A

key

value

:user_agent or "user_agent":: an string used as an user agent name for handshake, ajusting radio station and playlist requests :station or "staion":: an array of strings such as ["tag","electoro"]. refer adjust_station method :verbose or "verbose":: true: output oparations, false: no output



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/lastfm12.rb', line 88

def initialize(username,password,opt=nil)
  
  @user_name = username.to_s
  @password = password.to_s
  
  @user_agent=DEFAULT_USER_AGENT
  @station = nil
  @verbose = $DEBUG
  if opt.class == Hash
    opt.each do |k,v|
      case k
      when "user_agent",:user_agent
        @user_agent = v.to_s
      when "station", :station
        @station = _make_lastfm_url(*(v.to_a))
      when "verbose", :verbose
        @verbose = v
      end
    end
  end

  @session = nil
  @base_url = nil
  @base_path = nil
  _handshake

  if @station
    _adjust_station(@station)
  end

  @@instance=self

end

Instance Attribute Details

#stationObject (readonly)

Returns the value of attribute station.



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

def station
  @station
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



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

def user_agent
  @user_agent
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



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

def user_name
  @user_name
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Class Method Details

.play_loop(*args, &block) ⇒ Object

More simple API LastFM12.play_loop(username, password, opt, &block) or LastFM12.play_loop(hash, &block)

[username] an account name on last.fm [password] an password for the account [opt] a hash as Fig.A [hash] a hash as Fig.A*

===Fig.A

key

value

:user_agent or "user_agent":: an string used as an user agent name for handshake, ajusting radio station and playlist requests :station or "staion":: an array of strings such as ["tag","electoro"]. refer adjust_station method :verbose or "verbose":: true: output oparations, false: no output ===Fig.A*

key

value

:user_name or "user_name":: an account name on last.fm :password or "password":: an password for the account :user_agent or "user_agent":: an string used as an user agent name for handshake, ajusting radio station and playlist requests :station or "staion":: an array of strings such as ["tag","electoro"]. refer adjust_station method :verbose or "verbose":: true: output oparations, false: no output



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/lastfm12.rb', line 216

def LastFM12.play_loop(*args,&block)

  if args.length == 1 and args[0].kind_of?(Hash)
    hash = args[0]
  elsif args.length == 3 and args[2].kind_of?(Hash)
    hash = args[2]
    hash[:user_name] = args[0]
    hash[:password] = args[1]
  else
    raise ArgumentError
  end

  new_hash = Hash.new
  hash.each{|k,v|new_hash[k.to_sym]=new_hash[k.to_s]=v}
  hash = new_hash

  if @@instance.nil? or @@instance.user_name != hash[:user_name]
    @@instance = LastFM12.new(hash[:user_name],hash[:password],hash)
  else
    puts "[lastfm12] Skip Handshake" if @@instance.verbose
  end

  @@instance.play_loop(&block)

end

Instance Method Details

#adjust_station(param_name, param) ⇒ Object

Adjusting a station specified as arguments ===Arguments These argments class should be Symbol class or String class.

param_name

Discription

personal

param's personal radio station

artist

param's similar radio station

tag or globaltags

a radio station having tracks tagged as param

group

a radio statio for param group

neighbours

param's neighbours radio station

recommended

param's recommended radio station



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/lastfm12.rb', line 133

def adjust_station(param_name,param)

  station = _make_lastfm_url(param_name, param)

  if @station == station
    puts "[lastfm12] Already Adjusted The Station" if @verbose
    return station
  else
    @station = station
  end

  _adjust_station(@station)

end

#get_tracks(min_num_tracks = nil) ⇒ Object

Getting tracks Return a array of tracks. [min_num_tracks] the minimun number of tracks ===Note The length of the returned array is vary. So, sometime, get_tracks get no track. Give min_num_tracks, if you want to get tracks more than min_num_tracks.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lastfm12.rb', line 154

def get_tracks(min_num_tracks=nil)

  ret = []
  count = 0

  if min_num_tracks

    while ret.length < min_num_tracks

      ret = ret + _get_tracks
      
      if @verbose
        z = ret.length.zero?
        o = (ret.length == 1)
        puts "[lastfm12] #{z ? '' : 'Success'} Getting #{z ? 'No' : ret.length } Track#{z and o}s"
      end

    end

  else
    ret = _get_tracks
  end

  ret

end

#play_loop(&block) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/lastfm12.rb', line 182

def play_loop(&block)

  loop do
    self.get_tracks(1).each(&block)
  end

end