Skip to content

Commit 0dbedf5

Browse files
committed
v1.1.0
1 parent b8df045 commit 0dbedf5

File tree

5 files changed

+62
-68
lines changed

5 files changed

+62
-68
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2022 Vladislav Zenkevich
1+
Copyright (c) 2023 Vladislav Zenkevich
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.MD

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
<h1 align="center"> -How to use- </h1>
66

77
```python
8-
from problemator import *
8+
from problemator import Problemator
99
from random import choice
1010

11-
loadSession() # Initialize
12-
print(getCategories()) # See categories
11+
p = Problemator() # Initialize
12+
print(p.categories) # See categories
1313

14-
category = getCategory(0) # Get Addition
14+
category = p.get_category(0) # Get Addition
1515

1616
# LVL: 0 - Beginner; 1 - Intermediate; 2 - Advanced
1717
# Count - Number of problems
1818
# type - Category
19-
problem = generateProblem(lvl=0, type=category) # Generate a problem
19+
problem = p.generate_problem(lvl=0, type=category) # Generate a problem
2020

2121
print(problem['text']) # Text of the problem
2222
print(problem['image']) # Image of the problem
2323
print(problem['difficulty']) # Difficulty of the problem
2424

25-
c = checkProblem(problem, 'x+5') # Check problem, where x+5 - answer
26-
print(c['correct']) # True or False
27-
print(c['hint']) # Image of the Hint
28-
print(c['solution']) # Image of the Solution
25+
result = p.check_problem(problem, 'x+5') # Check problem, where x+5 - answer
26+
print(result['correct']) # True or False
27+
print(result['hint']) # Image of the Hint
28+
print(result['solution']) # Image of the Solution
2929
```

problemator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .problemator import loadSession, getCategories, getCategory, generateProblem, checkProblem
1+
from .problemator import Problemator

problemator/problemator.py

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,53 @@
22
from requests.cookies import create_cookie
33
from 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}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="problemator",
8-
version="1.0.5",
8+
version="1.1.0",
99
author="Maehdakvan",
1010
author_email="visitanimation@google.com",
1111
description="WolframAlpha's Unlimited AI-generated practice problems and answers API wrapper.",

0 commit comments

Comments
 (0)