Thursday, November 25, 2010

Captcha Prompter

  • This tool is written using PyGTK2. It takes an image file name as input, shows the image to the user and asks him to type the text shown in the image. The goal is to use it inside the Unix scripts. For example, one could do 'CAPTCHA_TEXT=$(python prompt_captcha.py captcha.jpg)' in Bash and perform further actions with $CAPTCHA_TEXT. This little tool will accept png, jpg, gif and most other commonimage formats.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
prompts user to solve captcha image.

This tool is written using PyGTK2. It takes an image file name as input, shows the image to the user and asks him to type the text shown in the image. The goal is to use it inside the Unix scripts. For example, one could do 'CAPTCHA_TEXT=$(python prompt_captcha.py captcha.jpg)' in Bash and perform further actions with $CAPTCHA_TEXT. This little tool will accept png, jpg, gif and most other common image formats.

[*] usage: python prompt_captcha.py captcha_image
"""

__author__ =  'Babil (Golam Sarwar)'
__version__=  '0.1'

import sys
import time
import Image
import gtk
import pygtk


pygtk.require('2.0')

class getCaptcha(object):
    """performs all the GTK2 gui actions"""

    def enter_callback(self, widget, textbox):
        """callback funcion for when user presses the enter key"""
 textbox_text = textbox.get_text()
        print textbox_text
 gtk.main_quit()

    def __init__(self, captcha=None):
        #
 if captcha == None:
  print >> sys.stderr, "[*] usage: python prompt_captcha.py captcha_image"
  sys.exit(-1)

 width, height = Image.open(captcha).size
 
 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 window.set_geometry_hints(window,width+15,height+15,width+30,height+30)
        window.set_size_request(width, height)
 window.resize(width+15, height+15)
        window.set_title("GTK textbox")
        window.connect("delete_event", lambda w,e: gtk.main_quit())
 window.set_title("Enter Captcha")
 window.set_keep_above(True)
 window.set_urgency_hint(True)
 window.move(gtk.gdk.screen_width()/2 - (width+15)/2, gtk.gdk.screen_height()/2 - (height+15)/2)


        vbox = gtk.VBox(False, 0)
        window.add(vbox)

 image = gtk.Image()
 image.set_from_file(captcha)

 button = gtk.Button()
 button.set_image(image)
 button.set_relief(gtk.RELIEF_HALF)
 #button.set_tooltip_text("Type captcha then press enter \n or click the image.")
        vbox.pack_start(button, True, True, 0)

 textbox = gtk.Entry()
        textbox.set_max_length(50)
        textbox.connect("activate", self.enter_callback, textbox)
 textbox.set_text("Enter captcha & then click on the image :-) ")
        textbox.select_region(0, len(textbox.get_text()))
 vbox.pack_start(textbox, True, True, 10)


 button.connect("clicked", self.enter_callback, textbox)
 window.show()
        vbox.show()
 button.show()
 image.show()
 textbox.show()
 textbox.grab_focus()



def main():
    gtk.main()
    return 0

if __name__ == "__main__":
 if len(sys.argv) >=2: 
  getCaptcha(sys.argv[1])
 else: 
  getCaptcha()
 main()

Monday, November 22, 2010

Download Fileserve links from shell

The script assumes, you have
  • ImageMagick
  • Curl
  • Common Unix utilities (eg. sed, awk, cut etc.)
... are installed in the path. The captcha is shown using 'display' component from ImageMagick. You need X forwarding if you are running the script over ssh.


#!/bin/bash
#source /home/babil/Desktop/recaptcha/plowshare-0.9.4/src/lib.sh

PUB_URL='http://www.fileserve.com/file/zXvrJS2'
FILESERVE_RECAPTCHA_PUBKEY='6LdSvrkSAAAAAOIwNj-IY-Q-p90hQrLinRIpZBPi'
IMAGE_VIEWER="display"

CAPTCHA_IMAGE=$(mktemp) #'captcha.jpg'
COOKIE=$(mktemp) #'tmp_cookie.txt'
HTML=$(mktemp) #'tmp_fileserve.htm'

AGENT='Mozilla/5.0 (X11; U; Linux x86_64; en-AU; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.10 (maverick) Firefox/3.6.12'
CUSTOM_HEADERS='-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-us,en;q=0.5" -H "Accept-Encoding: gzip,deflate" -H "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" -H "Keep-Alive: 115" -H "Connection: keep-alive"'
XML_HTTP_REQUEST='-H "X-Requested-With: XMLHTTPRequested"'

RED='\e[0;31m'
CYAN='\e[0;36m'
NC='\e[0m' # No Color


function regex_replace() {
RE='s/\(\/\|\\\|&\)/\\&/g'
FROM=$(sed -e "$RE" <<< "$1")
TO=$(sed -e "$RE" <<< "$2")
sed -e "s/$FROM/$TO/g"
}

# Delete leading and trailing spaces, tabs, \r, ...
function strip_whiteSpace() {
echo "$1" | sed "s/^[[:space:]]*//; s/[[:space:]]*$//"
}

# Return uppercase string
function uppercase() {
tr '[a-z]' '[A-Z]'
}

function clean_up()
{
rm -f $COOKIE
rm -f $CAPTCHA_IMAGE
rm -f $HTML
}

function find_fileName()
{

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Forum (BBCode)' | sed -n 's/.*File name: \(.*\) File size.*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Forum (BBCode)' | sed -n 's/.*url=\(.*\)\]\[b\].*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Website (HTML)' | sed -n 's/.*href=.\(.*\).>.b.File name.*/\1/p')
fi

if [ -z $FILE_NAME ]
then
FILE_NAME=$(cat $HTML | grep 'Text link' | sed -n 's/.*value="\(.*\)".*/\1/p')
fi

FILE_NAME=$(strip_whiteSpace $FILE_NAME)

}

function delay()
{
for((i=$1;i>=1;i--))
do
echo -n "$i "
sleep 1s
done
echo
}

function check_waitTime()
{
WAIT_TIME=$(cat $HTML | sed -n 's/.*You need to wait \(.*\) seconds to start another download.*/\1/p')
if [ -z $WAIT_TIME ]
then
WAIT_TIME=3
fi
WAIT_TIME=$(strip_whiteSpace $WAIT_TIME)
echo $WAIT_TIME
}

function solve_recaptcha()
{
CAPTCHA_URL='http://www.google.com/recaptcha/api/challenge?k='$FILESERVE_RECAPTCHA_PUBKEY
echo -e "$RED [*] captcha url: $NC $CAPTCHA_URL "

CHALLENGE=$(wget -q --referer $PUB_URL "$CAPTCHA_URL" -O - | grep challenge | sed -n "s/.*'\(.*\)',/\1/p")
echo -e "$RED [*] captcha challenge: $NC $CHALLENGE"
wget -q --referer $PUB_URL http://www.google.com/recaptcha/api/image?c=$CHALLENGE -O $CAPTCHA_IMAGE

#convert -trim $CAPTCHA_IMAGE $CAPTCHA_IMAGE
#$IMAGE_VIEWER "$CAPTCHA_IMAGE" &
#pid=$!
#read -p "[*] enter captcha response: " RESPONSE
#RESPONSE=$(echo $RESPONSE | sed 's/ /+/g')
#disown $(kill -9 $pid) 2>&1 1>/dev/null

echo -e -n "$RED [*] captcha input : $NC"
RESPONSE=$(prompt_captcha.py $CAPTCHA_IMAGE | sed 's/ /+/g')
echo "$RESPONSE"

RESULT=$(curl -s --user-agent "$AGENT" --cookie-jar $COOKIE --referer $PUB_URL http://www.fileserve.com/checkReCaptcha.php --data "recaptcha_challenge_field=$CHALLENGE&recaptcha_response_field=$RESPONSE&recaptcha_shortencode_field=$SHORTENCODE")
}


### - 0
if [ -z "$1" ]
then
echo
echo "[*] usage: $(basename $0) $PUB_URL"
echo
exit -1
else
PUB_URL="$(strip_whiteSpace $1 | sed 's#//fileserve.com#//www.fileserve.com#g')"
fi
echo -e "$RED [*] attempting to download: $CYAN $PUB_URL $NC"


### - 1
clean_up

### - 2
SHORTENCODE=$(echo $PUB_URL | sed -n 's/.*file\/\(.*\)/\1/p' | cut -d '/' -f 1)
SHORTENCODE=$(strip_whiteSpace $SHORTENCODE)

### - 3 save cookie
curl -s --user-agent "$AGENT" --cookie-jar $COOKIE --referer $PUB_URL $PUB_URL --data 'checkTimeLimit=check' --output $HTML

### - 4 immitating "slow download".click()
curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "recaptcha_shortencode_field=$SHORTENCODE" --output $HTML

### - 5
if [ -z "$2" ]
then
FILE_NAME="$(strip_whiteSpace $2)"
else
FILE_NAME=""
fi
find_fileName
if [ -z $FILE_NAME ]
then
echo "[*] can not find file-name, exiting ..."
exit -1
else
echo -e "$RED [*] file-name found: $NC $FILE_NAME"
fi

### - 6 solve captcha
RESULT="(null)"
while [ $RESULT != "success" ]
do
solve_recaptcha
RESULT=$(echo $RESULT | sed -n 's/{.*"\(.*\)".*/\1/p')
done
echo -e "$RED [*] captcha solved :)$NC"

### - 7 free-user delay
WAIT_TIME=$(curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "downloadLink=wait" --output -)
echo -e "$RED [*] free-user delay: $NC $WAIT_TIME"
WAIT_TIME=$(echo $WAIT_TIME | sed 's/[^0-9]//g' | bc)
delay $WAIT_TIME
$(curl -s --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "downloadLink=show" --output /dev/null)


### - 8 final download ... should give a 302 redirection
echo -e "$RED [*] Starting download ...$NC"
REDIR=$(curl --user-agent "$AGENT" --cookie $COOKIE --referer $PUB_URL $PUB_URL --data "download=normal" --location --location-trusted --output $FILE_NAME --write-out 'REDIRECT-URL:[%{url_effective}]' | sed -n 's/.*:\[\(.*\)\]/\1/p')

if [[ ! "$REDIR" =~ "/dl/" ]]
then
echo -e "$RED [*] incorrect redirection; delay then restart ...$NC"
mv -f $FILE_NAME $HTML
WAIT_TIME=$(check_waitTime)
delay $WAIT_TIME
$0 $PUB_URL
else
echo -e "$RED [*] done :D. $NC"
clean_up
exit
fi

Tuesday, October 19, 2010

IDLE not working in ActivePython

The problem is that "idle.bat" is missing some double-quotes. If you have installed Python in "C:\Programs files\Python27" instead of "C:\Python27\", "idle.bat" will fail to execute "idle.pyw".

Copy and paste the follow 2 lines in "C:\Program Files\Python27\Lib\idlelib\idle.bat" (or wherever "idle.bat" is located) to fix it:


@echo off
rem Start IDLE using the appropriate Python interpreter
set CURRDIR=%~dp0
start "%CURRDIR%..\..\pythonw.exe" "%CURRDIR%idle.pyw" %1 %2 %3 %4 %5 %6 %7 %8 %9

Monday, October 18, 2010

"Unlocker" not working on Windows 7 ?? Try "LockHunter"

Unlocker by http://ccollomb.free.fr/unlocker/ was a nice tool to find, unlock, delete files when Windows explorer can't delete them (as they are locked by other processes).

After my upgrade to Windows 7 - 64bit, Unlocker tool stopped working. Even the 64bit from author's official site doesn't work at all (no window shows up after running the application).

So, for whatever it is worth and whoever is looking for an alternative, LockHunder by http://lockhunter.com is a great alternative, works much like Unlocker and is working without any hiccup (so far) on my Windows 7.



Thursday, October 14, 2010

MikTex 2.8 Downloading/Missing "ifxetex.sty" On Every Run

I debugged this problem after seeing my LyX on my Windows 7 machine taking so long to finish. I saw "latex" process was taking too long to finish. The I ran "latex" manually and the problem was obvious - "ifxetex" is being downloaded in every run.

This seems to be a bug in MikTex 2.8. The bug report can be found here: http://sourceforge.net/tracker/?func=detail&aid=3067362&group_id=10783&atid=110783

The solution is:
  1. Copy ...
                from: C:\Program Files (x86)\MikTeX 2.8\tex\generic\ifxetex\ifxetex.sy
                to: C:\Program Files (x86)\MikTeX 2.8\tex\latex\ifxetex\ifxetex.sty
  2. From Start-Menu > MikTex 2.8 > Maintenance (Admin), run "Update (Admin)" and update all pending update-able packages.
  3. Start-Menu > MikTex 2.8 > Maintenance (Admin), run "Settings (Admin)". and Then from "General" tab, click "Refresh FNDB" and "Update Formats"
MikTex should not download "ifxetex.sty" in every run now ;-).

Friday, October 8, 2010

Post to PasteBin from Command-Line

Full credit for "urlencode" script goes to the author. 
I modified original "urlencode" script slightly to meet my 
needs. Original script can be downloaded at:  
http://www.shelldorado.com/scripts/cmds/urlencode
 
============
pastebin.sh
============


 
#!/bin/bash
#

name="your_name"
format="text"
expire_date="N"
private="0"


if [ -z $1 ]; then
 echo
 echo "[*] usage:  `basename $0`  filename"
 echo
 exit
fi


#DATA=`cat "$@" | uuencode -m -`
DATA=`cat "$@" | urlencode.sh -l`

curl -s -d paste_code="$DATA" -d paste_name="$name" -d paste_format="$format" -d
paste_expire_date="$expire_date" -d paste_private=$private --include http:
//pastebin.com/api_public.php | grep 'http://pastebin.com/' 
 


============
urlencode.sh
============



##########################################################################
# Title      : urlencode - encode URL data
# Author     : Heiner Steven (heiner.steven@odn.de)
# Date       : 2000-03-15
# Requires   : awk
# Categories : File Conversion, WWW, CGI
# SCCS-Id.   : @(#) urlencode 1.4 06/10/29
##########################################################################

PN=`basename "$0"`   # Program name
VER='1.4'

: ${AWK=awk}

Usage () {
    echo >&2 "$PN - encode URL data, $VER
usage: $PN [-l] [file ...]
    -l:  encode line endings (result will be one line of output)

The default is to encode each input line on its own."
    exit 1
}

Msg () {
    for MsgLine
    do echo "$PN: $MsgLine" >&2
    done
}

Fatal () { Msg "$@"; exit 1; }

set -- `getopt hl "$@" 2>/dev/null` || Usage
[ $# -lt 1 ] && Usage   # "getopt" detected an error

EncodeEOL=no
while [ $# -gt 0 ]
do
    case "$1" in
     -l) EncodeEOL=yes;;
 --) shift; break;;
 -h) Usage;;
 -*) Usage;;
 *) break;;   # First file name
    esac
    shift
done

LANG=C export LANG
$AWK '
    BEGIN {
 # We assume an awk implementation that is just plain dumb.
 # We will convert an character to its ASCII value with the
 # table ord[], and produce two-digit hexadecimal output
 # without the printf("%02X") feature.

 EOL = "%0D%0A"  # "end of line" string (encoded)
 split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
 hextab [0] = 0
 for ( i=1; i<=255; ++i ) ord [ sprintf ("%c", i) "" ] = i + 0
 if ("'"$EncodeEOL"'" == "yes") EncodeEOL = 1; else EncodeEOL = 0
    }
    {
 encoded = ""
 for ( i=1; i<=length ($0); ++i ) {
     c = substr ($0, i, 1)
     if ( c ~ /[a-zA-Z0-9.-]/ ) {
  encoded = encoded c  # safe character
     } else if ( c == " " ) {
  encoded = encoded "+" # special handling
     } else {
  # unsafe character, encode it as a two-digit hex-number
  lo = ord [c] % 16
  hi = int (ord [c] / 16);
  encoded = encoded "%" hextab [hi] hextab [lo]
     }
 }
 if ( EncodeEOL ) {
     printf ("%s", encoded EOL)
 } else {
     print encoded
 }
    }
    END {
     #if ( EncodeEOL ) print ""
    }
' $@



Monday, September 27, 2010

Executing commands in shell and accepting the output in Vim

Source: http://www.linux.com/archive/feed/57727

:r ! ls

It is also possible to select certain lines in Vim with v, V or ctrl+v and send it to a Bash command. The selected lines (input for Bash command) will be replaced by the output from Bash. Typical line number specification is also allowed.

:5,7 ! xargs ls -alh

Friday, September 24, 2010

Forcing Floating Point Calculation in Python

Sources:
  • http://docs.python.org/tutorial/floatingpoint.html
  • http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python

Method-1:Just write any of the parameters in the division operation in floating-point format (eg. 4.0, 3.0)
>>> 4.0 /  3
or, >>> 4 / 3.0

Method-2:
from __future__ import devision
Method-3:
>>> from decimal import Decimal
>>> Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')

Tuesday, September 21, 2010

Latex: Incorrect references for Tables, Figures etc.

Just put the \label{} inside \caption{}. 
See the following example.

\begin{table}[!ht]
\begin{center}
\caption{ \label{tab:blah} Blah Caption}
\begin{tabular}{c|c|c|c||c} A1 & B1 & C1 & D1 & E1 & F1 & G1 & & H1
\hline  
aa &bb &cc &dd &ff \\  
aa &bb &cc &dd &ff \\
\end{tabular}
\end{center}
\end{table}

Monday, September 20, 2010

Installing DMG or PKG from command-line on Mac, Apple TV

  • mounting dmg
hdid package.dmg
or, hdiutil attach package.dmg
or, hdiutil mount package.dmg
  • change directory
cd /Volumes/package/
  • install pkg
sudo installer -verbose -pkg package.pkg -target /
or, sudo /usr/sbin/installer -verbose -pkg package.pkg -target /
  • unmounting dmg
hdiutil detach /Volumes/package
or, hdiutil detach /Volumes/package -force




Wednesday, September 8, 2010

Install Windows from portable USB drive

This Windows tool [1] can apparently convert a Windows CD/ISO image (and also other Live CD/DVD images) into a boot-able USB stick. For whatever it is worth, it's Good to know there exists such a thing for Windows installation.

[1] http://www.wintoflash.com/home/en/

Monday, September 6, 2010

Installing Ubuntu with AARNET mirror

This post applies to you, only if ...
  • you are in Australia 
  • and you want to use AARNET's  mirror (which for many people would work a lot faster than the standard Australian Ubuntu mirror)
 Default Ubuntu installation provides the standard AU mirror for Australians. To use AARNET, you have to enter the following details manually during installation:
  1. Protocol: http (ftp should also be supported but I haven't tested it)
  2. Mirror address: mirror.aarnet.edu.au
  3. Mirror path: /pub/ubuntu/archive/ (don't drop the slashes, type them as shown)




Thursday, September 2, 2010

Sydney Cityrail (131500) Timetable on Command-line (Python version) v0.2

    1 #!/usr/bin/python
    2 
    3 import sys
    4 import getopt
    5 import time
    6 import re
    7 
    8 from BeautifulSoup import BeautifulSoup, Comment
    9 import urllib
   10 import urllib2
   11 
   12 
   13 def strip_comments(soup):
   14  comments = soup.findAll(text=lambda text:isinstance(text, Comment))
   15  [comment.extract() for comment in comments]
   16  return soup
   17 
   18 def show_timetable(html, from_sta):
   19 
   20  ## PARSE
   21  soup = BeautifulSoup(BeautifulSoup(html).prettify())
   22  regex = re.compile("\&nbsp",re.I)
   23 
   24  ## FINDING OPTIONS
   25  options = []
   26  div1s = soup.findAll("div",{ "class" : "boxhead tal iewfix2" })
   27 
   28  for d1 in div1s:
   29   option = d1.find("h2").renderContents().encode("ASCII").strip()
   30   options.append(option)
   31 
   32 
   33  ## FINDING TIMETABLE
   34  timetable = []
   35  div2s = soup.findAll("div",{ "class" : "boxbody iewfix" })
   36 
   37  for d2 in div2s:
   38   bs = d2.findAll("b")
   39   for b in bs:
   40    tmp = b.renderContents().encode("ASCII").strip()
   41    tmp = regex.sub("",tmp)
   42 
   43    # fixing "Sydney Terminal" / "Central Station" dilemma
   44    if tmp.find("Sydney Terminal") > 0:
   45     tmp = tmp.replace("Sydney Terminal", "Central Station (Sydney Terminal)
   46     ")
   47    elif tmp.find("Central Station") > 0:
   48     tmp = tmp.replace("Central Station", "Central Station (Sydney Terminal)
   49     ")
   50 
   51    timetable.append(tmp)
   52 
   53 
   54  ## DIPLAY
   55  a_regex = re.compile(from_sta,re.I)
   56 
   57  j=0
   58  for i in xrange(0,len(timetable)):
   59   if a_regex.search(timetable[i]):
   60    print "\n  [[ %s ]]" % (options[j])
   61    j = j + 1
   62 
   63   print timetable[i]
   64 
   65 def usage():
   66  print """
   67  [*] usage: %s  options
   68 
   69  OPTIONS:
   70  -u      print url
   71  -h      show this message
   72 
   73  -a      set from station
   74  -b      set dest. station
   75 
   76  -t      set search time (08:30:AM)
   77    hour, min, sec must be seperated by colon (:)
   78 
   79  -d      set search date (yyyy/mm/dd)
   80    yyyy = 4 digits
   81    mm = 2 digits
   82    dd = 2 ditigs
   83 
   84  EXAMPLE:
   85  %s -a central -b 'town hall' -d 2010/09/02 -t 05:30:PM
   86   """ % (sys.argv[0], sys.argv[0])
   87 
   88 def prepare_param(args):
   89  ## PRESET VARIABLES
   90  print_url = False
   91  from_sta = "redfern"
   92  to_sta = "blacktown"
   93  now_time = time.strftime("%H:%M:%p")
   94  now_date = time.strftime("%Y/%m/%d")
   95 
   96 
   97  ## PARSE COMMAND LINE ARGS
   98  try:
   99   opts, args = getopt.getopt(sys.argv[1:], "a:b:d:t:uh")
  100 
  101  except getopt.GetoptError, err:
  102   print str(err)
  103   usage()
  104   sys.exit(2)
  105 
  106  for opt, arg in opts:
  107   if opt in ("-a"):
  108    from_sta = arg
  109   elif opt in ("-b"):
  110    to_sta = arg
  111   elif opt in ("-h"):
  112    usage()
  113    sys.exit(-1)
  114   elif opt in ("-u"):
  115    print_url = True
  116   elif opt in ("-t"):
  117    now_time = arg
  118   elif opt in ("-d"):
  119    now_date = arg
  120   else:
  121    assert False, "unhandled option"
  122 
  123  ## BREAK TIME INTO HOUR, MIN, AM/PM
  124  try:
  125   now_hour = re.split(":", now_time)[0]
  126   now_min = re.split(":", now_time)[1]
  127   now_ampm = re.split(":", now_time)[2]
  128  except:
  129   print "Incorrect time format"
  130   sys.exit(-1)
  131 
  132  now_date = re.sub("[^0-9]","",now_date)
  133 
  134  return from_sta, to_sta, now_date, now_hour, now_min, now_ampm, print_url
  135 
  136 def fetch_html( from_sta,
  137     to_sta,
  138     now_date,
  139     now_hour,
  140     now_min,
  141     now_ampm):
  142 
  143  url = "http://www.131500.com.au/plan-your-trip/trip-planner?"
  144  values = { "session" : "invalidate",
  145     "itd_cmd" : "invalid",
  146     "itd_includedMeans" : "checkbox",
  147     "itd_inclMOT_5" : "1",
  148     "itd_inclMOT_7" : "1",
  149     "itd_inclMOT_1" : "1",
  150     "itd_inclMOT_9" : "1",
  151     "itd_anyObjFilter_origin" : "2",
  152     "itd_name_origin" : from_sta ,
  153     "itd_anyObjFilter_destination" : "2",
  154     "itd_name_destination" : to_sta,
  155     "itd_itdDate" : now_date,
  156     "itd_itdTripDateTimeDepArr" : "dep",
  157     "itd_itdTimeHour" : now_hour,
  158     "itd_itdTimeMinute" : now_min,
  159     "itd_itdTimeAMPM" : now_ampm,
  160     "x" : "56",
  161     "y" : "11"
  162     }
  163  data = urllib.urlencode(values)
  164  try:
  165   req = urllib2.Request(url, data)
  166  except:
  167   print "Server Error", error
  168   sys.exit(-1)
  169 
  170  response = urllib2.urlopen(req)
  171  html = response.read()
  172 
  173  return html, url, data
  174 
  175 def main(args):
  176  from_sta, to_sta, now_date, now_hour, now_min, now_ampm, print_url =
  177  prepare_param(args)
  178 
  179  print
  180  print "From Station:", from_sta.title()
  181  print "To Station:", to_sta.title()
  182  print "Time:", "%s:%s:%s" % (now_hour, now_min, now_ampm)
  183  print "Date: %s/%s/%s" % (now_date[:4], now_date[4:6],now_date[-2:])
  184 
  185  html, url, data = fetch_html(from_sta, to_sta, now_date, now_hour, now_min,
  186  now_ampm)
  187  if print_url:
  188   print "\n%s%s" % (url, data)
  189 
  190  show_timetable(html, from_sta)
  191 
  192 
  193 if __name__ == "__main__":
  194   main(sys.argv[1:])





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

DokuWiki Beautifier: Floating Table of Contents (TOC) and others

Just put the following code inside  conf/userstyle.css  inside DokuWiki directory.

P.S. What you get from the code:
  1. The table of content moves up/down as you scroll.
  2. "Edit" buttons are a bit more stylish. (comment out if you don't like it)

    1 /* Place customisations to screen mode style sheet here */
    2 
    3 /* table-of-content sidebar*/
    4 div.toc {
    5   position: fixed !important;
    6   top: 1em !important;
    7   bottom: 3em !important;
    8   right: 0px !important;
    9   overflow-y: auto !important;
   10   display: -moz-box !important;
   11   height: 90% !important;
   12   width: 16% !important;
   13 }
   14 
   15 /* reducing page width to save it from going under toc */
   16 div.header, div.bar, div.doc, div.breadcrumbs {width:82% !important}
   17 div.page {
   18   width: 80% !important;
   19   border: 0px #6C9AD0 solid !important;
   20   padding: 1em !important;
   21 }
   22 
   23 /* styling the 'Edit' button */
   24 input[value="Edit"] {
   25 font-weight: bold !important;
   26 font-size:1em !important;
   27 border-width: 0.2em !important;
   28 }
   29 

Wednesday, August 18, 2010

Mounting HTC Android (G1) SD card on Windows 7

  1. Connect phone to the Windows 7 computer with USB cable
  2. On phone, select "Turn on USB storate" from status bar (slides down from top)
  3. On Windows 7, go to Control Panel > Device Manager
  4. Expand/Click on "Disk Drives" and select "HTC Android Phone USB Device"
  5. Right click on "HTC Android Phone USB Device" and Click "Disable"
  6. Now right click again on "HTC Android Phone USB Device" and Click "Enable"
  7. Windows 7 should show you the SD card files now ;)

Tuesday, August 17, 2010

Debian/Ubuntu: Delay in getting SSH login prompt

On some of my Debian/Ubuntu machines, I get an annoying 10-20 seconds delay in seeing the prompt while trying to login. My first guess was - something wrong in key exchange and it's must be falling back with standard password authentication, as can be seen with ssh -vv babil@servername

But the solution seems to be in disabling DNS resolution for the SSH server. I found the solution here: http://www.unix.com/linux/96010-delay-getting-ssh-login-prompt.html. /etc/ssh/sshd_conf needs a "UseDNS no" line, followed by a "sudo /etc/init.d/ssh restart".
Cheers.

Thursday, August 12, 2010

Stopping Gnuplot from flashing a plot window and disappear

In Ubuntu/Debian, Gnuplot tends to use terminal 'X11' if no terminal has been defined before the 'plot' command. The reason for not specifying a terminal could be various (eg. being lazy and ignorant, subsequent use of 'replot' command using other terminal types in several other scripts)

In my case I had a loop of 5x5x5x5 times in my NS-2 script invoking Gnuplot 6 times with every run. The flashing window literally leaves the screen unusable as the Gnuplot X11 terminal window keeps popping up and out in a blink.

A temporary fix (which doesn't require rewriting my whole Gnuplot script set) is to use:
set terminal unknown
Doing this stops the flash with the first 'plot' command. The subsequent 'replot' commands can define their own terminals as needed.