Skip to content

Instantly share code, notes, and snippets.

@bonhag
Created June 16, 2013 20:30
Show Gist options
  • Select an option

  • Save bonhag/5793309 to your computer and use it in GitHub Desktop.

Select an option

Save bonhag/5793309 to your computer and use it in GitHub Desktop.
Shuffle your Turntable playlist
var shuffle = function() {
var playlistLength = $('#playlist .song').length;
for (var i = 1; i < playlistLength; i = i + 1) {
setTimeout(function() {
var s = Math.floor(Math.random() * playlistLength);
console.log('moving song ' + s + ' to bottom');
$($('#playlist .song .playlist-open-options')[s]).click();
$('.move-bottom').click();
}, 500 * i);
}
}
@benw54
Copy link

benw54 commented Jun 24, 2013

Ok, so a little longer code-wise, but much faster... here's the problem though, it's very picky and I don't know why. Maybe you can check it out. If it doesn't work for you, try picking the playlist from the dropdown menu again. Paste this into Web Console and then call shufflePL();

function doreorder(plname,from,to,last){
    var i={
    api:"playlist.reorder",
    playlist_name:plname,
    index_from:from,
    index_to:to
    };

    // function uqFjkH below seems to need queue editing to be locked
    turntable.playlist.lockQueueEdits();
    turntable.uqFjkH(i,function(){console.log('Moving from ' +from+ ' to ' +to); turntable.playlist.unlockQueueEdits()});
}

function shufflePL() {
    var playlistname = this.playlist.activePlaylist;
    var playlistlength = this.playlist.fileids.length;

    // number of times we go through and shuffle
    var iter = 2;

    console.log('Shuffling your playlist ' +playlistname+ ' (' +playlistlength+ ' songs)');

    for (var i = iter * playlistlength; i >= 0; i--)
    {
    var from = 0;
    var to = 0;
    while (from == 0)
        from = Math.floor(Math.random() * playlistlength);
    while (to == 0 || to == from)
        to = Math.floor(Math.random() * playlistlength);

    doreorder(playlistname, from, to, i);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment