-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (126 loc) · 5.44 KB
/
Copy pathmain.py
File metadata and controls
133 lines (126 loc) · 5.44 KB
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
import cloudscraper
from bs4 import BeautifulSoup
class AkinatorError(Exception):
pass
class Akinator():
def __init__(self, theme: str = "characters", lang: str = "jp", child_mode: bool = False) -> None:
self.ENDPOINT = f"https://{lang}.akinator.com/"
self.name = None
self.description = None
self.photo = None
self.answer_id = None
self.akitude = None
self.scraper = cloudscraper.create_scraper() # 创建cloudscraper
if theme == "characters":
sid = 1
elif theme == "objects":
sid = 2
elif theme == "animals":
sid = 14
else:
raise AkinatorError("the theme must be 'characters' / 'objects' / 'animals'")
self.json = {
"step": 0,
"progression": 0.0,
"sid": sid,
"cm": child_mode,
"answer": 0,
}
def start_game(self):
self.name = None
self.description = None
self.photo = None
self.answer_id = None
self.akitude = "https://en.akinator.com/assets/img/akitudes_670x1096/defi.png"
# 使用 cloudscraper 发送 POST 请求,以绕过Cloudflare拦截
game = self.scraper.post(f"{self.ENDPOINT}game", json={"sid": self.json["sid"], "cm": self.json["cm"]}).text
soup = BeautifulSoup(game, "html.parser")
askSoundlike = soup.find(id="askSoundlike")
question_label = soup.find(id="question-label").get_text()
session_id = askSoundlike.find(id="session").get("value")
signature_id = askSoundlike.find(id="signature").get("value")
self.json["session"] = session_id
self.json["signature"] = signature_id
self.step = 0
self.progression = 0.0
self.question = question_label
return question_label
def post_answer(self, answer: str):
if answer == "y":
self.json["answer"] = 0
elif answer == "n":
self.json["answer"] = 1
elif answer == "idk":
self.json["answer"] = 2
elif answer == "p":
self.json["answer"] = 3
elif answer == "pn":
self.json["answer"] = 4
else:
raise AkinatorError("the answer must be 'y' / 'n' / 'idk' / 'p' / 'pn'")
try:
progression = self.scraper.post(f"{self.ENDPOINT}answer", json=self.json)
progression = progression.json()
if progression["completion"] == "KO":
raise AkinatorError("completion : KO")
elif progression["completion"] == "SOUNDLIKE":
raise AkinatorError("completion : SOUNDLIKE")
try:
self.json["step"] = int(progression["step"])
self.json["progression"] = float(progression["progression"])
self.step = int(progression["step"])
self.progression = float(progression["progression"])
self.question = progression["question"]
self.question_id = progression["question_id"]
self.akitude = f"https://en.akinator.com/assets/img/akitudes_670x1096/{progression['akitude']}"
except:
self.name = progression["name_proposition"]
self.description = progression["description_proposition"]
self.photo = progression["photo"]
self.answer_id = progression["id_proposition"]
self.json["step_last_proposition"] = int(self.json["step"])
return progression
except Exception as e:
raise AkinatorError(progression)
def go_back(self):
self.name = None
self.description = None
self.photo = None
self.answer_id = None
if self.json["step"] == 0:
raise AkinatorError("it's first question")
if "answer" in self.json:
del self.json["answer"]
try:
goback = self.scraper.post(f"{self.ENDPOINT}cancel_answer", json=self.json)
goback = goback.json()
self.json["step"] = int(goback["step"])
self.json["progression"] = float(goback["progression"])
self.step = int(goback["step"])
self.progression = float(goback["progression"])
self.question = goback["question"]
self.question_id = goback["question_id"]
self.akitude = f"https://en.akinator.com/assets/img/akitudes_670x1096/{goback['akitude']}"
return goback
except:
raise AkinatorError(goback)
def exclude(self):
self.name = None
self.description = None
self.photo = None
self.answer_id = None
if "answer" in self.json:
del self.json["answer"]
try:
exclude = self.scraper.post(f"{self.ENDPOINT}exclude", json=self.json)
exclude = exclude.json()
self.json["step"] = int(exclude["step"])
self.json["progression"] = float(exclude["progression"])
self.step = int(exclude["step"])
self.progression = float(exclude["progression"])
self.question = exclude["question"]
self.question_id = exclude["question_id"]
self.akitude = f"https://en.akinator.com/assets/img/akitudes_670x1096/{exclude['akitude']}"
return exclude
except:
raise AkinatorError(exclude)