Skip to content

Instantly share code, notes, and snippets.

@sbauch
Created July 8, 2014 13:54
Show Gist options
  • Save sbauch/0f5ebdf71393505fe6f5 to your computer and use it in GitHub Desktop.
Save sbauch/0f5ebdf71393505fe6f5 to your computer and use it in GitHub Desktop.
Echonest & Spotfiy: Taste Profile Create => Update => Playlist => Spotify Web API Lookup
HTTParty.post('http://developer.echonest.com/api/v4/tasteprofile/create',
:body => {:api_key => '#############',
:type => 'general',
:name => 'tester testy'
}
)
=> {"response"=>{"status"=>{"version"=>"4.2", "code"=>0, "message"=>"Success"}, "type"=>"general", "name"=>"tester testy", "id"=>"CAIGHXV147163258A7"}}
data = [
{:action => 'update',
:item =>{
:track_id => 'spotify:track:6DDw8ALI8woZLu6R4RHae9'
}
}
]
HTTParty.post('http://developer.echonest.com/api/v4/tasteprofile/update',
:body => {
:api_key => '#################',
:id => 'CAVRIFH146FA0C1891',
:data => data.to_json
})
=> {"response"=>{"status"=>{"version"=>"4.2", "code"=>0, "message"=>"Success"}, "ticket"=>"CAVRIFH146FA0C18915F9294BC8CCB4B"}}
HTTParty.get('http://developer.echonest.com/api/v4/tasteprofile/status?api_key=################&ticket=CAVRIFH146FA0C18915F9294BC8CCB4B')
=> {"response"=> {"status"=>{"version"=>"4.2", "code"=>0, "message"=>"Success"}, "total_items"=>1, "items_updated"=>1, "percent_complete"=>100.0, "ticket_status"=>"complete", "update_info"=>[]}}
songs = HTTParty.get('http://developer.echonest.com/api/v4/playlist/static?api_key=###########&results=20&type=catalog-radio&adventurousness=1.0&bucket=tracks&bucket=id:spotify-WW&song_selection=song_discovery-top&seed_catalog=CAVRIFH146FA0C1891')
track_ids = []
songs.each do |song|
begin
track_ids << song['tracks'][0]['foreign_id']
rescue => e
p e
end
end
ids_str = track_ids.map{|id| id.split(':').last}.join(',')
response = HTTParty.get("https://api.spotify.com/v1/tracks/?ids=" + ids_str).parsed_response
response['tracks'].each do |track|
Track.create(:spotify_id => track['id'],
:preview_url => track['preview_url'],
:artwork_url => track['album']['images'][1]['url'],
:artist => track['artists'][0]['name'],
:track_name => track['name'],
:deep_link => 'spotify://track/' + track['id'],
:open_link => track['external_urls']['spotify'],
:profile_id => profile_id
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment