Skip to content

Instantly share code, notes, and snippets.

@lnstchtped
Created February 19, 2024 04:35
Show Gist options
  • Select an option

  • Save lnstchtped/6a3126f5c31ecee9b715a3234b1f8c02 to your computer and use it in GitHub Desktop.

Select an option

Save lnstchtped/6a3126f5c31ecee9b715a3234b1f8c02 to your computer and use it in GitHub Desktop.
Brainybrawls Bot
import json
import time
import tls_client
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y != 0:
return x / y
else:
return 0
operators = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide
}
class BrainyBrawls:
def __init__(self, username: str):
self.username = username
self.session = tls_client.Session(
client_identifier="chrome_120",
random_tls_extension_order=True,
)
self.session.headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.6",
"Rsc": "1",
"Referer": "https://brainybrawls.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
}
self.start_time = 0
def register(self) -> str:
return self.session.get(f"https://brainybrawls.com/game?name={self.username}&_rsc=9ehs5").text
def parse_response(self, response: str) -> list:
lines = response.split("\n")
for line in lines:
if len(line) < 10:
continue
if line[2:].startswith("["):
line = line[2:]
try:
json.loads(line)
except:
continue
for item in json.loads(line):
if type(item) is dict:
if item.get("questions", None):
self.session.headers.update(
{
"Authorization": f"Bearer {item['sessionToken']}"
}
)
return item["questions"]
return None
def answer_question(self, question_id: str, answer: int) -> bool:
r = self.session.post(
"https://brainybrawls.com/game/answer",
data=json.dumps({
"questionId": question_id,
"answer": str(answer)
})
)
if r.status_code == 429:
time.sleep(2)
return self.answer_question(question_id, answer)
return r.text == "Success"
def end_game(self) -> int:
return int(self.session.post("https://brainybrawls.com/game/finish").json().get("score", 0))
def runner(self):
questions = self.parse_response(self.register())
self.start_time = time.time()
if questions:
self.session.headers.update(
{"Content-Type": "text/plain;charset=UTF-8"},
)
for question in questions:
answer = int(operators[question["operator"]](
question["firstOperand"], question["secondOperand"]
))
if self.answer_question(question["id"], answer):
print(
f"Answered question {question['id']} ({question['firstOperand']} {question['operator']} {question['secondOperand']} = {answer})")
else:
print(
f"Failed to answer question {question['id']} ({question['firstOperand']} {question['operator']} {question['secondOperand']} = {answer})")
else:
print("No questions found")
return
delta = time.time() - self.start_time
print(
f"Finished answering questions in {delta} seconds, sleeping for {60 - delta} seconds for game to end")
time.sleep(delta)
print(f"Game ended with score: {self.end_game()}")
if __name__ == "__main__":
username = "LnsTchTps"
bot = BrainyBrawls(username)
bot.runner()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment