Ady Wicaksono Daily Activities

When will your SBS Transit Bus arrive?

leave a comment »

Iris next bus is provided by SBS Transit (www.sbstransit.com.sg) for us here at Singapore to check the estimated time of SBS bus arrival. It’s very important for us, especially if we don’t want to wait for the bus too long😦

This service is accessible by internet freely, simply put bus number and bus stop we can get the
information.

Just now I create a simple perl script to access this service
Here an example session of my script in my linux box

$ perl sbs.pl
Enter Bus Number           : 91
Enter Bus Stop Number      : 18069
Bus Stop Name              : AYER RAJAH CRES - OPP BLK 71
Next Bus Arriving in       : 15 minutes
Subsequent Bus Arriving in : 32 minutes

Here the similar screen capture from web


iris-next-bus1.jpg

The HTTP transaction to get the next bus information from Iris Next Bus system is like this:

Step 1. From Browser (Firefox) we send this HTTP request
========================================================
POST http://www.sbstransit.com.sg/index.aspx HTTP/1.1
Host: www.sbstransit.com.sg
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Cookie: rl-sticky-key=c0a8089c; ASP.NET_SessionId=1qtfoortt4l2id45wgafpbzz
Content-Type: application/x-www-form-urlencoded
Content-Length: 197

__VIEWSTATE=dDwyNjcxMTQzMTA7O2w8aGVhZGVyMDppbWdidG5TZWFyY2g7Sm91cm5leXBsYW46aW1nR287aVJJUzpidG5nbzs%2BPg%3D%3D
&keyword=&iRIS%3Atxtsvcno=91&iRIS%3Atxtbusstop=18069&iRIS%3Abtngo.x=28&iRIS%3Abtngo.y=8

Step 2. SBSTransit.com.sg respond with this HTTP response
=========================================================
HTTP/1.0 302 Moved Temporarily
Date: Tue, 26 Feb 2008 02:46:52 GMT
Location: /iris3/myirisnextbus.aspx?svcno=91&stopcode=18069
Server: Concealed by Juniper Networks DX
Via: 1.1 dx2 (Juniper Networks Application Acceleration Platform - DX 5.2.6 0)
Set-Cookie: rl-sticky-key=c0a8089c; path=/;

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/iris3/myirisnextbus.aspx?svcno=91&amp;stopcode=18069'>here</a>.</h2>
</body></html>

Step 3. From Browser (Firefox) we send this HTTP request
=========================================================
GET http://www.sbstransit.com.sg/iris3/myirisnextbus.aspx?svcno=91&stopcode=18069 HTTP/1.1
Host: www.sbstransit.com.sg
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Referer: http://www.sbstransit.com.sg/
Cookie: rl-sticky-key=c0a8089c; ASP.NET_SessionId=1qtfoortt4l2id45wgafpbzz

Step 4. SBSTransit.com.sg respond with this HTTP response
=========================================================
HTTP/1.0 200 OK
Date: Tue, 26 Feb 2008 02:46:53 GMT
Server: Concealed by Juniper Networks DX
Content-Encoding: deflate
Warning: 214  "Juniper Networks DX Active"
Vary: Accept-Encoding, User-Agent
Via: 1.1 dx2 (Juniper Networks Application Acceleration Platform - DX 5.2.6 0)
Set-Cookie: rl-sticky-key=c0a8089c; path=/;

[Web content in gzip-deflate format]

base on the HTTP transaction above, we can create a perl code like this

#!/usr/bin/perl
# Iris Next Bus
# Usage:
#  perl sbs.pl

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Status;

do{
	print "Enter Bus Number: ";
	chop($bus_number = <STDIN>);
	if($bus_number !~ /^[0-9]+[A-Z}*[a-z]*$/){
		$bus_number = '';
	}
}while($bus_number == '');
do{
	print "Enter Bus Stop Number: ";
	chop($bus_stop_number = <STDIN>);
	if($bus_stop_number !~ /^[0-9]+$/){
		$bus_stop_number = '';
	}
}while($bus_stop_number == '');

$DEBUG  = 0;
$cookie = "rl-sticky-key=c0a8089c; ASP.NET_SessionId=1qtfoortt4l2id45wgafpbzz";
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (Windows) Gecko/20080201 Firefox/2.0.0.12");

# Create Request
$req = HTTP::Request->new(POST => "http://www.sbstransit.com.sg/index.aspx");
$req->content_type('application/x-www-form-urlencoded');
$req->header("Cookie" => "$cookie");
$cnt  = '__VIEWSTATE=dDwyNjcxMTQzMTA7O2w8aGVhZGVyMDppbWdidG5TZWFyY2g7Sm91';
$cnt .= 'cm5leXBsYW46aW1nR287aVJJUzpidG5nbzs%2BPg%3D%3D&keyword=&iRIS%3Atxtsvcno=';
$cnt .= $bus_number;
$cnt .= '&iRIS%3Atxtbusstop=';
$cnt .= $bus_stop_number;
$cnt .= '&iRIS%3Abtngo.x=28&iRIS%3Abtngo.y=8';
$req->content($cnt);
if($DEBUG){
	print $req->as_string."n";
}

# Send Request
my $res = $ua->request($req);
my $rc  = $res->code;
if($DEBUG){
	print $res->content;
}
if (is_redirect($rc)){
	$referral_uri = $res->header('Location');
	# Create Request
	$req          = HTTP::Request->new(GET => 
	                                 "http://www.sbstransit.com.sg/".$referral_uri);
	$req->header("Cookie" => "$cookie");
	$req->header("Referer" => "http://www.sbstransit.com.sg/");
	if($DEBUG){
		print $req->as_string."n";
	}
	# Send Request
	$res = $ua->request($req);
	$cnt = $res->content;
	# Find Bus Stop Info
	$cnt   =~ s/^.*id="lblroadesc"[^>]*>//gs;
	$binfo = $cnt;
	$binfo =~ s/</span.*$//gs;
	$cnt   =~ s/^.*id="bus1"[^>]*>//gs;
	$arrive= $cnt;
	$arrive=~ s/</span.*$//gs;
	$cnt   =~ s/^.*id="bus2"[^>]*>//gs;
	$next  = $cnt;
	$next  =~ s/</span.*$//gs;
	print "Bus Stop Name              : $binfon";
	print "Next Bus Arriving in       : $arriven";
	print "Subsequent Bus Arriving in : $nextn";
	if($DEBUG){
		#print $cnt;
	}
}

Written by adywicaksono

February 26, 2008 at 8:33 am

Posted in life, Linux, perl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: