Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. from kol.Session import Session
  2. from kol.request.ChoiceRequest import ChoiceRequest as CR
  3. from kol.request.UseItemRequest import UseItemRequest as UR
  4. import re
  5.  
  6. username = ""
  7. password = ""
  8. include_action_data = True
  9.  
  10.  
  11.  
  12. number_reg = re.compile("(?<=the box clearly says ")\d+")
  13. choice_reg = re.compile("(?<=submit value=\")[^\"]+")
  14. face_reg = re.compile("(?<=the )\w+(?= face)")
  15.  
  16. deepest=-1
  17. deepest_full=-1
  18. choice=-1
  19. nonBuzz = []
  20. myData= []
  21. myChoices = []
  22.  
  23. def sixbuzz():
  24.     lastRow = myData[deepest-1]
  25.     return len(lastRow)==6 and len(list(set(lastRow)))==1 and "Z" in lastRow
  26.    
  27. def parse_response(body):
  28.     datapoint = None
  29.     if "the box clearly says" in body:
  30.         datapoint = number_reg.search(body).group()
  31.     elif "hear a quiet" in body:
  32.         datapoint = "C"
  33.     elif "the box makes a loud" in body:
  34.         datapoint = "B"
  35.     elif "box makes a harsh buzzing noise" in body:
  36.         datapoint = "Z"
  37.        
  38.     if datapoint is None:
  39.         f = open("dump.html","w")
  40.         f.write(body)
  41.         f.close()
  42.         raise Exception("Unknown response, dumped to file dump.html")
  43.        
  44.     return datapoint
  45.    
  46. def parse_actions(body):
  47.     myList = []
  48.     actions = [x.lower() for x in choice_reg.findall(body)[:6]]
  49.     for act in actions:
  50.         if "push" in act:
  51.             current = "push_"
  52.         elif "counter" in act:
  53.             current = "ccw_"
  54.         else:
  55.             current = "cw_"
  56.         myList.append(current+face_reg.search(act).group())
  57.     myChoices.append(myList)
  58.    
  59. def reset():
  60.     print("Resetting box")
  61.     make_choice=CR(s,525,7)
  62.     try:
  63.         make_choice.doRequest()
  64.     except:
  65.         pass
  66.    
  67.     use_box = UR(s,5054)
  68.     use_box.doRequest()
  69.    
  70.     while True:
  71.         make_choice=CR(s,525,1)
  72.         make_choice.doRequest()
  73.         if parse_response(make_choice.responseText)=="Z":
  74.             break
  75.     print("Box reset")
  76.    
  77. def write_file():
  78.     f=open("datafile.csv","w")
  79.     for line1,line2 in zip(myChoices,myData):
  80.         if include_action_data:
  81.             f.write(",".join(line1)+"\n")
  82.         f.write(",".join(line2)+"\n")
  83.     f.close()
  84.  
  85. s = Session()
  86. s.login(username,password)
  87.  
  88. print "Logged in!"
  89.  
  90. reset()
  91.  
  92. while True:
  93.     print "Starting new choice with deepest "+`deepest`+" and deepest_full "+`deepest_full`
  94.     i=0
  95.    
  96.     while True:
  97.         if i>deepest:
  98.             myData.append([])
  99.             nonBuzz.append(None)
  100.             deepest=i
  101.            
  102.         if i<=deepest_full:
  103.             make_choice=CR(s,525,nonBuzz[i])
  104.             make_choice.doRequest()
  105.         else:
  106.             choice = len(myData[i])+1
  107.             make_choice=CR(s,525,choice)
  108.             make_choice.doRequest()
  109.             result = parse_response(make_choice.responseText)
  110.             print "Took choice "+`choice`+" at "+`i`+" and got "+result
  111.             if not len(myData[i]):
  112.                 parse_actions(make_choice.responseText)
  113.                
  114.             myData[i].append(result)
  115.             if len(myData[i])==6:
  116.                 deepest_full=i
  117.             if result == "Z":
  118.                 break
  119.             elif nonBuzz[i] is None:
  120.                 nonBuzz[i]=choice
  121.         i+=1
  122.  
  123.     write_file()
  124.     print("File written")
  125.    
  126.     if sixbuzz():
  127.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement