-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathakinator.py
142 lines (115 loc) · 4.39 KB
/
akinator.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import requests
# Somebody told me that the link has changed, but I'm not
# really maintaining this anymore, so here is something that
# I think should fix it.
NEW_SESSION_URL = "https://srv2.akinator.com:9157/ws/new_session?constraint=ETAT<>'AV'&partner=1"
ANSWER_URL = "https://srv2.akinator.com:9157/ws/answer?constraint=ETAT<>'AV'"
GET_GUESS_URL = "https://srv2.akinator.com:9157/ws/list?constraint=ETAT<>'AV'"
CHOICE_URL = "https://srv2.akinator.com:9157/ws/choice?constraint=ETAT<>'AV'"
EXCLUSION_URL = "https://srv2.akinator.com:9157/ws/exclusion?constraint=ETAT<>'AV'"
GLB_URL = "https://pastebin.com/gTua3dg2"
def ans_to_strint(ans: str):
ans = ans.lower()
if ans == "yes" or ans == "y":
return "0"
elif ans == "no" or ans == "n":
return "1"
elif ans == "i" or ans == "idk" or ans == "i dont know" or ans == "i don't know":
return "2"
elif ans == "probably" or ans == "p":
return "3"
elif ans == "probably not" or ans == "pn":
return "4"
else:
return "-1"
game_over = False
akinator_session = requests.get(NEW_SESSION_URL)
akinator_data = akinator_session.json()
try:
if akinator_data['completion'] == "OK":
success = True
else:
success = False
except:
success = False
if not success:
raise Exception('Error')
print("Question " +
str(int(akinator_data['parameters']['step_information']['step']) + 1) +
":\n" +
akinator_data['parameters']['step_information']['question'] +
'\n"yes", "no", "idk", "probably", "probably not"')
response = input("> ")
response = ans_to_strint(response)
params = {
"session": akinator_data['parameters']['identification']['session'],
"signature": akinator_data['parameters']['identification']['signature'],
"step": akinator_data['parameters']['step_information']['step'],
"answer": response
}
session = akinator_data['parameters']['identification']['session']
signature = akinator_data['parameters']['identification']['signature']
akinator_session = requests.get(ANSWER_URL, params=params)
akinator_data = akinator_session.json()
can_guess = False
guessed_wrong_once = False
while not game_over:
while not can_guess:
if int(float(akinator_data['parameters']['progression'])) > 90 and not guessed_wrong_once:
can_guess = True
break
guessed_wrong_once = False
print(akinator_data['parameters']['progression'])
print("Question " +
str(int(akinator_data['parameters']['step']) + 1) +
":\n" +
akinator_data['parameters']['question'] +
'\n"yes", "no", "idk", "probably", "probably not"')
response = input("> ")
response = ans_to_strint(response)
params = {
"session": session,
"signature": signature,
"step": akinator_data['parameters']['step'],
"answer": response
}
akinator_session = requests.get(ANSWER_URL, params=params)
akinator_data = akinator_session.json()
params = {
"session": session,
"signature": signature,
"step": akinator_data['parameters']['step']
}
guess_session = requests.get(GET_GUESS_URL, params=params)
guess_data = guess_session.json()
name = guess_data['parameters']['elements'][0]['element']['name']
desc = guess_data['parameters']['elements'][0]['element']['description']
print("Is this your character? [yes/no]\n" +
name + "\n" +
desc + "\n")
answer = input('> ')
if answer.lower() == "yes" or answer.lower() == "y":
# TODO dump to choice_url
params = {
"session": session,
"signature": signature,
"step": akinator_data['parameters']['step'],
"element": guess_data['parameters']['elements'][0]['element']['id']
}
r = requests.get(CHOICE_URL, params=params)
print("I guessed right! Thanks for playing with me.")
game_over = True
break
elif answer.lower() == "no" or answer.lower() == "n":
# TODO dump to exclusion_url
params = {
"session": session,
"signature": signature,
"step": akinator_data['parameters']['step'],
"forward_answer": response
}
r = requests.get(EXCLUSION_URL, params=params)
can_guess = False
guessed_wrong_once = True
else:
pass