-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (168 loc) · 7.04 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import time, requests
from selenium import webdriver
from bs4 import BeautifulSoup
class Bot:
def __init__(self, site="", browser=True, name="b0t"):
self.site = site
self.name = name
self.storage = { }
self.limit = 0
self.count = 0
self.offset = 3
self.soup = None
# init browser
if browser:
self.driver = webdriver.Firefox()
self.driver.get(site)
#
# === GAME FUNCTIONALITIES ===
#
# Play the quiz.
def play(self):
# Make sure set of correct answers is present.
if self.storage == { }:
self.parse_answers()
self.count = 0
self.limit = len(self.storage) - self.offset
for _ in range(len(self.storage)):
try:
time.sleep(5 / 10)
# See if scoreboard appears. If it does, end the loop.
self.soup = BeautifulSoup(self.driver.page_source, "html.parser")
if self.soup.find(string="Tallenna tulos") != None:
break
# If the question limit is met, end the game.
selection_status = False
if self.count >= self.limit:
print("\t* Limit reached *")
self.end_game()
break
# If "choose" returns False, somethings wrong.
if selection_status == self.choose(True):
break
except Exception as e:
print(e)
print("\n -- Game Done. --")
return True
# Insert the player name into the scoreboard.
def insert_name(self):
time.sleep(1)
self.soup = BeautifulSoup(self.driver.page_source, 'html.parser')
time.sleep(5 / 10)
self.driver.find_element(by="xpath", value="//input[@title='nimi']").send_keys(self.name)
time.sleep(5 / 10)
self.driver.find_element(by="xpath", value="//button").click()
time.sleep(10)
#self.driver.close()
# Choose the desired answer. Correct = False for the wrong answer, and True for the correct.
def choose(self, correct=True):
try:
self.soup = BeautifulSoup(self.driver.page_source, "html.parser")
image = self.soup.find_all("img", class_="Question_question-img__22zm6")[0].get("src")
if not image in self.storage:
print(" ! Could not find image from storage. ")
return False
# Go through all of the options and pick the desired one:
for option in self.soup.find_all("div", class_="Alternative_alternative__2orVq"):
if (option.text == self.storage[image]) == correct:
print(" + Found the correct answer.. " * correct, " - Found the wrong answer.. " * (correct == False) ,end=" ")
print(self.count)
self.click_div(option.text)
return True
# If no answer match; somethings wrong.
print("! DIDN'T FIND DESIRED ANSWER.")
for option in self.soup.find_all("div", class_="Alternative_alternative__2orVq"):
print(f"{option.text} | {self.storage[image]} ")
return False
# If you go over the limit, the website breaks, as no new questions appear.
except IndexError:
print(" !! Website broken. ")
return False
# As the game needs 3 wrong answers to "end" it, choose 3 wrong intentionally to exit.
def end_game(self):
for _ in range(0, 3):
time.sleep(5 / 10)
self.choose(False)
# Return "king of the hill" status as false / true.
# If someone else is on the board, we're not the king.
def check_leaderboard(self):
self.end_game()
time.sleep(5 / 10)
king = True
self.soup = BeautifulSoup(self.driver.page_source, 'html.parser')
print(" -- Checking leaderboard --")
for row in self.soup.find_all("tr"):
player = str(row.find_all("td")[2]).replace("<td>", "").replace("</td>", "")
# If we occupy the leaderboard, continue.
if player == "Nimi" or player == self.name:
continue
print(" !! Someone else occupies the board: ", player)
king = False
print(" -- Done! --")
return king
#
# === SUPPORTING FUNCTIONS ===
#
# Find the div with the correct option, and then click it.
def click_div(self, correct_option):
try:
self.driver.find_element(by="xpath", value=f"//div[contains(text(), '{correct_option}')]").click()
self.count += 1
except Exception as e:
print(e)
return False
return True
# get all the correct answers from the static JS file.
def parse_answers(self):
static_js = "main.45acba1b.chunk.js"
# get the static JS used to play the game.
request = requests.get(self.site + "/static/js/" + static_js)
# Find all the questions.
for question in request.text.split("questions:[{")[1].split("}]"):
try:
first_id = False
# Find all the correct answers.
for line in question.split(","):
if "results:[{:" in line:
break
if "id" in line and not first_id:
first_id = True
continue
if "img" in line:
link = line.split("img:\"")[1].split("\"")[0]
self.storage[link] = ""
continue
if "text:" in line:
text = line.split("text:\"")[1].split("\"")[0]
continue
if "isCorrect:" in line and "!0" in line:
# Correct UTF-8 errors.
self.storage[link] = \
text.replace("\\xe4", "ä").replace("\\xf6", "ö")\
.replace("\\xc3\\xa4", "ä")
link = ""
break
except IndexError:
break
def main():
bot = Bot("https://haalar.it")
bot.parse_answers()
while 1:
while 1:
# Check leaderboard every 20s.
king_status = bot.check_leaderboard()
if not king_status:
# If we don't dominate the board, play the game again.
break
time.sleep(20)
# Leaderboard consists of 9 places, fill them:
for _ in range(0, 9):
bot.driver.get(bot.site)
bot.play()
bot.insert_name()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print(" ** EXITING GAME **")
exit()