Sunday, January 17, 2010

Sathyam cinemas online booking using greasemonkey


I wrote a simple JavaScript tweak for greasemonkey users (or non-users!) to alert/play a sound when bookings are open for movies released at sathyam Cineplex (http://thecinema.in) at Chennai. Ever since online booking has been facilitated here, it has become easier for IT professionals to book movie tickets sitting at their desk. But as more and more people started to use this service, we had to keep a watch on the ticket portal minute by minute when the bookings are open so as get the “first come first serve” service. So this involved numerous page refreshes by midnight on tuesday’s (normal booking open day). Even the starting time for booking won’t be officially confirmed / announced by the theater authorities.
So what I needed was a script which checks the portal for ticket availability for a particular movie and for a particular show like noon/matinee/night and if found alerts me with a sound so that I can lock my PC and sleep for the night rather than keep my eyes on my PC screen for tickets by refreshing the portal every minute.
Thecinema.in
Since this portal uses web services for each and every screen, it was easier for me to write the code. I’ve pasted the code below:
// ==UserScript==
// @name         Satyam
// @namespace     http://thecinema.in/
// @description   script to play sound if ticket is available
// @include       http://thecinema.in/*
// @resource      GMwavaudio http://gmflowplayer.googlecode.com/files/notify.wav
// ==/UserScript==

var oggB64 = GM_getResourceURL("GMwavaudio");
var au = document.createElement('audio');
var ausrc = 'data:audio/wav;base64,'+oggB64.split('data:application/octet-stream;base64,')[1];
au.setAttribute('src', ausrc);
au.setAttribute('id', 'GMwavaudio');
document.body.appendChild(au);

var intval='';
console.log('scriptloaded1');
intval=setInterval(function()
{
var http = new XMLHttpRequest();
var url = '/webWCF/AjaxBackService.svc/GetTicketDetail';
var params = '{"movieID":"ST00001265","date":"Friday |18 December"}';

http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4 && http.status == 200) {

                                var json=eval('(' +http.responseText+')');
                if(json&&json.d&&json.d.length>0)
                {
                    for(var i=0;i<json.d.length;i++)
                    {
                        if(json.d[i].DaySessionNo=="4"&&json.d[i].TicketTypes)
                        {
                            for(var j=0;j<json.d[i].TicketTypes.length;j++)
                            {
                               au.play();
                               
                                 if(intval!="")
                                  clearInterval(intval);

                                var ticket=json.d[i].TicketTypes[j]; 
                                if(ticket)
                                {             
                                    console.log(ticket.SessionID);
                                    console.log(ticket.SessionName);
                                    console.log(ticket.TicketDetail);
                                    console.log(ticket.Time);
                                }
                            }
                        }
                    }
                }
                else
                {
                    console.log('NA at '+Date());
                }
                }
}
http.send(params);
}
,30000);

If anybody doesn’t know what is greasemonkey? It’s just another Firefox add-on which makes it easier for us to run our JavaScript on any website.  Greasemonkey can be installed from https://addons.mozilla.org/en-US/firefox/addon/748.
The above given script can be added to greasemonkey user scripts for the domain Thecinema.in.
For further help please read the greasemonkey manual at:  http://wiki.greasespot.net/Greasemonkey_Manual:Installing_Scripts

Customizing the user script:
You can change the sound played when tickets are available by just editing the script (@resource) at the top.  The day of booking and the movie ID should be changed. To get the movie ID you can use greasemonkey or firebug JavaScript command line to execute the below given script. This will print out the movie ID’s for all the movies available for that day. I know this may sound raw, but definitely worth a try if you are a JavaScript user. Happy scripting!
 var params = '{"date":"Sunday |17 January"}';
var http = new XMLHttpRequest();
var url = '/webWCF/AjaxBackService.svc/GetMovieInfo';
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/json");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function()
{
  if(http.readyState == 4 && http.status == 200)
  {
    var json=eval('(' +http.responseText+')');
    if(json&&json.d&&json.d.length>0)
    {
       for(var i=0;i<json.d.length;i++)
       {
        if(i>8)
        {
            if(i%9==0)
             {       console.log("movieID:"+json.d[i]+" for movie:"+json.d[i+4]);
             }

        }
       }
    }
  }
}
http.send(params);

4 comments:

Raja said...

hi how do i give the value for Movie ID is it the full film name as shown in the website

aline said...

My cousin recommended this blog and she was totally right keep up the fantastic work!

Book Movie Tickets

prasadsubra said...

Super... I will try this for THE DARK KNIGHT RISES>>> Tnx a lot

A.Arumugam Saiarun said...

Arumugam S