Class: Echonest::Api
- Inherits:
-
Object
- Object
- Echonest::Api
- Defined in:
- lib/echonest/api.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'3'
- BASE_URL =
'http://developer.echonest.com/api/'
- USER_AGENT =
'%s/%s' % ['ruby-echonest', ::Echonest::VERSION]
Instance Attribute Summary collapse
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #build_params(params) ⇒ Object
- #get_analysys(method, filename) ⇒ Object
- #get_bars(filename) ⇒ Object
- #get_beats(filename) ⇒ Object
- #get_duration(filename) ⇒ Object
- #get_end_of_fade_in(filename) ⇒ Object
- #get_key(filename) ⇒ Object
- #get_loudness(filename) ⇒ Object
- #get_metadata(filename) ⇒ Object
- #get_mode(filename) ⇒ Object
- #get_sections(filename) ⇒ Object
- #get_segments(filename) ⇒ Object
- #get_start_of_fade_out(filename) ⇒ Object
- #get_tatums(filename) ⇒ Object
- #get_tempo(filename) ⇒ Object
- #get_time_signature(filename) ⇒ Object
- #get_trackinfo(method, filename, &block) ⇒ Object
-
#initialize(api_key) ⇒ Api
constructor
A new instance of Api.
- #request(name, params) ⇒ Object
- #upload(filename) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ Api
Returns a new instance of Api.
14 15 16 17 |
# File 'lib/echonest/api.rb', line 14 def initialize(api_key) @api_key = api_key @user_agent = HTTPClient.new(:agent_name => USER_AGENT) end |
Instance Attribute Details
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
12 13 14 |
# File 'lib/echonest/api.rb', line 12 def user_agent @user_agent end |
Instance Method Details
#build_params(params) ⇒ Object
144 145 146 147 148 |
# File 'lib/echonest/api.rb', line 144 def build_params(params) params = params. merge(:version => VERSION). merge(:api_key => @api_key) end |
#get_analysys(method, filename) ⇒ Object
150 151 152 153 154 |
# File 'lib/echonest/api.rb', line 150 def get_analysys(method, filename) get_trackinfo(method, filename) do |response| yield response.xml.find_first('/response/analysis') end end |
#get_bars(filename) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/echonest/api.rb', line 19 def (filename) get_analysys(:get_bars, filename) do |analysis| analysis.map do || Bar.new(.content.to_f, ['confidence'].to_f) end end end |
#get_beats(filename) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/echonest/api.rb', line 27 def get_beats(filename) get_analysys(:get_beats, filename) do |analysis| analysis.map do |beat| Beat.new(beat.content.to_f, beat['confidence'].to_f) end end end |
#get_duration(filename) ⇒ Object
85 86 87 88 89 |
# File 'lib/echonest/api.rb', line 85 def get_duration(filename) get_analysys(:get_duration, filename) do |analysis| analysis.first.content.to_f end end |
#get_end_of_fade_in(filename) ⇒ Object
91 92 93 94 95 |
# File 'lib/echonest/api.rb', line 91 def get_end_of_fade_in(filename) get_analysys(:get_end_of_fade_in, filename) do |analysis| analysis.first.content.to_f end end |
#get_key(filename) ⇒ Object
97 98 99 100 101 |
# File 'lib/echonest/api.rb', line 97 def get_key(filename) get_analysys(:get_key, filename) do |analysis| ValueWithConfidence.new(analysis.first.content.to_i, analysis.first['confidence'].to_f) end end |
#get_loudness(filename) ⇒ Object
103 104 105 106 107 |
# File 'lib/echonest/api.rb', line 103 def get_loudness(filename) get_analysys(:get_loudness, filename) do |analysis| analysis.first.content.to_f end end |
#get_metadata(filename) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/echonest/api.rb', line 109 def (filename) get_analysys(:get_metadata, filename) do |analysis| analysis.inject({}) do |memo, key| memo[key.name] = key.content memo end end end |
#get_mode(filename) ⇒ Object
118 119 120 121 122 |
# File 'lib/echonest/api.rb', line 118 def get_mode(filename) get_analysys(:get_mode, filename) do |analysis| ValueWithConfidence.new(analysis.first.content.to_i, analysis.first['confidence'].to_f) end end |
#get_sections(filename) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/echonest/api.rb', line 74 def get_sections(filename) get_analysys(:get_sections, filename) do |analysis| analysis.map do |section| Section.new( section['start'].to_f, section['duration'].to_f ) end end end |
#get_segments(filename) ⇒ Object
35 36 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 |
# File 'lib/echonest/api.rb', line 35 def get_segments(filename) get_analysys(:get_segments, filename) do |analysis| analysis.map do |segment| max_loudness = loudness = nil segment.find('loudness/dB').map do |db| if db['type'] == 'max' max_loudness = Loudness.new(db['time'].to_f, db.content.to_f) else loudness = Loudness.new(db['time'].to_f, db.content.to_f) end end pitches = segment.find('pitches/pitch').map do |pitch| pitch.content.to_f end timbre = segment.find('timbre/coeff').map do |coeff| coeff.content.to_f end Segment.new( segment['start'].to_f, segment['duration'].to_f, loudness, max_loudness, pitches, timbre ) end end end |
#get_start_of_fade_out(filename) ⇒ Object
124 125 126 127 128 |
# File 'lib/echonest/api.rb', line 124 def get_start_of_fade_out(filename) get_analysys(:get_start_of_fade_out, filename) do |analysis| analysis.first.content.to_f end end |
#get_tatums(filename) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/echonest/api.rb', line 130 def get_tatums(filename) get_analysys(:get_tatums, filename) do |analysis| analysis.map do |tatum| Tatum.new(tatum.content.to_f, tatum['confidence'].to_f) end end end |
#get_tempo(filename) ⇒ Object
68 69 70 71 72 |
# File 'lib/echonest/api.rb', line 68 def get_tempo(filename) get_analysys(:get_tempo, filename) do |analysis| analysis.first.content.to_f end end |
#get_time_signature(filename) ⇒ Object
138 139 140 141 142 |
# File 'lib/echonest/api.rb', line 138 def get_time_signature(filename) get_analysys(:get_time_signature, filename) do |analysis| ValueWithConfidence.new(analysis.first.content.to_i, analysis.first['confidence'].to_f) end end |
#get_trackinfo(method, filename, &block) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/echonest/api.rb', line 156 def get_trackinfo(method, filename, &block) content = open(filename).read md5 = Digest::MD5.hexdigest(content) begin response = request(method, :md5 => md5) block.call(response) rescue Echonest::Api::Error => e case e. when /Analysis not ready/ sleep 20 # wait for serverside analysis get_trackinfo(method, filename, &block) when 'Invalid parameter: unknown MD5 file hash' upload(filename) sleep 60 # wait for serverside analysis get_trackinfo(method, filename, &block) else raise end end end |
#request(name, params) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/echonest/api.rb', line 185 def request(name, params) method = (name == :upload ? 'post' : 'get') response_body = @user_agent.__send__(method + '_content', URI.join(BASE_URL, name.to_s), build_params(params)) response = Response.new(response_body) unless response.success? raise Error.new(response.status.) end response end |
#upload(filename) ⇒ Object
179 180 181 182 183 |
# File 'lib/echonest/api.rb', line 179 def upload(filename) open(filename) do |f| request(:upload, :file => f) end end |