brian_hassinger

Software Developer

Autoswitch avatars on turntable.fm

For those uninitiated, turntable.fm is an online chat room crossed with internet radio. Up to 5 DJs are allowed to select songs from their carefully crafted playlists and play them for the room earning DJ points. Each user is represented by an avatar, many of which are unlocked in tiers by the points.

After unlocking a few avatars, one was not enough. I wondered if it was possible to be every avatar. With a little tinkering and reading other turntable plugins, I made a userscript/extension to constantly switch avatars. However it is very resource heavy.

This function is the heart of this gist.

ttplugin.start = function() {
  console.log('Avatar switcher started!');

  var userid = turntable.user.id;
  var auth = turntable.user.auth;
  //var avatars = [1,2,3,4,5,6,7,8]; // people
  //var avatars = [9,10,11,12,13,14,15,16,17];  // dolls
  var avatars = [18,19,121];  //cats
  var index = 0

  ttplugin.iv = setTimeout(function random() {
    index = (index + 1) % avatars.length;
    var cache = Date.now();
    var avatar = avatars[index];
    var avatar_url = 'http://turntable.fm/api/user.set_avatar?'
                    + 'avatarid='+avatar
                    + '&userid='+userid
                    + '&userauth='+auth
                    + '&client=web&decache='+cache;
    $.get(avatar_url, function(e){
      ttplugin.iv = setTimeout(random, ttplugin.speed);
    })
  }, ttplugin.speed);
}

To change avatars the turntable api will issue a specially formed GET request. This is easy to recreate, and from there trigger every 15 seconds. I've used lower intervals successfully, this one seems reasonable though. I did lower it to 250ms at one point. This did not make my browser happy, and other people in the room noticed some problems. One person reported their computer locking up for 6 full minutes.