22from requests .cookies import create_cookie
33from user_agent import generate_user_agent
44
5- categories = {}
6-
7-
8- def searchCategories (cats ):
9- global categories
10- for c in cats :
11- if 'Subcategories' in c :
12- searchCategories (c ['Subcategories' ])
13- else :
14- categories [c ['LinkTo' ]] = len (categories )
15-
16-
17- def getCategories ():
18- return categories
19-
20-
21- def getCategory (id ):
22- for cat in categories .keys ():
23- if categories [cat ] == id :
24- return cat
25-
26-
27- def loadSession ():
28- global API , s
29- s = Session ()
30- s .headers ['User-Agent' ] = generate_user_agent ()
31- s .headers ['Accept' ] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
32- s .headers ['Accept-Encoding' ] = 'gzip, deflate, br'
33- s .headers ['Accept-Language' ] = 'ru-RU,ru;q=0.9,kk-KZ;q=0.8,kk;q=0.7,en-US;q=0.6,en;q=0.5'
34-
35- r = s .get ('https://www.wolframalpha.com/input/wpg/categories.jsp?load=true' ).json ()
36- searchCategories (r ['Categories' ]['Categories' ])
37- API = r ['domain' ]
38-
39- def checkProblem (problem , answer ):
40- lvl = problem ['difficulty' ]
41- pid = problem ['id' ]
42- 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 )
46- r = s .get (f'{ API } /input/wpg/checkanswer.jsp?attempt=1&difficulty={ lvl } &load=true&problemID={ pid } &query={ answer } &s={ machine } &type=InputField' ).json ()
47- return {'correct' : r ['correct' ], 'hint' : r ['hint' ], 'solution' : r ['solution' ]}
48-
49-
50- def generateProblem (lvl = 0 , type = 'IntegerAddition' ):
51- lvl = {0 : 'Beginner' , 1 : 'Intermediate' , 2 : 'Advanced' }[lvl ]
52- r = s .get (f'{ API } /input/wpg/problem.jsp?count=1&difficulty={ lvl } &load=1&type={ type } ' ).json ()
53- problems = r ['problems' ]
54- machine = r ['machine' ]
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 }
5+
6+ class Problemator :
7+ def __init__ (self ):
8+ self .categories = {}
9+ self .load_session ()
10+
11+ def search_categories (self , cats ):
12+ for c in cats :
13+ if 'Subcategories' in c :
14+ self .search_categories (c ['Subcategories' ])
15+ else :
16+ self .categories [c ['LinkTo' ]] = len (self .categories )
17+
18+ def get_category (self , id ):
19+ for cat in self .categories .keys ():
20+ if self .categories [cat ] == id :
21+ return cat
22+
23+ def load_session (self ):
24+ self .s = Session ()
25+ self .s .headers ['User-Agent' ] = generate_user_agent ()
26+ self .s .headers ['Accept' ] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
27+ self .s .headers ['Accept-Encoding' ] = 'gzip, deflate, br'
28+ self .s .headers ['Accept-Language' ] = 'ru-RU,ru;q=0.9,kk-KZ;q=0.8,kk;q=0.7,en-US;q=0.6,en;q=0.5'
29+
30+ r = self .s .get ('https://www.wolframalpha.com/input/wpg/categories.jsp?load=true' ).json ()
31+ self .search_categories (r ['Categories' ]['Categories' ])
32+ self .API = r ['domain' ]
33+
34+ def check_problem (self , problem , answer ):
35+ lvl = problem ['difficulty' ]
36+ pid = problem ['id' ]
37+ machine = problem ['machine' ]
38+ for c in problem ['session' ]:
39+ cookie = create_cookie (name = c ['name' ], value = c ['value' ], domain = c ['domain' ])
40+ self .s .cookies .set_cookie (cookie )
41+ r = self .s .get (f'{ self .API } /input/wpg/checkanswer.jsp?attempt=1&difficulty={ lvl } &load=true&problemID={ pid } &query={ answer } &s={ machine } &type=InputField' ).json ()
42+ return {'correct' : r ['correct' ], 'hint' : r ['hint' ], 'solution' : r ['solution' ]}
43+
44+ def generate_problem (self , lvl = 0 , type = 'IntegerAddition' ):
45+ lvl = {0 : 'Beginner' , 1 : 'Intermediate' , 2 : 'Advanced' }[lvl ]
46+ r = self .s .get (f'{ self .API } /input/wpg/problem.jsp?count=1&difficulty={ lvl } &load=1&type={ type } ' ).json ()
47+ problems = r ['problems' ]
48+ machine = r ['machine' ]
49+ cookies = []
50+ for c in self .s .cookies :
51+ if c .name == 'JSESSIONID' :
52+ cookies .append ({'name' : c .name , 'value' : c .value , 'domain' : c .domain })
53+ problem = problems [0 ]
54+ return {'text' : problem ['string_question' ], 'image' : problem ['problem_image' ], 'difficulty' : lvl , 'id' : problem ['problem_id' ], 'machine' : machine , 'session' : cookies }
0 commit comments