TV show episode randomizer

3 November 2017

Dealing with FRIENDS (my favourite TV show) is always a huge pain in the ass. What episode am I gonna watch today? How can I randomly choose one (blind scrolling is not an option for an engineer)?

The other day I was so frustrated about that, I decided to write a script to solve of this world-wide problem.

joey-gif

When you’re such a huge fan of a TV show or a music group, you don’t rely on Netflix or Spotify. No matter what, you have the physical files with you all the time.

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++];
var ssxee = (++season < 10 ? "0" : "") + season + "x" + (episode < 10 ? "0" : "") + episode;

var command = "open ~/Movies/Friends/Season\\ " + season + "/" + ssxee + "*";

var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
  console.log(stdout);
}

exec(command, puts);

On my GitHub account you can find both the source code and its Alfred Workflow. Feel free to edit this script for your favourite TV show and enjoy it!

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