About Me

Curriculum Vitae

A brief list of my current skill set


Faster Loops and Faster Iterations in Node.js and V8
Wednesday, 29th August 2012, 13:16

Is Object.keys faster than For...In?

And the Fastworks.js framework is Born!
Wednesday, 22nd August 2012, 16:23

Well I'm excited, even if you aren't

Libxmljs Update on CentOS 3.8 throws an SELinux Wobbley Fit
Monday, 20th August 2012, 15:40

The right way to fix this sort of issue

TV Land Doesn't Understand Technology
Friday, 17th August 2012, 17:09

Or maybe it does and thinks we don't?

Yet More Benchmarking - Function Chains vs Object Chains
Wednesday, 15th August 2012, 13:34

Working towards a faster Node.js framework

More Benchmarking in Node.js and V8
Tuesday, 14th August 2012, 12:19

Working out the fastest way to route

MinnaHTML.js Benchmarking for Speed in Node.js
Monday, 13th August 2012, 17:55

Don't believe it, test it

Playing Around With HTML5's Canvas
Friday, 10th August 2012, 16:19

Speccy loading screens in a browser!

Scripting in Node.js AKA How to Watch for Olympic Tickets Using a Script
Tuesday, 7th August 2012, 23:37

Let Node refresh the webpage so you don't have to!

A Javascript Confirm/Alert Replacement jQuery Plugin
Monday, 6th August 2012, 23:06

Much prettier than the horrible alert and confirm dialogs

A Few Node.js Essential Modules
Friday, 3rd August 2012, 14:40

As essential as they can be anyway

Retro Coding Corner: Loading ZX Spectrum Snapshots off Microdrives - Part 2
Tuesday, 31st July 2012, 18:20

It's like juggling 8 balls with one hand behind your back

One Layout To Rule Them All
Monday, 30th July 2012, 16:03

Designing a layout to fit every screen

Retro Coding Corner: Loading ZX Spectrum Snapshots off Microdrives - Part 1
Thursday, 26th July 2012, 16:57

From emulation to super fast tape

Upgrading Libxml2 on CentOS 5.8 fixes Libxmljs
Wednesday, 25th July 2012, 11:33

Fixes strange compile errors and everyfin

Projects and Sillyness

MAME Cabinet Diary

How I built my own arcade cabinet

Loading Screen Simulator

I don't miss the ZX Spectrum, I still use it!

The Little Guy Chat Room

It's a Pitfall inspired chat room


A fully featured MP3 player what I wrote

GP Space Invaders

My first little emulator

GP32 Development Page

Some info and links about this cute little handheld

Disney Nasties

Uncensored images, you must be 18 to view them

Diary of a Hamster

Learn about how hamsters think, first hand


Time Calculator

A simple little online utility for working out how many hours to bill a client

A Few Links

Scripting in Node.js AKA How to Watch for Olympic Tickets Using a Script
Tuesday, 7th August 2012, 23:37

So, in the first Olympic Tickets ballot I think I applied for maybe £500 worth of tickets, expecting to get a pair if I was lucky. And I was unlucky, I got nothing. Then maybe a few month before the games they released more tickets, and I snapped up a pair for the mixed doubles quarter finals in badminton.

I was happy with that, at least I got to go to something. Although the irony of being in the East end of London a few stops from where the Olympic Park is, and having to trapse across town to the West side to visit Wembley, wasn't lost on me. And all of Team GB had been knocked out by then so no home talent to cheer for, but the two supporters from Thailand more than made up for that.

Then literally the evening after the badminton trip, at 11pm, my brother managed to get some tickets for the women's hockey the next day, in the park. So that was great, a chance to see all of that and enjoy some hockey. Still no Team GB, but it was nice to get to the main park itself.

So now we are hoping to get tickets for the Athletics this Thursday, and I came rather close today after refreshing the website a lot, but no bananas. And since refreshing webpages is the order of the day, I thought... this is a job for a Node script.

Why should I refresh the page everytime when I can get my computer to do it for me?

The Script

Rather than post the entire thing, I'll post it broken up into lines so you can see how the general principle works.

var http = require("http");
var colour = require("coloured");


Just two requires for this script, the built in http module and the coloured one I mentioned already in my Essential Node.js Modules list from the other day.

Now we set up some global variables, normally an evil thing, but for small scripts this sort of thing is fine and saves a lot of messing about.

var intInterval = 10000;
var reUnavailableMatch = /Tickets to this session are currently unavailable/i;
var strAppVersion = "0.1";
var strAppName = "Olympic Tickets Availability Alerter";

The first is the rough interval we will check the webpage, in milliseconds. I think every 10 seconds is plenty. We also have the name of the app and version, just because I find putting these in strings at the start a historically sensible approach.

We also create a regular expression which matches something on the page that only appears if there is no tickets available. We only use this in once place, but we do use it more than once, and it's good practise to not create a new regexp every single time we use it if it's the same.

var strUrl = process.argv[2];
var strName = process.argv[3];
var intErrors = 0;
var intGets = 0;

Our script will accept two parameters, the URL it will be pulling, and an optional friendly name. The reason for the latter is if you have several scripts running at the same time and want to know quickly which alert is being triggered.

We also will keep track of how many times we pull the webpage, and how many errors we get. For no other reason than it gives us something useful we can spam to the screen every five minutes just to reassure us the thing hasn't crashed.

function urlScan()
http.get(strUrl, function(result) {
var pageData = "";

result.on("data", function (chunk) {
pageData += chunk;

result.on("end", function() {
if (reUnavailableMatch.exec(pageData) == null)
setTimeout(urlScan, intInterval);
}).on("error", function(error) {
console.log("Webpage error: ".green().bold() + error.message.bold());

setTimeout(urlScan, intInterval);

urlScan() is our main worker function. It increases our get counter, and then attempts to pull the webpage. On an error it logs that to the console, increases our error counter and then sets an interval to pull the page again.

On success it adds two listener functions to the EventEmitter for the request, "data" builds a string containing the returned webpage, whilst "end" is called once it is complete. When we have the whole page, we check it against our regex to see if our tickets unavailable string exists, and if so we set an interval to pull the page again.

If we don't get a match, we can assume that this page has a good chance of containing tickets, so we call our alerter function.

setTimeout Good setInterval Bad

Just a quick note on these two functions. As a general rule, you should never ever use setInterval(), unless you have a completely most excellent reason for doing so. This is because doing so means you have to absolutely guarantee that the previous setInterval() has finished or you'll start to get an ever increasing backlog which will grind everything to a halt.

A much safer thing to do, is use setTimeout(), and then call it again just before your function exits. This pretty much guarantees you'll never get a backlog, and unless you are relying on something to happen exactly every so many milliseconds, is perfectly adequate. And actually, if you really do require precision, setInterval() can't give you it either, because Javascript is a single threaded environment, so there is no guarantee that your function can be called exactly on time anyway.

Back to the Scripting...

Now our alarm function.

function ringAling()
if (strName)
console.log("\u0007Tickets available for ".cyan().bold() + strName.yellow().bold());
console.log("\u0007Tickets available!".cyan().bold());

setTimeout(ringAling, 500);

Nothing special here, apart from the strange \u0007 thing, what is that I hear you ask? Well it is the ASCII bell which will cause your console (even if you are logged in via an SSH terminal) to beep.

We will beep twice a second, until you quit the script with CTRL-C, and spam text up the screen just in case you can't hear the beeping over Def Leppard or something.

function showStats()
var strMonitorLine = "Monitoring ".red().bold()

if (strName)
strMonitorLine += " " + strName;
if (intGets)
strMonitorLine += " Checked " + intGets + " times (" + intErrors + " errors)";

setTimeout(showStats, 1000 * 60 * 5);

Just so we can not panic that our script has stalled, every 5 minutes we'll spam a line with some info on it. That's our showStats() function in a nutshell.

And finally, the last bit that kicks it all off:

console.log("\n" + strAppName.cyan().bold() + " " + strAppVersion.yellow().bold() + "\n\n");

if (strUrl)
console.log("Usage: ".yellow().bold() + "olert http://www.tickets.london2012.com/eventdetails?id=EVENTID [\"Event Name\"]\n".bold());

Here we spam the name of the app and the version to the console, then check to see if a URL was passed. If it was, we kick off the timeouts to show the stats and pull the webpage, if it wasn't we show a help line, after which the script will gracefully exit because the event queue is empty.

With that in mind, the usage is something like this:

node olympic_alerter.js http://www.tickets.london2012.com/eventdetails?id=0000455AC9A2095C "Thursday Evening Athletics"

Bear in mind the URL you need to pass is the one that you can only click through to when you go to the Search Events page, find the correct event (if it is available) and click the See Tickets button. Something which is alas not there if there are none to see. :/ You also want an account, and make sure you are logged by regularly visiting the My Account page too.

I never said it was going to be easy. :(

Other Applications

I've actually used a similar sort of system to this for the DVD Reviewer Bargain Watcher. It pretty much goes through a database list and uses it to pull webpages, then runs regexps against them to retrieve the current prices.

Can be a pain when the sites it supports change their layouts, as I have to fix the regexps, but that seems to happen rarely these days.


Add Your Own Comment