narendra sisodiya wrote:
> On Tue, Jul 15, 2008 at 11:45 AM, Shamail Tayyab <[EMAIL PROTECTED] 
> <mailto:[EMAIL PROTECTED]>> wrote:
>
>     Warning: No one is authorised to make any type of commercial usage
>     like
>     putting web advertisements or SMS service and
>     Reproducing/Transmitting/Storing in its database, any content of
>     www.indianrail.gov.in <http://www.indianrail.gov.in>
>     <http://www.indianrail.gov.in> website, without
>     prior written permission from Indian Railways. Violators will be
>     severely prosecuted.
>
>
>
> Oh ya, I have read this notice before also, And i do not think that 
> the script do not violate any such violation,
> I am viewing page in the command line browser, and submitting the pnr 
> number using some standard way in perl. Technically I am submitting 
> and taking response in smarter way.
> Also i am not using this for commercial purpose.
> releasing this in GPLv2 is meant for sharing, just like a trick or tweak.
>
> Still i am not sure -- "Will Indian Railway Guys come to me for any 
> violation ? "
>
>
> PS: @Parser : Initially I was also motivated for parser but later I 
> found there is no need for parser in this case. We can just view the 
> response in command line browser. But please send me that parser, I 
> want to study !!
Ya! With script i didn't meant that. I was talking about the usage 
aspect and specifically our work. And yes the script is doing nothing 
but opening it in text browser. Here is that parser...

#! /usr/bin/env python

from HTMLParser import HTMLParser
import httplib

""" Exception Class """
class NoPNRException (Exception):
   def __init__ ( self ):
      pass
   def __str__ ( self ):
      return "No Such PNR Number"

""" Makes and HTTP POST request to indianrail.gov.in and fetches the 
html """
class Requester:
   def result ( self, pnri, pnrl ):
      SoapMessage = 
"lccp_pnrno1="+pnri+"&lccp_pnrno2="+pnrl+"&submitpnr=Wait+for+PNR+status%21"

      webservice = httplib.HTTP("www.indianrail.gov.in")
      webservice.putrequest("POST", "/cgi_bin/inet_pnrstat_cgi.cgi")
      webservice.putheader("User-Agent", "Mozilla/5.0 (X11; U; Linux 
i686; en-US; rv:1.8.1.13) Gecko/20080325 Nothing Authentic Bot.")
      webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
      webservice.putheader("Content-length", "%d" % len(SoapMessage))
      webservice.endheaders()
      webservice.send(SoapMessage)

      statuscode, statusmessage, header = webservice.getreply()
      res = webservice.getfile().read()
      return res


""" Parses the result returned by indianrail """
class MyHTMLParser (HTMLParser):
   def handle_data ( self, data ):
      if data == "\n":
         return
      if "FLUSHED" in data:
         raise NoPNRException
      try:
        if self.previous == "PNR Number":
          print "PNR:", data
          self.answer.append ( data )
        if self.previous == "Charting":
          print "Chart:", data.strip ()
          self.answer.append ( data.strip () )
      except:
         self.previous = ""
         self.answer = []

      try:
         if self.start < 9 and self.start != 0:
            print "Stat:", data
            self.answer.append ( data )
            self.start += 1
      except:
         self.start = 0

      try:
         if self.poll == "true" and self.fieldctr < 2:
            print "Passenger", data
            self.answer.append ( data )
            self.fieldctr += 1
         if self.fieldctr == 2:
            self.fieldctr = 0
            self.poll = "false"
      except AttributeError:
         self.poll = "false"
         self.fieldctr = 0
    

      for i in [ "1", "2", "3", "4", "5", "6" ]:
         if "Passenger "+i in data:
            self.answer.append ( "Passenger " + i )
            self.poll = "true"

      if "PNR Number" in data:
         self.previous = "PNR Number"
      elif "Charting Status" in data:
         self.previous = "Charting"
      else:
         self.previous = ""

      if data == "1":
         self.start = 1
   def list ( self ):
      return self.answer

""" Test Case """
a = MyHTMLParser ()
try:
  a.feed ( Requester().result ( "225", "0199127" ) )
except NoPNRException:
   print 'NoSuchPNRNumber'


This was supposed to be a package, so in last a test case at last!

Regards!

-- 
Shamail Tayyab
shamail [at] inbox [dot] com
8 5 6 4 5 0 8 1 7 9 - 1 9 + (right to left without spaces)
<<Bringing Linux and Mobiles one step closer>>
Blog at: http://tuxatwork.blogspot.com


_______________________________________________
ilugd mailinglist -- ilugd@lists.linux-delhi.org
http://frodo.hserus.net/mailman/listinfo/ilugd
Archives at: http://news.gmane.org/gmane.user-groups.linux.delhi 
http://www.mail-archive.com/ilugd@lists.linux-delhi.org/

Reply via email to