Updated play_quiz to use logger

This commit is contained in:
piair 2024-02-27 00:48:55 +01:00
parent a47a460a56
commit 76acfb42c4

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. # 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: 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): for j in range(override):
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);
}
};
return(get_correct_answer())
"""
correct_answer_value = driver.execute_script(js_function)
try: 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);
}
};
return(get_correct_answer())
"""
correct_answer_value = driver.execute_script(js_function)
answer_elem = driver.find_element(By.ID, f"rqAnswerOption{correct_answer_value}") answer_elem = driver.find_element(By.ID, f"rqAnswerOption{correct_answer_value}")
answer_elem.click() answer_elem.click()
if g.log: except exceptions.ElementNotInteractableException:
progressBar(j, 10, name="quiz 2") answer_elem = driver.find_element(By.ID, f"rqAnswerOption{correct_answer_value}")
except exceptions.ElementNotInteractableException as e:
driver.execute_script("arguments[0].click();", answer_elem) driver.execute_script("arguments[0].click();", answer_elem)
except Exception as e: except Exception as e:
log_error(e) log_error(e)
break break
printf("play_quiz2 done") info("Quiz 2 done.")
custom_sleep(3) custom_sleep(3)
def play_quiz8(): def play_quiz8():
info(f"Starting Quiz 8")
override = len(findall("<span id=\"rqQuestionState.\" class=\"emptyCircle\"></span>", driver.page_source)) + 1 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: try:
counter = 0
# rgpd_popup(driver)
for _ in range(override): for _ in range(override):
sleep(uniform(3, 5)) sleep(uniform(3, 5))
correct_answers = [] correct_answers = []
for i in range(1, 9): for i in range(1, 9): # todo: remove this odd 1-offset
try: try:
element = driver.find_element(By.ID, f"rqAnswerOption{i - 1}") element = driver.find_element(By.ID, f"rqAnswerOption{i - 1}")
if 'iscorrectoption="True"' in element.get_attribute("outerHTML"): if 'iscorrectoption="True"' in element.get_attribute("outerHTML"):
correct_answers.append(f'rqAnswerOption{i - 1}') correct_answers.append(f'rqAnswerOption{i - 1}')
except Exception as e: 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) shuffle(correct_answers)
for answer_id in correct_answers: for answer_id in correct_answers:
wait_until_visible(By.ID, answer_id, timeout=20, browser=driver) wait_until_visible(By.ID, answer_id, timeout=20, browser=driver)
counter += 1
if g.log:
progressBar(counter, 16, name="Quiz 8")
try: try:
answer_elem = driver.find_element(By.ID, answer_id) answer_elem = driver.find_element(By.ID, answer_id)
answer_elem.click() answer_elem.click()
@ -160,27 +155,29 @@ def play_quiz8():
except Exception as e: except Exception as e:
log_error(f"{format_error(e)} \n Good answers : {' '.join(correct_answers)}") log_error(f"{format_error(e)} \n Good answers : {' '.join(correct_answers)}")
info("Quiz 8 done.")
custom_sleep(3) custom_sleep(3)
def play_quiz4(override: int = None): def play_quiz4(override: int = None):
printf("play_quiz4 : start") info(f"Starting Quiz 4")
if not override: if not override:
try: # fidelity quiz are much longer than usual ones try: # fidelity quiz are much longer than usual ones
override = int(findall('rqQuestionState([\d]{1,2})"', driver.page_source)[-1]) override = int(findall('rqQuestionState([\d]{1,2})"', driver.page_source)[-1])
printf(f"Override : {override}")
except: except:
override = 3 override = 3
debug(f"Override : {override}")
try: try:
for i in range(override): for i in range(override):
custom_sleep(uniform(3, 5)) custom_sleep(uniform(3, 5))
txt = driver.page_source txt = driver.page_source
# rgpd_popup(driver)
answer_option = search('correctAnswer":"([^"]+)', txt)[1] answer_option = search('correctAnswer":"([^"]+)', txt)[1]
answer_option = answer_option.replace("\\u0027", "'") # replace Unicode weird symbols answer_option = answer_option.replace("\\u0027", "'") # replace Unicode weird symbols
answer_element = driver.find_element(By.CSS_SELECTOR, f'[data-option="{answer_option}"]')
try: try:
answer_element = driver.find_element(By.CSS_SELECTOR, f'[data-option="{answer_option}"]')
answer_element.click() answer_element.click()
except exceptions.ElementNotInteractableException: except exceptions.ElementNotInteractableException:
driver.execute_script("arguments[0].click();", answer_element) driver.execute_script("arguments[0].click();", answer_element)
@ -188,22 +185,24 @@ def play_quiz4(override: int = None):
except Exception as e: except Exception as e:
log_error(e) log_error(e)
raise ValueError(e) raise ValueError(e)
info("Quiz 8 done.")
custom_sleep(3) custom_sleep(3)
# do_poll() answer a random thing to poll, on of daily activities # do_poll() answer a random thing to poll, on of daily activities
def do_poll(): def do_poll():
printf("do_poll : start") info("Starting poll")
try: try:
answer_elem = driver.find_element(By.ID, f"btoption{choice([0, 1])}")
try: try:
answer_elem = driver.find_element(By.ID, f"btoption{choice([0, 1])}")
answer_elem.click() answer_elem.click()
except exceptions.ElementNotInteractableException: except exceptions.ElementNotInteractableException:
driver.execute_script("arguments[0].click();", answer_elem) driver.execute_script("arguments[0].click();", answer_elem)
custom_sleep(uniform(2, 2.5)) custom_sleep(uniform(2, 2.5))
except Exception as error: except Exception as err:
log_error(error) log_error(err)
raise ValueError(error) raise ValueError(err)
info("Poll done.")
custom_sleep(3) custom_sleep(3)