Skip to content

Commit

Permalink
v1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
DedInc committed Nov 18, 2022
1 parent d0a1c92 commit b8df045
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
6 changes: 2 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ from random import choice
loadSession() # Initialize
print(getCategories()) # See categories

category = getCategoryByID(0) # Get Addition by ID
category = getCategoryByName('Add') # Get Addition by Name
category = getCategory(0) # Get Addition

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

print(problem['text']) # Text of the problem
print(problem['image']) # Image of the problem
Expand Down
2 changes: 1 addition & 1 deletion problemator/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .problemator import loadSession, getCategories, getCategoryByID, getCategoryByName, generateProblems, checkProblem
from .problemator import loadSession, getCategories, getCategory, generateProblem, checkProblem
31 changes: 16 additions & 15 deletions problemator/problemator.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
from requests import Session, get
from requests.cookies import create_cookie
from user_agent import generate_user_agent

categories = {}


def searchCategories(cats):
global categories
for c in cats:
if 'Subcategories' in c:
searchCategories(c['Subcategories'])
else:
categories[c['LinkTo']] = {'name': c['Display'], 'id': len(categories)}
categories[c['LinkTo']] = len(categories)


def getCategories():
return categories


def getCategoryByID(id):
for cat in categories.keys():
if id == categories[cat]['id']:
return cat
return None


def getCategoryByName(name):
def getCategory(id):
for cat in categories.keys():
if name == categories[cat]['name']:
if categories[cat] == id:
return cat
return None


def loadSession():
Expand All @@ -42,18 +36,25 @@ def loadSession():
searchCategories(r['Categories']['Categories'])
API = r['domain']


def checkProblem(problem, answer):
lvl = problem['difficulty']
pid = problem['id']
machine = problem['machine']
for c in problem['session']:
cookie = create_cookie(name=c['name'], value=c['value'], domain=c['domain'])
s.cookies.set_cookie(cookie)
r = s.get(f'{API}/input/wpg/checkanswer.jsp?attempt=1&difficulty={lvl}&load=true&problemID={pid}&query={answer}&s={machine}&type=InputField').json()
return {'correct': r['correct'], 'hint': r['hint'], 'solution': r['solution']}


def generateProblems(lvl=0, type='IntegerAddition', count=1):
def generateProblem(lvl=0, type='IntegerAddition'):
lvl = {0: 'Beginner', 1: 'Intermediate', 2: 'Advanced'}[lvl]
r = s.get(f'{API}/input/wpg/problem.jsp?count={count}&difficulty={lvl}&load=1&type={type}').json()
r = s.get(f'{API}/input/wpg/problem.jsp?count=1&difficulty={lvl}&load=1&type={type}').json()
problems = r['problems']
machine = r['machine']
return [{'text': problem['string_question'], 'image': problem['problem_image'], 'difficulty': lvl, 'id': problem['problem_id'], 'machine': machine} for problem in problems]
cookies = []
for c in s.cookies:
if c.name == 'JSESSIONID':
cookies.append({'name': c.name, 'value': c.value, 'domain': c.domain})
problem = problems[0]
return {'text': problem['string_question'], 'image': problem['problem_image'], 'difficulty': lvl, 'id': problem['problem_id'], 'machine': machine, 'session': cookies}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="problemator",
version="1.0.4",
version="1.0.5",
author="Maehdakvan",
author_email="visitanimation@google.com",
description="WolframAlpha's Unlimited AI-generated practice problems and answers API wrapper.",
Expand Down

0 comments on commit b8df045

Please sign in to comment.