Updated play_quiz to use logger

This commit is contained in:
piair 2024-02-27 00:48:55 +01:00
parent a47a460a56
commit 76acfb42c4
1 changed files with 42 additions and 43 deletions

85
V6.py
View File

@ -85,66 +85,61 @@ def close_tab(tab, switch_to: int = 0) -> None:
# play_quiz[N]([int : override]) make the quiz with N choice each time. They usually have between 4 and 10 questions.
# override is the number of question, by default, it's the number of question in this specific quiz. Can be useful in some case, where the program crashes before finishing the quiz
# override is the number of question, by default, it's the number of question in this specific quiz.
# Can be useful in some case, where the program crashes before finishing the quiz
def play_quiz2(override=10) -> None:
printf("starting play_quiz2")
info("Starting to play quiz 2.")
debug(f"override: {override}")
for j in range(override):
try:
# rgpd_popup(driver)
custom_sleep(uniform(3, 5))
js_function = """
function get_correct_answer(){
function br(n) { for (var r, t = 0, i = 0; i < n.length; i++)t += n.charCodeAt(i); return r = parseInt(_G.IG.substr(_G.IG.length - 2), 16), t += r, t.toString() } // Ms check function
function namedRAValue() { //allow calls to getRAvalue
return _w.getRAValue()
};
if (br(document.getElementById("rqAnswerOption0").attributes["data-option"].value) == namedRAValue()){
return(0);
}
else {
return(1);
}
custom_sleep(uniform(3, 5))
js_function = """
function get_correct_answer(){
function br(n) { for (var r, t = 0, i = 0; i < n.length; i++)t += n.charCodeAt(i); return r = parseInt(_G.IG.substr(_G.IG.length - 2), 16), t += r, t.toString() } // Ms check function
function namedRAValue() { //allow calls to getRAvalue
return _w.getRAValue()
};
return(get_correct_answer())
"""
correct_answer_value = driver.execute_script(js_function)
if (br(document.getElementById("rqAnswerOption0").attributes["data-option"].value) == namedRAValue()){
return(0);
}
else {
return(1);
}
};
return(get_correct_answer())
"""
correct_answer_value = driver.execute_script(js_function)
try:
answer_elem = driver.find_element(By.ID, f"rqAnswerOption{correct_answer_value}")
answer_elem.click()
if g.log:
progressBar(j, 10, name="quiz 2")
except exceptions.ElementNotInteractableException as e:
except exceptions.ElementNotInteractableException:
answer_elem = driver.find_element(By.ID, f"rqAnswerOption{correct_answer_value}")
driver.execute_script("arguments[0].click();", answer_elem)
except Exception as e:
log_error(e)
break
printf("play_quiz2 done")
info("Quiz 2 done.")
custom_sleep(3)
def play_quiz8():
info(f"Starting Quiz 8")
override = len(findall("<span id=\"rqQuestionState.\" class=\"emptyCircle\"></span>", driver.page_source)) + 1
printf(f"play_quiz8 : start, override : {override}")
debug(f"override : {override}")
correct_answers = ["Should", "be", "reset", "before", "you", "see", "this."] # supress warning
try:
counter = 0
# rgpd_popup(driver)
for _ in range(override):
sleep(uniform(3, 5))
correct_answers = []
for i in range(1, 9):
for i in range(1, 9): # todo: remove this odd 1-offset
try:
element = driver.find_element(By.ID, f"rqAnswerOption{i - 1}")
if 'iscorrectoption="True"' in element.get_attribute("outerHTML"):
correct_answers.append(f'rqAnswerOption{i - 1}')
except Exception as e:
printf(f"can't find rqAnswerOption{i - 1}. Probably already clicked" + str(e))
warning(f"can't find rqAnswerOption{i - 1}. Probably already clicked" + str(e))
shuffle(correct_answers)
for answer_id in correct_answers:
wait_until_visible(By.ID, answer_id, timeout=20, browser=driver)
counter += 1
if g.log:
progressBar(counter, 16, name="Quiz 8")
try:
answer_elem = driver.find_element(By.ID, answer_id)
answer_elem.click()
@ -160,27 +155,29 @@ def play_quiz8():
except Exception as e:
log_error(f"{format_error(e)} \n Good answers : {' '.join(correct_answers)}")
info("Quiz 8 done.")
custom_sleep(3)
def play_quiz4(override: int = None):
printf("play_quiz4 : start")
info(f"Starting Quiz 4")
if not override:
try: # fidelity quiz are much longer than usual ones
override = int(findall('rqQuestionState([\d]{1,2})"', driver.page_source)[-1])
printf(f"Override : {override}")
except:
override = 3
debug(f"Override : {override}")
try:
for i in range(override):
custom_sleep(uniform(3, 5))
txt = driver.page_source
# rgpd_popup(driver)
answer_option = search('correctAnswer":"([^"]+)', txt)[1]
answer_option = answer_option.replace("\\u0027", "'") # replace Unicode weird symbols
answer_element = driver.find_element(By.CSS_SELECTOR, f'[data-option="{answer_option}"]')
try:
answer_element = driver.find_element(By.CSS_SELECTOR, f'[data-option="{answer_option}"]')
answer_element.click()
except exceptions.ElementNotInteractableException:
driver.execute_script("arguments[0].click();", answer_element)
@ -188,22 +185,24 @@ def play_quiz4(override: int = None):
except Exception as e:
log_error(e)
raise ValueError(e)
info("Quiz 8 done.")
custom_sleep(3)
# do_poll() answer a random thing to poll, on of daily activities
def do_poll():
printf("do_poll : start")
info("Starting poll")
try:
answer_elem = driver.find_element(By.ID, f"btoption{choice([0, 1])}")
try:
answer_elem = driver.find_element(By.ID, f"btoption{choice([0, 1])}")
answer_elem.click()
except exceptions.ElementNotInteractableException:
driver.execute_script("arguments[0].click();", answer_elem)
custom_sleep(uniform(2, 2.5))
except Exception as error:
log_error(error)
raise ValueError(error)
except Exception as err:
log_error(err)
raise ValueError(err)
info("Poll done.")
custom_sleep(3)