Java – Ticket Checker Bot

Recently I was out-gunned in my ability to mash the keyboard fast enough and get past the capchas quick enough to nab myself some sought after tickets to Kate Bush in concert. There are rumours that there might be a second round of tickets released at some point. I’m not sure that’s going to turn out to be true but just in case it is I thought I would turn my failure into action and write an application which  will check for me and send me an email should tickets suddenly become available. It’s also an good excuse to learn some new things with Guice , the Java email API and velocity  (a template engine for Java).

In the end I created a wee application which has a whole load of interesting stuff in it. I can see it might come in useful for me and maybe others.

I wanted something I could set to run periodically and which would notify me by email.

I initially started using selenium but soon realised that I didn’t need a browser to navigate and scan a target site so settled on htmlunit instead.

The email component is setup to use gmail as a smtp gateway so you will need an gmail account with two-step validation switched off.

It’s totally over-engineered of course (you could probably do it in about 10 lines of perl no doubt) but that’s not the point is it?

Modules

The application has the concept of module (separate from the Guice module). These are custom classes which implement an operation of some kind which returns a list of URL links.

A module subclasses from the ModuleBot class and implements the interface:


public interface BotModule
{
   /**
   * Run module.
   *
   */
   public void run();

   /**
   * Return list of valid links.
   *
   * @return links.
   */
   public List<String> getLinkStatus();
}

In my sample I navigate to the gigsandtours website and look for the presence of a ‘buy ticket’ button. You could make it anything your were watching for.

A module is added to run by the application by adding it to the loadModules method in the TicketBot class i.e.

/**
* Load site modules.
*
* @param modules
* @param client
*/
private void loadModules(List<BotModule> modules, WebClient client)
{
   modules.add(new ModuleGigsAndTours(client));
}

This is the current definition which takes the custom module I wrote for checking the gigsandtours website.
Email

You will need to fill in your gmail credentials in the application-live.properties file. I have put dummy placemarkers in for the moment. Since this will be running on your own machine noone is going to be able to access it. If they can access your machine to look at your gmail password in this file you are already stuffed!

The application uses velocity as a template engine to build a notification email. The advantage of this is that the email layout is defined in the template and not hard-coded into the application. Take a look at the sample.

Build And Run

The component uses Maven.

To build issue the command line:

**mvn install**

This will create a zip file of the component distribution. Unzip this file to your target machine and modify the properties to match your email etc.
For Windows there is a batch file for Linux a shell file. For Windows create a periodic task to run the batch file and for Linux setup a cron job.

The project and code is up in github under the Apache 2.0 license.

 

 

Comments

Leave a Reply