From 76acfb42c4fe7115a03c5fbbaa07f4ff585d56d3 Mon Sep 17 00:00:00 2001 From: piair Date: Tue, 27 Feb 2024 00:48:55 +0100 Subject: [PATCH] Updated play_quiz to use logger --- V6.py | 85 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/V6.py b/V6.py index ba386e4..4960552 100755 --- a/V6.py +++ b/V6.py @@ -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): + 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: - # 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.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("", 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)