-
Notifications
You must be signed in to change notification settings - Fork 3
/
quizlet.py
105 lines (89 loc) · 3.06 KB
/
quizlet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
import json
import os
import re
import sys
from quizlet_secret import QUIZLET_CLIENT_ID
###########################################################################
# Constants
###########################################################################
SET_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "sets")
###########################################################################
# Helper functions
###########################################################################
def get_answer_parts(termStr):
answerPartList = filter(None, termStr.split("\n"))
return answerPartList
def get_keyterms(answerPart):
keyterms = re.findall(r'\[[^]]+\]', answerPart)
# get rid of surrounding brackets
for i,keyterm in enumerate(keyterms):
keyterms[i] = keyterm[1:-1]
return keyterms
def user_answer_index(answer, parts):
answer = answer.lower()
for i, part in enumerate(parts):
keyterms = get_keyterms(part)
nonMatchedTerm = 0
for keyterm in keyterms:
keyterm = keyterm.lower()
if answer.find(keyterm) == -1:
nonMatchedTerm += 1
if nonMatchedTerm == 0:
return i
return -1
def make_quizlet_request(endpoint):
params = {"client_id": QUIZLET_CLIENT_ID, "whitespace": 0}
apiPrefix = "https://api.quizlet.com/2.0"
url = os.path.join(apiPrefix, endpoint)
r = requests.get(url=url, params=params)
dictFromJSON = json.loads(r.content)
# Force status code key. Quizlet doesn't put one in for 200, only errors
dictFromJSON['http_code'] = r.status_code
return dictFromJSON
def get_flashcard_set(setID):
return make_quizlet_request("sets/%s" % setID)
def save_flashcard_set_terms_to_file(flashcardSet, f):
termsJSON = json.dumps(flashcardSet['terms'])
f.write(termsJSON)
def load_flashcard_set_terms_from_file(f):
termJSON = f.read()
return json.loads(termJSON)
def check_answer(userAnswer, answerParts):
'''
If userAnswer is in the answerParts list, remove it from the list and
return True. Otherwise return False
'''
answerIndex = user_answer_index(userAnswer, answerParts)
if answerIndex != -1:
answerParts.pop(answerIndex)
return True
else:
return False
def hintify(answerPart):
'''
Return a string signifying a hint of an answerPart
'''
answerStr = list(answerPart)
inBracket = False
i = 0
startOfNewWord = False
while i < len(answerStr):
if not inBracket and answerStr[i] != '[':
i += 1
elif not inBracket and answerStr[i] == '[':
inBracket = True
i += 2
elif inBracket and answerStr[i] != ']':
if answerStr[i] == " ":
startOfNewWord = True
else:
if startOfNewWord:
startOfNewWord = False
else:
answerStr[i] = "_"
i += 1
elif inBracket and answerStr[i] == ']':
inBracket = False
i += 1
return "".join(answerStr)