11from requests import Session , get
2+ from requests .cookies import create_cookie
23from user_agent import generate_user_agent
34
45categories = {}
56
7+
68def searchCategories (cats ):
79 global categories
810 for c in cats :
911 if 'Subcategories' in c :
1012 searchCategories (c ['Subcategories' ])
1113 else :
12- categories [c ['LinkTo' ]] = { 'name' : c [ 'Display' ], 'id' : len (categories )}
14+ categories [c ['LinkTo' ]] = len (categories )
1315
1416
1517def getCategories ():
1618 return categories
1719
1820
19- def getCategoryByID (id ):
20- for cat in categories .keys ():
21- if id == categories [cat ]['id' ]:
22- return cat
23- return None
24-
25-
26- def getCategoryByName (name ):
21+ def getCategory (id ):
2722 for cat in categories .keys ():
28- if name == categories [cat ][ 'name' ] :
23+ if categories [cat ] == id :
2924 return cat
30- return None
3125
3226
3327def loadSession ():
@@ -42,18 +36,25 @@ def loadSession():
4236 searchCategories (r ['Categories' ]['Categories' ])
4337 API = r ['domain' ]
4438
45-
4639def checkProblem (problem , answer ):
4740 lvl = problem ['difficulty' ]
4841 pid = problem ['id' ]
4942 machine = problem ['machine' ]
43+ for c in problem ['session' ]:
44+ cookie = create_cookie (name = c ['name' ], value = c ['value' ], domain = c ['domain' ])
45+ s .cookies .set_cookie (cookie )
5046 r = s .get (f'{ API } /input/wpg/checkanswer.jsp?attempt=1&difficulty={ lvl } &load=true&problemID={ pid } &query={ answer } &s={ machine } &type=InputField' ).json ()
5147 return {'correct' : r ['correct' ], 'hint' : r ['hint' ], 'solution' : r ['solution' ]}
5248
5349
54- def generateProblems (lvl = 0 , type = 'IntegerAddition' , count = 1 ):
50+ def generateProblem (lvl = 0 , type = 'IntegerAddition' ):
5551 lvl = {0 : 'Beginner' , 1 : 'Intermediate' , 2 : 'Advanced' }[lvl ]
56- r = s .get (f'{ API } /input/wpg/problem.jsp?count={ count } &difficulty={ lvl } &load=1&type={ type } ' ).json ()
52+ r = s .get (f'{ API } /input/wpg/problem.jsp?count=1 &difficulty={ lvl } &load=1&type={ type } ' ).json ()
5753 problems = r ['problems' ]
5854 machine = r ['machine' ]
59- return [{'text' : problem ['string_question' ], 'image' : problem ['problem_image' ], 'difficulty' : lvl , 'id' : problem ['problem_id' ], 'machine' : machine } for problem in problems ]
55+ cookies = []
56+ for c in s .cookies :
57+ if c .name == 'JSESSIONID' :
58+ cookies .append ({'name' : c .name , 'value' : c .value , 'domain' : c .domain })
59+ problem = problems [0 ]
60+ return {'text' : problem ['string_question' ], 'image' : problem ['problem_image' ], 'difficulty' : lvl , 'id' : problem ['problem_id' ], 'machine' : machine , 'session' : cookies }
0 commit comments