Skip to content

Instantly share code, notes, and snippets.

@liesen
Created December 5, 2011 09:40
Show Gist options
  • Save liesen/1433041 to your computer and use it in GitHub Desktop.
Save liesen/1433041 to your computer and use it in GitHub Desktop.
Count number of tracks in playlists
_ = sp.require('underscore');
sp.core.library.getTracks().length
// Versus:
// Total number of tracks in "library" = all playlists
_.reduce(sp.core.library.getPlaylists(), function (a, x) { return a + _.reject(_.map(_.range(x.length || 0), function (i) { return x.getTrack(i); }), function (t) { return t.isLocal || t.isAd || t.isPlaceholder || !t.availableForPlayback; }).length; }, 0)
// Number of unique tracks (unique track URIs)
_(sp.core.library.getPlaylists()).chain().map(function (p) { return _.map(_.range(p.length || 0), function (i) { return p.getTrack(i) }) }).flatten().filter(function (t) { return t.availableForPlayback && !t.isLocal }).map(function (t) { return t.uri }).uniq().size().value()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment