-
Notifications
You must be signed in to change notification settings - Fork 2
/
getquestions.py
61 lines (45 loc) · 1.19 KB
/
getquestions.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
import requests
import json
import sys
import random
def writeOUT(outDict):
JSONstr = json.dumps(outDict)
#file = open('output/getquestionspyOUT.json', 'w') # writes to the out file, read by the php script
#f.write(JSONstr)
#f.close()
#### these will be uncommented if the latter doesn't work on the windows server
print JSONstr
def getJSON(url):
tempStore = requests.get(url)
searchResults = tempStore.text
JSONdict = json.loads(searchResults)
return JSONdict
def getResults(id):
search = 'https://api.quizlet.com/2.0/sets/' + id + '/terms?client_id=RFsBbdeZFt&whitespace=1' # quizlet api terms request url
results = getJSON(search)
return results
def perform(ids): # the entry point
idList = ids.split(',') # read csv values
outDict = {}
cards = []
for id in idList:
id.strip()
results = getResults(id)
cards = cards + results
random.shuffle(cards)
i = 0
while i < 5:
card = cards[i]
q = card['term']
a = card['definition']
outDict[q] = a # i.e. {term:definition, term:definition...}
i = i + 1
writeOUT(outDict)
return outDict
if(__name__ == "__main__"): # if not run with other input
args = sys.argv
try:
argIds = args[1]
except:
exit()
perform(argIds)