def enq(train_info):  # enquires  availability info of a train
    br = mech_fn.init_br()
    url1 = "http://www.indianrail.gov.in/seat_Avail.html"
    date1 = datetime.datetime.now() + datetime.timedelta(days=train_info["offset"])

    res1 = br.open(url1)
    try:
        br.select_form(nr=0)
    except:
        mech_fn.ungzipResponse(res1, br)
        br.select_form(nr=0)

    br.form["lccp_trnno"] = str(train_info["train_no"])
    br.form["lccp_day"] = str(date1.day)
    br.form["lccp_month"] = str(date1.month)
    br.form["lccp_srccode"] = train_info["src_stn"]
    br.form["lccp_dstncode"] = train_info["dest_stn"]
    br.form["lccp_quota"] = ["CK"]
    br.form["lccp_class1"] = [train_info["class"]]

    html1 = br.submit().read()
    date2 = datetime.datetime.now()
    br.close()
    ####################

    obj1 = process_html(html1)

    [cl1, cl2] = obj1

    f1.write("%15s %15s-- %2s %s\n" % (cl1, cl2, date2.time().second, train_info["train_name"]))
    return [cl1, cl2, date2.strftime("%I:%M:%S"), train_info["src_stn"] + "->", train_info["dest_stn"]]
def enq(train_info):    # enquires  availability info of a train
    br = mech_fn.init_br()
    url1 = 'http://www.indianrail.gov.in/seat_Avail.html'
    date1 = datetime.datetime.now() + datetime.timedelta(days= train_info['offset'] )
    
    res1= br.open(url1)    
    try:
        br.select_form(nr=0)
    except:
        mech_fn.ungzipResponse(res1, br)
        br.select_form(nr=0)
    
    br.form['lccp_trnno'] =  str( train_info['train_no'] )
    br.form['lccp_day'] = str( date1.day)
    br.form['lccp_month'] = str( date1.month )
    br.form['lccp_srccode'] = train_info[ 'src_stn']
    br.form['lccp_dstncode'] = train_info[ 'dest_stn']
    br.form['lccp_quota'] = ['CK']
    br.form['lccp_class1'] = [ train_info['class'] ]
      
    html1 = br.submit().read()
    date2 = datetime.datetime.now()
    br.close()
    ####################
    
    
    obj1 = process_html(html1)
    

    [cl1, cl2] =obj1
    
    f1.write("%15s %15s-- %2s %s\n" % (cl1, cl2, date2.time().second, train_info['train_name']  ))
    return [ cl1, cl2, date2.strftime( '%I:%M:%S') , train_info['src_stn']+"->", train_info['dest_stn'] ]
def empty_query():  # just to check if the connection to indian rail is working
    try:
        br = mech_fn.init_br()
        url1 = "http://www.indianrail.gov.in/seat_Avail.html"
        print "status: ", [br.open(url1).code]
    except:
        print "***Exception in empty Query : unable to open****"
        print traceback.print_exc()
        print "\n\n\n**** Continuing ****"
def enq():
    
    url1 = 'http://www.indianrail.gov.in/seat_Avail.html'
     
    br = init_br()
     
    br.open(url1)
     
#     for f in br.forms():
#         print f
      
    br.select_form(nr=0)
    br.form['lccp_trnno'] = '12451'
    br.form['lccp_day'] = '12'
    br.form['lccp_month'] = '2'
    br.form['lccp_srccode'] = 'CNB'
    br.form['lccp_dstncode'] = 'NDLS'
    br.form['lccp_quota'] = ['GN']
      
    html1 = br.submit().read()
     
#     html1 = open('test1.html', 'r').read().lower()
    # print html1
    
    tree1 = html.fromstring(html1)

    elem_sl = tree1.xpath('/html/body/table/tr/td/table/tr[1]/td/table/tr[3]/td/table/tr/td/table/tr/td[2]/table/tr[2]/td/table/tr[2]/td[2]/table/tr[2]/td/table/div/tr[4]/td/'+
                'table/tbody/tr/td[3]')
    
    elem_ac = tree1.xpath('/html/body/table/tr/td/table/tr[1]/td/table/tr[3]/td/table/tr/td/table/tr/td[2]/table/tr[2]/td/table/tr[2]/td[2]/table/tr[2]/td/table/div/tr[4]/td/'+
                'table/tbody/tr/td[4]')
    
    
    
    # for elem in elem_sl:
    str1 =  elem_sl[0].text_content()
    
#     str1,",", datetime.datetime.now()

    f1.write("%s,%s\n"%(str1,datetime.datetime.now() ))
    f2.write("%s,%s\n"%(str1,datetime.datetime.now() ))