# coding: utf-8
from lxml import etree
import base64
import time
import codecs
import ctypes
import re
from grab import Grab
import logging
import sys
import locale
from sys import stderr
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter('cp866')(sys.stdout,'replace')
nextstep = "ctl00$plhMain$cboVAC"
nextstep1 = "ctl00$plhMain$cboVisaCategory"
errors = "We are sorry for the inconvenience"
errors1 = "Invalid Attempt"
def go2():
g.set_input('ctl00$plhMain$cboVAC', "8")
g.set_input('ctl00$plhMain$cboPurpose', "1")
g.submit()
tmp = g.response.body
if nextstep1 in g.response.body :
return 2
else:
return 0
def go3():
if errors in g.response.body :
return 0
else:
g.set_input('ctl00$plhMain$cboVisaCategory',"239")
g.set_input('__EVENTTARGET', "239")
g.submit(submit_name='ctl00$plhMain$btnSubmit')
tmp = g.response.body
if errors1 in g.response.body :
return 0
#if errors in g.response.body :
# return 0
return 2
def go1():
if "EVENTTARGET" in g.response.body :
time.sleep(3)
g.set_input('__EVENTTARGET', "ctl00$plhMain$lnkSchApp")
g.submit()
tmp = g.response.body
if nextstep in g.response.body :
return 2
else:
return 0
else:
return 0
chekurl = "visaservices.firm.in/Poland-Ukraine/Ukranian/scheduleanappointment.html"
g = Grab(charset='utf-8', reuse_cookies=False, user_agent='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)', referer='visaservices.firm.in/Poland-Ukraine/Ukranian/scheduleanappointment.html', hammer_mode=True, debug_post=False, hammer_timeouts=((90, 90),(90, 90)))
g.setup(log_dir='C:\\Log1\\')
g.go(chekurl)
tmp = g.response.body
chekurl = tmp.split("<iframe src=")[1].split("width")[0].split('"')[1]
time.sleep(3)
g.go(chekurl)
print chekurl
n=1
while n > 0:
if go1() > 1:
print 1
if go2() > 1:
time.sleep(4)
print 2
if go3() > 1:
print 3
time.sleep(4)
n = 0-1
tmp = g.response.body
errors2 = "No date(s) available for appointment at"
if errors2 in g.response.body :
print "No date(s) available"
else :
g.submit()
print "done. sleeping 10 sec"
time.sleep(10)