Class: Echonest::ApiMethods::Track

Inherits:
Base
  • Object
show all
Defined in:
lib/echonest/api.rb

Instance Method Summary collapse

Methods inherited from Base

build_params_with_validation, #initialize, method_with_option, method_with_required_any, #request, validator

Constructor Details

This class inherits a constructor from Echonest::ApiMethods::Base

Instance Method Details

#analysis(filename) ⇒ Object



247
248
249
250
# File 'lib/echonest/api.rb', line 247

def analysis(filename)
  analysis_url = analysis_url(filename)
  Analysis.new_from_url(analysis_url)
end

#analysis_url(filename) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/echonest/api.rb', line 252

def analysis_url(filename)
  md5 = Digest::MD5.hexdigest(open(filename).read)

  while true
    begin
      response = profile(:md5 => md5)
    rescue Api::Error => e
      if e.message =~ /^The Identifier specified does not exist/
       response = upload(:filename => filename)
     else
       raise
     end
   end

    case response.body.track.status
    when 'unknown'
      upload(:filename => filename)
    when 'pending'
      sleep 60
    when 'complete'
      return response.body.track.audio_summary.analysis_url
    when 'error'
      raise Error.new(response.body.track.status)
    when 'unavailable'
      analyze(:md5 => md5)
    end

    sleep 5
  end
end

#analyze(options) ⇒ Object



223
224
225
226
227
# File 'lib/echonest/api.rb', line 223

def analyze(options)
  @api.request('track/analyze',
    :post,
    options.merge(:bucket => 'audio_summary'))
end

#profile(options) ⇒ Object



217
218
219
220
221
# File 'lib/echonest/api.rb', line 217

def profile(options)
  @api.request('track/profile',
    :get,
    options.merge(:bucket => 'audio_summary'))
end

#upload(options) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/echonest/api.rb', line 229

def upload(options)
  options.update(:bucket => 'audio_summary')

  if options.has_key?(:filename)
    filename = options.delete(:filename)
    filetype = filename.match(/\.(mp3|au|ogg)$/)[1]

    open(filename) do |f|
      @api.request('track/upload',
        :post,
        options.merge(:filetype => filetype),
        f)
    end
  else
    @api.request('track/upload', :post, options)
  end
end