Randomising episodes selection of my favourite TV show

3 November 2015

Dealing with Friends, my favourite TV show, is always posing some difficult decisions. What episode am I gonna watch today? How can I randomly choose one? Blind scrolling is not a viable option for an engineer, so the other day I decided to write a script to solve this recurring issue.

An engineeristic approach

When you’re such a big fan of a TV show or a music group, chances are you don’t rely on third-party providers, you have digital copies of all the releases (and perhaps redundant backups of them). In my case Friends consists of 236 episodes, divided in 10 seasons: 24, 24, 25, 24, 24, 25, 24, 24, 24 and 18 episodes each. The way I stored them is:

Watching a random episode has never been so easy thanks to these few JavaScript lines:

const episodes = [24,24,25,24,24,25,24,24,23,17];
var season = 0;
var episode = Math.floor(Math.random() * 234) + 1;
while(episode > episodes[season])
  episode -= episodes[season++];
const ssxee = (++season < 10 ? "0" : "") + season + "x" + (episode < 10 ? "0" : "") + episode;

const command = "open ~/TV\\ Shows/Friends/Season\\ " + season + "/" + ssxee + "*";
const exec = require('child_process').exec;
function puts(error, stdout, stderr) {
  if(error != null) {
    console.log(stderr);
  }
}

exec(command, puts);

At the following link you can download the workflow for Alfred.

Considerations and further improvements

The next step would be to implement a weighted random selection that takes into account the episodes watched in the past 30 days, conveniently cached somewhere. I’ll save this improvement for another time, in the meantime, enjoy your favourite tv show.

  1. Except for episodes 09x23-24 and 10x17-18 which have been aired together, lowering the files number to 234.