Tuesday, August 24, 2010

Sydney Cityrail (131500) Timetable on Command-line (ver: 0.2)

Sydney CiryRail (www.131500.info) changed how parameters are fed in the URL from user's browser which broke my previous command line attempt.

Changes in this version:
  • Time format to use with '-t' is: "16:00" or "04:08:pm"
  • Date format to use with '-d' is: "2010:08:24"
Here is a fixed version (0.2) of the script:

    1 #!/bin/bash -e
    2 
    3 print_url=0
    4 timeout=10
    5 
    6 from_sta='Central'
    7 dest_sta='Town+Hall'
    8 
    9 old_now_date=$(date +%d%%2F%m%%2F%y)
   10 old_now_time=$(date +%I%%3A%M%p)
   11 
   12 now_date=$(date +%Y%m%d)
   13 now_hour=$(date +%I)
   14 now_min=$(date +%M)
   15 now_ampm=$(date +%P)
   16 
   17 function usage()
   18 {
   19 echo
   20 cat <<EOF
   21 [*] usage: `basename $0`  options
   22 
   23 OPTIONS:
   24  -u print url
   25  -h show this message
   26  -a set from station
   27  -b set dest. station
   28  -t set search time (08:30AM)
   29  -d set search date (21/5/10)
   30 EOF
   31 echo
   32 exit
   33 }
   34 
   35 function get_cookie()
   36 {
   37  wget -q -O /dev/null --keep-session-cookies --save-cookies="cookie.txt" "${
   38  1}"
   39 }
   40 
   41 while getopts "uha:b:t:d:" flag
   42 do
   43  if [ "$flag" == 'u' ]
   44  then
   45   print_url=1
   46  fi
   47 
   48  if [ "$flag" == 'h' ]
   49  then
   50   usage
   51  fi
   52 
   53  if [ "$flag" == 'a' ]
   54  then
   55   A=$(echo -n "${OPTARG:0:1}" | tr "[:lower:]" "[:upper:]")
   56   from_sta="$(echo -n "${A}${OPTARG:1}" | sed 's/ /+/g')"
   57  fi
   58 
   59  if [ "$flag" == 'b' ]
   60  then
   61   A=$(echo -n "${OPTARG:0:1}" | tr "[:lower:]" "[:upper:]")
   62   dest_sta="$(echo -n "${A}${OPTARG:1}" | sed 's/ /+/g')"
   63  fi
   64 
   65  if [ "$flag" == 't' ]
   66  then
   67   now_time=$(echo -n "$OPTARG" | tr '[:lower:]' '[:upper:]')
   68   now_time=$(echo "$now_time" | sed 's/://g;s/\-//g;s/\///g;s/\\//g')
   69 
   70   now_hour=$(echo -n "${now_time:0:2}")
   71   now_min=$(echo -n "${now_time:2:2}")
   72   now_ampm=$(echo -n "${now_time:4:2}")
   73  fi
   74 
   75  if [ "$flag" == 'd' ]
   76  then
   77   now_date="$OPTARG"
   78   now_date=$(echo "$now_date" | sed 's/://g;s/\-//g;s/\///g;s/\\//g')
   79  fi
   80 done
   81 
   82 old_url="http://www.131500.com.au/fullEnquiry.asp?&Vehicle=Bus,Train,
   83 Ferry&WalkSpeed=NORMAL&&IsAfter=A&MaxChanges=-
   84 1&FromLocType=s&ToLocType=s&x=64&y=11&FromLoc=$from_sta+Station~~;$from_sta+
   85 Station;$from_sta+Station~~LOCATION&ToLoc=$dest_sta+Station~~;$dest_sta+
   86 Station;$dest_sta+Station~~LOCATION&Time=$now_time&Date=$now_date"
   87 
   88 ori_url='http://www.131500.com.au/plan-your-trip/trip-
   89 planner?session=invalidate&itd_cmd=invalid&itd_includedMeans=checkbox&itd_in
   90 clMOT_5=1&itd_inclMOT_7=1&itd_inclMOT_1=1&itd_inclMOT_9=1&itd_anyObjFilter_o
   91 rigin=2&itd_name_origin=central&itd_anyObjFilter_destination=2&itd_name_dest
   92 ination=redfern&itd_itdDate=20100824&itd_itdTripDateTimeDepArr=dep&itd_itd
   93 TimeHour=2&itd_itdTimeMinute=0&itd_itdTimeAMPM=pm&x=56&y=11'
   94 
   95 url="http://www.131500.com.au/plan-your-trip/trip-
   96 planner?session=invalidate&itd_cmd=invalid&itd_includedMeans=checkbox&itd_in
   97 clMOT_5=1&itd_inclMOT_7=1&itd_inclMOT_1=1&itd_inclMOT_9=1&itd_anyObjFilter_o
   98 rigin=2&itd_name_origin=${from_sta}
   99 &itd_anyObjFilter_destination=2&itd_name_destination=${dest_sta}
  100 &itd_itdDate=${now_date}&itd_itdTripDateTimeDepArr=dep&itd_itdTimeHour=${
  101 now_hour}&itd_itdTimeMinute=${now_min}&itd_itdTimeAMPM=${now_ampm}
  102 &x=56&y=11"
  103 
  104 if [ $print_url == '1' ]
  105 then
  106  echo $url
  107 fi
  108 
  109 echo
  110 echo "From: $from_sta Station"
  111 echo "Dest: $dest_sta Station"
  112 echo "Time: $(echo $now_hour:$now_min $now_ampm | sed 's/\%3A/:/g')"
  113 echo "Date: $(echo ${now_date:0:4}:${now_date:4:2}:${now_date:6:2} | sed
  114 's/\%2F/\//g')"
  115 echo
  116 
  117 referer='http://www.131500.com.au/'
  118 agent='Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8)
  119 Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)'
  120 
  121 #get_cookie $url
  122 #wget $url -q -O - --referer "$referer" --load-cookies=cookie.txt --user-
  123 agent="$agent" | html2text | grep 'Option \([0-9]\+\)\|Arr:\|Dep:'
  124 lynx -noredir -dump $url | grep 'Option \([0-9]\+\)\|Arr:\|Dep:'
  125 
  126 echo

0 comments:

Post a Comment