public
Last active

Demonstration of how to call the Echo Nest API from a Spotify App

  • Download Gist
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Playlister</title>
<link rel="stylesheet" href="sp://import/css/adam.css">
<link rel="stylesheet" href="styles.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
 
<body>
<h2 id='title'> Here's a cool playlist based upon what is now playing!</h2>
<div id="info"> </div>
<button id="new" onclick="makePlaylistFromNowPlaying()"> Generate Playlist </button>
<div id='all-results'>
<ul id="results"> </ul>
</div>
</body>
 
<script type="text/javascript">
 
sp = getSpotifyApi(1);
jQuery.ajaxSettings.traditional = true;
 
 
function makePlaylistFromNowPlaying() {
var playerTrackInfo = sp.trackPlayer.getNowPlayingTrack();
console.log(playerTrackInfo);
 
if (playerTrackInfo == null) {
info("Start playing something and I'll make a playlist of good songs based on that song");
} else {
var track = playerTrackInfo.track;
var artist = track.artists[0].name;
fetchPlaylist(artist, 25);
}
}
 
function fetchPlaylist(artist, size) {
info('Getting playlist for ' + artist);
var url = 'http://developer.echonest.com/api/v4/playlist/basic?api_key=N6E4NIOVYMTHNDM8J&callback=?';
 
$.getJSON(url, { 'artist': artist, 'format':'jsonp',
'results': size, 'type':'artist-radio'}, function(data) {
if (checkResponse(data)) {
$("#results").empty();
info("");
var curTracks = []
for (var i = 0; i < data.response.songs.length; i++) {
var song = data.response.songs[i];
var song_name = song.title + " by " + song.artist_name;
var li = $("<li>").text(song_name);
$("#results").append(li);
}
} else {
info("trouble getting results");
}
});
}
 
function info(s) {
$("#info").text(s);
}
 
function error(s) {
info(s);
}
 
function checkResponse(data) {
if (data.response) {
if (data.response.status.code != 0) {
error("Whoops... Unexpected error from server. " + data.response.status.message);
log(JSON.stringify(data.response));
} else {
return true;
}
} else {
error("Unexpected response from server");
}
return false;
}
 
$(document).ready(function() {
makePlaylistFromNowPlaying();
});
 
</script>
</html>
manifest.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{
"BundleType": "Application",
"AppIcon": {
"18x18": "icon.png"
},
"AppName": {
"en": "SpotifyEchoNestPlaylistDemo"
},
"SupportedLanguages": [
"en"
],
"RequiredPermissions": [
"http://*.echonest.com"
]
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.