-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.py
77 lines (73 loc) · 2.69 KB
/
API.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
try:
from tkinter.messagebox import showinfo
except:
print("tkinter is niet (correct) geinstalleerd.")
exit()
try:
from PIL import Image, ImageTk
except:
showinfo("ERROR","pillow is niet (correct) geinstalleerd.")
exit()
import hashlib
import time
import json
import random
import urllib.request
import urllib.parse
import re
import io
questionBuffer = []
def sendMarvelRequest(request):
'stuurt een aanvraag naar de Marvel API'
loginInfo = json.load(open('apikey.json', 'r'))
stamp = str(time.time())
pubkey = loginInfo['pubkey']
privatekey = loginInfo['privatekey']
hashString = stamp + privatekey + pubkey
hash = hashlib.md5(hashString.encode()).hexdigest()
jsondata=urllib.request.urlopen(f'https://gateway.marvel.com/v1/public/{request}&ts={stamp}&apikey={pubkey}&hash={hash}').read()
return json.loads(jsondata)['data']['results']
def selectCharacter():
'selecteert een willekeurig character die een beschrijving heeft'
while True:
randomNumber = random.randint(0, 1400)
characters = sendMarvelRequest(f'characters?offset={randomNumber}&orderBy=modified')
for character in characters:
if (len(character['description']) > 0) and (len(character['description']) < 200) and (character['thumbnail']['path']!="http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available") and (character['thumbnail']['extension']=='jpg'):
return character, characters
def selectNames(characters, exclude):
'selecteert de namen die gebruikt worden bij multiplechoice'
names = [exclude]
while len(names) < 10:
character = random.choice(characters)
if character['name'] not in names:
names.append(character['name'])
random.shuffle(names)
return names
def questionInfo():
'geeft de informatie die nodig is per vraag'
character, characters = selectCharacter()
name = character['name']
beschrijving = character['description']
names = selectNames(characters, name)
replace_regex = re.compile(re.escape(name), re.IGNORECASE) # zoeken zonder op hoofdletters te letten.
description = replace_regex.sub('<naam>', character['description'])
urlpath = character['thumbnail']['path']
urlextension = character['thumbnail']['extension']
url = f"{urlpath}/portrait_uncanny.{urlextension}"
raw_data = urllib.request.urlopen(url).read()
img = ImageTk.PhotoImage(Image.open(io.BytesIO(raw_data)))
return {'names': names, 'description': description, 'name': name, 'img':img}
def bufferVraag():
'zet de nieuwe vraag in de buffer.'
try:
questionBuffer.append(questionInfo())
except:
time.sleep(3)
bufferVraag()
def getQuestion():
if len(questionBuffer)>0:
return questionBuffer.pop()
else:
showinfo("Error", "Het downloaden van de vragen is niet gelukt.\nKlik op OK om het opnieuw te proberen.")
return getQuestion()