Skip to content

Commit b8df045

Browse files
committed
v1.0.5
1 parent d0a1c92 commit b8df045

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

README.MD

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ from random import choice
1111
loadSession() # Initialize
1212
print(getCategories()) # See categories
1313

14-
category = getCategoryByID(0) # Get Addition by ID
15-
category = getCategoryByName('Add') # Get Addition by Name
14+
category = getCategory(0) # Get Addition
1615

1716
# LVL: 0 - Beginner; 1 - Intermediate; 2 - Advanced
1817
# Count - Number of problems
1918
# type - Category
20-
problems = generateProblems(count=10, lvl=0, type=category)
21-
problem = choice(problems) # Get random problem
19+
problem = generateProblem(lvl=0, type=category) # Generate a problem
2220

2321
print(problem['text']) # Text of the problem
2422
print(problem['image']) # Image of the problem

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, getCategoryByID, getCategoryByName, generateProblems, checkProblem
1+
from .problemator import loadSession, getCategories, getCategory, generateProblem, checkProblem

problemator/problemator.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
from requests import Session, get
2+
from requests.cookies import create_cookie
23
from user_agent import generate_user_agent
34

45
categories = {}
56

7+
68
def 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

1517
def 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

3327
def loadSession():
@@ -42,18 +36,25 @@ def loadSession():
4236
searchCategories(r['Categories']['Categories'])
4337
API = r['domain']
4438

45-
4639
def 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}

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.4",
8+
version="1.0.5",
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)