public
Created

Demo of of Echo Nest playlist generation with variety and distribution

  • Download Gist
PlaylistWithDistribution.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 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
!DOCTYPE html>
<html>
<head>
<title>Playlist Distribution Demo</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<link type="text/css" href="demo_styles.css" rel="stylesheet" />
</head>
 
<body>
<h1 id='title'> Playlist Distribution Demo</h1>
<div class ='info'>
This is a demonstration of how the
<a href="http://developer.echonest.com/docs/v4/playlist.html#static">Echo Nest playlist API </a>
gives you control over the the number and type of artists appearing in a playlist.
</div>
 
<br>
 
<span class='box'> Seed artist:
<input title="Artist" type="text" size=24 id="artist"
onkeydown="if (event.keyCode == 13) newArtist()" name="artist" value='weezer'/>
</span>
 
<span class='box'> Variety:
<span id='range'> min <input type="range" name="variety" id=variety value='3' min="1" max="10" /> max
</span>
</span>
 
<span class='box'> Distribution:
<input type="radio" name="distribution" id="wandering" value="wandering" checked /> wandering
<input type="radio" name="distribution" id="focused" value="focused" /> focused
</span>
 
<button value="go" id="go" name="go" disabled onclick="newArtist()"> Go </button>
 
<div id="info"> </div>
<div id="all_results">
<div id='artists'>
<div id="histogram2"> </div>
</div>
 
 
<div id='tracks'>
<h2> The Playlist</h2>
<ol id="results"> </ol>
</div>
</div>
</body>
 
<script type="text/javascript">
 
$("#all_results").hide();
google.load("visualization", "1", {packages:["corechart"]});
 
 
function fetchArtistPlaylist(artist, wandering, variety) {
var url = 'http://developer.echonest.com/api/v4/playlist/static?api_key=GETYOUROWNKEY&callback=?';
 
$("#all_results").hide();
info("Creating the playlist ...");
$.getJSON(url, { 'artist': artist, 'format':'jsonp',
'results': 100, 'type':'artist-radio', 'variety' : variety,
'distribution' : wandering ? 'wandering' : 'focused' }, function(data) {
 
info("");
$("#results").empty();
$("#all_results").show();
 
var artists = {};
for (var i = 0; i < data.response.songs.length; i++) {
var song = data.response.songs[i];
if (! (song.artist_name in artists) ) {
artists[song.artist_name] = 0;
}
artists[song.artist_name]++;
var li = $("<li>").html("<span class='title'>" + song.title + "<span>"
+ " by <span class='artist'>" + song.artist_name + "</span>");
$("#results").append(li);
}
showArtistHistogram(artists);
});
}
 
 
function showArtistHistogramTable(artist) {
$("#histogram").empty();
var keys = [];
for (a in artists) {
console.log(a);
keys.push(a);
}
keys.sort(function(a,b) { return artists[b] - artists[a]; });
 
var ul = $("<ul>");
console.log(keys);
for (var i in keys) {
var artist = keys[i];
var li = $("<li>").html(artists[artist] + " " + "<span class='artist'>" + artist + "</span>");
$("#histogram").append(li);
}
}
 
function showArtistHistogram(artists) {
var keys = [];
for (a in artists) {
keys.push(a);
}
keys.sort(function(a,b) { return artists[b] - artists[a]; });
 
var rows = [];
for (var i in keys) {
var name = keys[i];
var count = artists[name];
rows.push( [name, count] );
}
 
var data = new google.visualization.DataTable();
data.addColumn('string', 'Artist');
data.addColumn('number', 'Count');
 
data.addRows(rows);
var options = {
title: 'Artist Histogram',
height: rows.length * 32,
width: 500,
fontSize: 12,
vAxis: {title: 'Artist', titleTextStyle: {color: 'blue'}},
titleTextStyle: {fontSize:18},
chartArea: { top: 32 },
legend: { position: 'none' },
};
 
var chart = new google.visualization.BarChart(document.getElementById('histogram2'));
chart.draw(data, options);
}
 
function newArtist() {
var artist = $("#artist").val();
var wandering = $("#wandering").is(':checked');
var variety = $("#variety").val() / 10.0;
fetchArtistPlaylist(artist, wandering, variety);
}
 
function info(txt) {
$("#info").text(txt);
}
 
 
$(document).ready(function() {
$("#go").removeAttr("disabled");
});
 
</script>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.