Last active
May 21, 2016 15:23
-
-
Save harthur/4507182 to your computer and use it in GitHub Desktop.
How I got tickets to the Gymnastics finals at the Olympics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This node.js script checks the Olympics website for any new Women's | |
* Gymnastics tickets. Every five minutes it fetches the available tickets | |
* page for the event, and uses the cheerio (https://npmjs.org/package/cheerio) | |
* module to parse the page and look for the UI element that indicates there | |
* are tickets. | |
* | |
* If there are any tickets it plays a song using node's built in `exec()` | |
* function to call OS X's command line utility to play a sound file. It also | |
* uses the growl module (https://npmjs.org/package/growl) to post a growl | |
* notification. | |
* | |
* To run the script, get node.js (http://nodejs.org/). Open up the Terminal | |
* and install the cheerio and growl modules with the commands | |
* `npm install cheerio` and `npm install growl`. If you use growl you'll | |
* also need the growlnotify app (http://growl.info/extras.php#growlnotify). | |
* | |
* If you're not on OS X, growl won't work, but you can replace `afplay` with | |
* your OS's built in command to play sound. | |
* | |
* Then run the script with `node tix.js` | |
*/ | |
var request = require('request'), | |
growl = require("growl"), | |
cheerio = require("cheerio"), | |
exec = require('child_process').exec; | |
var sessionURL = "http://www.tickets.london2012.com/eventdetails?id=0000455ACD8C0B72"; | |
var fiveMinutes = 1000 * 60 * 5; | |
// check tickets once | |
checkTickets(); | |
// then check tickets every five minutes | |
setInterval(checkTickets, fiveMinutes); | |
function checkTickets() { | |
console.log("checking for tickets"); | |
request(sessionURL, function (error, response, body) { | |
if (error) { | |
console.log(error); | |
} | |
else if (response.statusCode != 200) { | |
console.log(response.statusCode, "error"); | |
} | |
else if (hasSeats(body)) { | |
celebrate(); | |
} | |
}) | |
} | |
function hasSeats(html) { | |
var $ = cheerio.load(html); | |
var elements = $("select#price_category > option"); | |
return elements.toArray().length; | |
} | |
function celebrate() { | |
console.log("Gymnastics tickets found!"); | |
// play a song | |
exec("afplay ~/heartbreaker.mp3", function(err) { | |
if (err) console.log(err); | |
}); | |
// post a growl notification | |
growl("Women's gymnastics tickets available!", { | |
title: "Tickets available", | |
sticky: true, | |
image: "./gymnast.jpg" | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps to run:
Install node.js
Install required packages with
npm install cheerio request growl
run with
node tix.js