Wednesday, June 30, 2010

Sydney Cityrail (131500) Timetable on Command-line

You need "Lynx" installed. On Debian/Ubuntu, you can do "apt-get install Lynx".

Example usage:
  • Put a '+' between the words, if the station name is not a single word.
  • '-h' shows you the help.
  • By default, it uses current date,time and Redfern-Blacktown stations as from-to combination. The station-name variables are easily editable on top of the script.
babil@quad:~$ train.sh -h

[*] usage: train.sh options

OPTIONS:
-u print url
-h show this message
-a set from station
-b set dest. station
-t set search time (08:30AM)
-d set search date (21/5/10)


babil@quad:~$ train.sh -a redfern -b north+sydney -d 2/7/2010 -t 14:00

From: Redfern Station
Dest: North+sydney Station
Time: 14:00
Date: 2/7/2010

Option 1 : 15 minutes
Dep: 2:00pm Redfern Station Platform 3
Arr: 2:15pm North Sydney Station Platform 4
Option 2 : 15 minutes
Dep: 2:04pm Redfern Station Platform 3
Arr: 2:19pm North Sydney Station Platform 4
Option 3 : 15 minutes
Dep: 2:15pm Redfern Station Platform 3
Arr: 2:30pm North Sydney Station Platform 4
Option 4 : 15 minutes
Dep: 2:19pm Redfern Station Platform 3
Arr: 2:34pm North Sydney Station Platform 4
Option 5 : 21 minutes
Dep: 2:20pm Redfern Station Platform 5
Arr: 2:28pm Wynyard Station Platform 6
Dep: 2:35pm Wynyard Station Platform 4
Arr: 2:41pm North Sydney Station Platform 4
Here's is the script:
    1 #!/bin/bash
2
3 print_url=0
4
5 from_sta='Central'
6 dest_sta='Town+hall'
7 now_date=$(date +%d%%2F%m%%2F%y)
8 now_time=$(date +%I%%3A%M%p)
9
10 function usage()
11 {
12 echo
13 cat <<EOF
14 [*] usage: `basename $0` options
15
16 OPTIONS:
17 -u print url
18 -h show this message
19 -a set from station
20 -b set dest. station
21 -t set search time (08:30AM)
22 -d set search date (21/5/10)
23 EOF
24 echo
25 exit
26 }
27
28 while getopts "uha:b:t:d:" flag
29 do
30 if [ "$flag" == 'u' ]
31 then
32 print_url=1
33 fi
34
35 if [ "$flag" == 'h' ]
36 then
37 usage
38 fi
39
40 if [ "$flag" == 'a' ]
41 then
42 A=$(echo -n "${OPTARG:0:1}" | tr "[:lower:]" "[:upper:]")
43 from_sta="$(echo -n "${A}${OPTARG:1}" | sed 's/ /+/g')"
44 fi
45
46 if [ "$flag" == 'b' ]
47 then
48 A=$(echo -n "${OPTARG:0:1}" | tr "[:lower:]" "[:upper:]")
49 dest_sta="$(echo -n "${A}${OPTARG:1}" | sed 's/ /+/g')"
50 fi
51
52 if [ "$flag" == 't' ]
53 then
54 now_time="$(echo -n "$OPTARG" | tr "[:lower:]" "[:upper:]")"
55 fi
56
57 if [ "$flag" == 'd' ]
58 then
59 now_date="$OPTARG"
60 fi
61 done
62
63 url="http://www.131500.com.au/fullEnquiry.asp?&Vehicle=Bus,Train,
64 Ferry&WalkSpeed=NORMAL&&IsAfter=A&MaxChanges=-
65 1&FromLocType=s&ToLocType=s&x=64&y=11&FromLoc=$from_sta+Station~~;$from_sta+
66 Station;$from_sta+Station~~LOCATION&ToLoc=$dest_sta+Station~~;$dest_sta+
67 Station;$dest_sta+Station~~LOCATION&Time=$now_time&Date=$now_date"
68
69 if [ $print_url == '1' ]
70 then
71 echo $url
72 fi
73
74 echo
75 echo "From: $from_sta Station"
76 echo "Dest: $dest_sta Station"
77 echo "Time: $(echo $now_time | sed 's/\%3A/:/g')"
78 echo "Date: $(echo $now_date | sed 's/\%2F/\//g')"
79 echo
80
81 lynx -dump $url | grep 'Option \([0-9]\+\)\|Arr:\|Dep:'
82
83 echo