Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DedInc committed May 29, 2023
1 parent b8df045 commit 0dbedf5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 68 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2022 Vladislav Zenkevich
Copyright (c) 2023 Vladislav Zenkevich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
18 changes: 9 additions & 9 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
<h1 align="center"> -How to use- </h1>

```python
from problemator import *
from problemator import Problemator
from random import choice

loadSession() # Initialize
print(getCategories()) # See categories
p = Problemator() # Initialize
print(p.categories) # See categories

category = getCategory(0) # Get Addition
category = p.get_category(0) # Get Addition

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

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

c = checkProblem(problem, 'x+5') # Check problem, where x+5 - answer
print(c['correct']) # True or False
print(c['hint']) # Image of the Hint
print(c['solution']) # Image of the Solution
result = p.check_problem(problem, 'x+5') # Check problem, where x+5 - answer
print(result['correct']) # True or False
print(result['hint']) # Image of the Hint
print(result['solution']) # Image of the Solution
```
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, getCategory, generateProblem, checkProblem
from .problemator import Problemator
106 changes: 50 additions & 56 deletions problemator/problemator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,53 @@
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']] = len(categories)


def getCategories():
return categories


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


def loadSession():
global API, s
s = Session()
s.headers['User-Agent'] = generate_user_agent()
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'
s.headers['Accept-Encoding'] = 'gzip, deflate, br'
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'

r = s.get('https://www.wolframalpha.com/input/wpg/categories.jsp?load=true').json()
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 generateProblem(lvl=0, type='IntegerAddition'):
lvl = {0: 'Beginner', 1: 'Intermediate', 2: 'Advanced'}[lvl]
r = s.get(f'{API}/input/wpg/problem.jsp?count=1&difficulty={lvl}&load=1&type={type}').json()
problems = r['problems']
machine = r['machine']
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}

class Problemator:
def __init__(self):
self.categories = {}
self.load_session()

def search_categories(self, cats):
for c in cats:
if 'Subcategories' in c:
self.search_categories(c['Subcategories'])
else:
self.categories[c['LinkTo']] = len(self.categories)

def get_category(self, id):
for cat in self.categories.keys():
if self.categories[cat] == id:
return cat

def load_session(self):
self.s = Session()
self.s.headers['User-Agent'] = generate_user_agent()
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'
self.s.headers['Accept-Encoding'] = 'gzip, deflate, br'
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'

r = self.s.get('https://www.wolframalpha.com/input/wpg/categories.jsp?load=true').json()
self.search_categories(r['Categories']['Categories'])
self.API = r['domain']

def check_problem(self, 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'])
self.s.cookies.set_cookie(cookie)
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()
return {'correct': r['correct'], 'hint': r['hint'], 'solution': r['solution']}

def generate_problem(self, lvl=0, type='IntegerAddition'):
lvl = {0: 'Beginner', 1: 'Intermediate', 2: 'Advanced'}[lvl]
r = self.s.get(f'{self.API}/input/wpg/problem.jsp?count=1&difficulty={lvl}&load=1&type={type}').json()
problems = r['problems']
machine = r['machine']
cookies = []
for c in self.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.5",
version="1.1.0",
author="Maehdakvan",
author_email="[email protected]",
description="WolframAlpha's Unlimited AI-generated practice problems and answers API wrapper.",
Expand Down

0 comments on commit 0dbedf5

Please sign in to comment.