Improve perdoliq class

This commit is contained in:
AB
2018-05-11 18:44:38 +03:00
parent fa276a62cc
commit 5b04a2c440
2 changed files with 53 additions and 38 deletions

View File

@@ -5,30 +5,43 @@ import random
config = configparser.ConfigParser() config = configparser.ConfigParser()
def input_line(): # just parse user input and make proper action
allah = [
'>>>'
]
return allah[random.randint(0, len(allah)-1)]
def act(q): def act(q):
if (q == 'h') or (q== 'help'): if (q == 'h') or (q == 'help'):
print('''I don't know how to help you... print('''
`list` - list all test `list` - list all test
`resolve <Subj> <Test>` `resolve <Subj> <Test>`
''') ''')
elif q == 'list': elif q == 'list':
list_all() list_all()
elif q[:7] == 'resolve': elif q[:7] == 'resolve':
# chance to override accuracy
global accuracy
accuracy_new = input('Accuracy level [%s]: ' % accuracy)
if accuracy_new != '':
try:
accuracy = int(accuracy_new)
except:
pass
# set delay if needed
is_delayed = input(
'Wait random time per question and auto commit? [y/N]: ')
if is_delayed == 'y' or is_delayed == 'Y':
is_delayed = True
else:
is_delayed = False
q = q.split(' ') q = q.split(' ')
print(q[1], q[2])
if len(q) != 3: if len(q) != 3:
print('''Specify Subj number and Test number. print('''Specify Subj number and Test number.
List it with `list` command List it with `list` command
example: example:
resolve 4 1''') resolve 4 1''')
return 1 return 1
app.resolve(int(q[1]), int(q[2]), accuracy) app.resolve(int(q[1]), int(q[2]), accuracy, is_delayed=is_delayed)
def list_all(): def list_all():
i = 0 i = 0
@@ -39,26 +52,28 @@ def list_all():
for test in app.subjects[subj]: for test in app.subjects[subj]:
print("\t[%s] %s" % (j, test)) print("\t[%s] %s" % (j, test))
j += 1 j += 1
# read config. in case of missing config create new one.
try: try:
config.read("creds.ini") config.read("creds.ini")
username = config['creds']['user'] username = config['creds']['user']
password = config['creds']['pass'] password = config['creds']['pass']
accuracy = config['mics']['accuracy'] accuracy = int(config['mics']['accuracy'])
except: except:
print("Couldn't find appropriate config file. Let's create new.") print("Couldn't find appropriate config file. Let's create new.")
print("Provide credentials for fesmu.ru/eport/eport/ below") print("Provide credentials for fesmu.ru/eport/eport/ below")
username = input('User ID %s ' % input_line()) username = input('User ID %s ' % '>>>')
password = input('Password %s ' % input_line()) password = input('Password %s ' % '>>>')
config['creds'] = {} config['creds'] = {}
config['creds']['user'] = username config['creds']['user'] = username
config['creds']['pass'] = password config['creds']['pass'] = password
print("Also you are able to set desired level of accuracy in percent (%).") print("Also you are able to set desired level of accuracy in percent (%).")
accuracy = int(input('Accuracy %s ' % input_line())) accuracy = int(input('Accuracy %s ' % '>>>'))
while (accuracy < 0) or (accuracy > 100): while (accuracy < 0) or (accuracy > 100):
print("Are you stupid? Try again.") print("Are you stupid? Try again.")
accuracy = int(input('Accuracy %s ' % input_line())) accuracy = int(input('Accuracy %s ' % '>>>'))
config['mics'] = {} config['mics'] = {}
config['mics']['accuracy'] = str(accuracy) config['mics']['accuracy'] = str(accuracy)
with open('creds.ini', 'w') as configfile: with open('creds.ini', 'w') as configfile:
@@ -66,7 +81,9 @@ except:
app = Main(username, password) app = Main(username, password)
# make auth
app.auth() app.auth()
# update tests
app.get_tests() app.get_tests()
print(''' print('''
@@ -75,6 +92,5 @@ Type h or help for help.
''') ''')
while True: while True:
q = input('Command %s ' % input_line()) q = input('Command %s ' % '>>>')
act(q) act(q)

39
main.py
View File

@@ -17,6 +17,7 @@ class Main:
self.name = '' self.name = ''
self.subjects = {} self.subjects = {}
# make auth
def auth(self): def auth(self):
r = requests.get(settings.fesmu_root_url) r = requests.get(settings.fesmu_root_url)
for c in r.cookies: for c in r.cookies:
@@ -43,6 +44,9 @@ class Main:
'', soup.find(id="ctl00_MainContent_Label1").get_text())[14:] '', soup.find(id="ctl00_MainContent_Label1").get_text())[14:]
logging.info('Current user is %s', self.name) logging.info('Current user is %s', self.name)
return self.SessionId
# update and return list of subjs and tests
def get_tests(self): def get_tests(self):
r = requests.get( r = requests.get(
settings.fesmu_root_url + 'studtst1.aspx', settings.fesmu_root_url + 'studtst1.aspx',
@@ -70,7 +74,7 @@ class Main:
test.get_text())) test.get_text()))
i += 1 i += 1
#print(self.subjects) return self.subjects
def start_test(self, pred, test): def start_test(self, pred, test):
# start test # start test
@@ -161,6 +165,7 @@ class Main:
(q_number, answers)) (q_number, answers))
return answers return answers
# mark answers into requested question
def answer(self, q_number, answers): def answer(self, q_number, answers):
def gen_a(a_count, answers): def gen_a(a_count, answers):
a = {} a = {}
@@ -205,6 +210,7 @@ class Main:
logging.info("Send correct answer for %s. It's %s" % (q_number, logging.info("Send correct answer for %s. It's %s" % (q_number,
answers)) answers))
# commit test
def finish_test(self): def finish_test(self):
requests.post( requests.post(
settings.fesmu_root_url + 'studtst2.aspx', settings.fesmu_root_url + 'studtst2.aspx',
@@ -215,44 +221,37 @@ class Main:
data=settings.scam_data_10, data=settings.scam_data_10,
cookies={'ASP.NET_SessionId': self.SessionId}) cookies={'ASP.NET_SessionId': self.SessionId})
def resolve(self, subj, test, accuracy): def resolve(self, subj, test, accuracy, is_delayed=False):
# renew auth
self.auth() self.auth()
accuracy = int(accuracy) # get count of questions
# chance to override accuracy
accuracy_new = input('Accuracy level [%s]: ' % accuracy)
if accuracy_new != '':
try:
accuracy = int(accuracy_new)
except:
pass
is_delayd = input('Wait random time per question and auto commit? [y/N]: ')
if is_delayd == 'y' or is_delayd == 'Y':
is_delayd = True
else:
is_delayd = False
q_count = self.start_test(subj, test) q_count = self.start_test(subj, test)
def sleep_rand(): def sleep_rand():
delay = randint(20, 40) delay = randint(20, 40)
logging.info("Going to wait %s sec for this question." % delay) logging.info("Going to wait %s sec for this question." % delay)
time.sleep(delay) time.sleep(delay)
# applying accuracy here. trying to make resulting # applying accuracy here. trying to make resulting
# accuracy no less than requested. # accuracy NO LESSthan requested.
spoil_count = int(q_count - q_count * (accuracy / 100)) spoil_count = int(q_count - q_count * (accuracy / 100))
if int(spoil_count / q_count * (-100) + 100) < accuracy: if int(spoil_count / q_count * (-100) + 100) < accuracy:
spoil_count -= 1 spoil_count -= 1
# skip random `spoil_count` questions. # skip random `spoil_count` questions.
# Choose questions which going to be skipped
skip_this = [] skip_this = []
for i in range(0, spoil_count): for i in range(0, spoil_count):
skip_this.append(randint(1, q_count + 1)) skip_this.append(randint(1, q_count + 1))
# start resolve test
for i in range(1, q_count + 1): for i in range(1, q_count + 1):
if is_delayd == True: if is_delayed == True:
sleep_rand() sleep_rand()
if i not in skip_this: if i not in skip_this:
prediction = self.predict(i) prediction = self.predict(i)
self.answer(i, prediction) self.answer(i, prediction)
if is_delayd == True:
self.finish_test()
# make autocommit
if is_delayed == True:
self.finish_test()