From 5fe05712bd0d1445abf467675b88a9e9e4e718ad Mon Sep 17 00:00:00 2001 From: piair Date: Wed, 28 Feb 2024 00:14:42 +0100 Subject: [PATCH] refactored all_cards --- V6.py | 119 ++++++++++++++++++++++++++------------------------------ version | 2 +- 2 files changed, 56 insertions(+), 65 deletions(-) diff --git a/V6.py b/V6.py index 73027f6..8da7f99 100755 --- a/V6.py +++ b/V6.py @@ -12,21 +12,21 @@ from modules.error import * # create a webdriver def create_driver(mobile=False): - PC_USER_AGENT = ( + pc_user_agent = ( "Mozilla/5.0 (X11; Linux x86_64)" "AppleWebKit/537.36 (KHTML, like Gecko)" "Chrome/122.0.0.0 Safari/537.36 Edg/122.0.2088.46" ) - MOBILE_USER_AGENT = ( + mobile_user_agent = ( "Mozilla/5.0 (Linux; Android 7.0; Nexus 5 Build/MRA58N)" "AppleWebKit/537.36 (KHTML, like Gecko)" "Chrome/22 Mobile Safari/537.36" ) chrome_options = webdriver.ChromeOptions() if mobile: - chrome_options.add_argument(f"--user-agent={MOBILE_USER_AGENT}") + chrome_options.add_argument(f"--user-agent={mobile_user_agent}") else: - chrome_options.add_argument(f"--user-agent={PC_USER_AGENT}") + chrome_options.add_argument(f"--user-agent={pc_user_agent}") # disabled as it may cause detection if config.proxy.is_enabled(): chrome_options.add_argument(f'--proxy-server={config.proxy.ip}:{config.proxy.port}') @@ -91,13 +91,13 @@ def play_quiz8(): for _ in range(override): sleep(uniform(3, 5)) correct_answers = [] - for i in range(1, 9): # todo: remove this odd 1-offset + for i in range(8): try: - element = driver.find_element(By.ID, f"rqAnswerOption{i - 1}") + element = driver.find_element(By.ID, f"rqAnswerOption{i}") if 'iscorrectoption="True"' in element.get_attribute("outerHTML"): - correct_answers.append(f'rqAnswerOption{i - 1}') + correct_answers.append(f'rqAnswerOption{i}') except Exception as e: - warning(f"can't find rqAnswerOption{i - 1}. Probably already clicked" + str(e)) + warning(f"can't find rqAnswerOption{i}. 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) @@ -171,92 +171,83 @@ def do_poll(): # Find each playable card and tries to click on it to earn points -# todo : refactor def all_cards(): driver = config.WebDriver.driver driver.get("https://rewards.bing.com") wait_until_visible(By.CLASS_NAME, "c-card-content", 10, driver) - liste = driver.find_elements(By.CLASS_NAME, "c-card-content") + + card_list = driver.find_elements(By.CLASS_NAME, "c-card-content") custom_sleep(2) + try: promo() except Exception as e: + debug(e) info("no promo card") - if (len(liste) < 10): # most likely an error during loading + + if len(card_list) < 10: # most likely an error during loading if "suspendu" in driver.page_source: raise Banned() driver.refresh() - liste = driver.find_elements(By.CLASS_NAME, "c-card-content") - if (len(liste) < 10): + card_list = driver.find_elements(By.CLASS_NAME, "c-card-content") + if len(card_list) < 10: log_error("Less than 10 cards. Most likely an error with login.") - return ("PAS ASSEZ DE CARTES") - if (len(liste) < 20): # most likely not in france - error("moins de 20 cartes. Probablement pas en France.") - for i in range(len(liste)): + return "Not enough cards" + + for i in range(len(card_list)): debug(f"carte {i}") + checked = False try: - checked = ("mee-icon-AddMedium" in liste[i].get_attribute("innerHTML")) + checked = "mee-icon-AddMedium" in card_list[i].get_attribute("innerHTML") + except StaleElementReferenceException: driver.refresh() - liste = driver.find_elements(By.CLASS_NAME, "c-card-content") - warning(f"staled, {len(liste)}") - checked = ("mee-icon-AddMedium" in liste[i].get_attribute("innerHTML")) + card_list = driver.find_elements(By.CLASS_NAME, "c-card-content") + warning(f"staled, {len(card_list)}") + checked = "mee-icon-AddMedium" in card_list[i].get_attribute("innerHTML") + except IndexError: driver.get("https://rewards.bing.com") custom_sleep(10) - liste = driver.find_elements(By.CLASS_NAME, "c-card-content") + card_list = driver.find_elements(By.CLASS_NAME, "c-card-content") try: - checked = ("mee-icon-AddMedium" in liste[i].get_attribute("innerHTML")) + checked = "mee-icon-AddMedium" in card_list[i].get_attribute("innerHTML") except IndexError: - if i == len(liste) & i > 15: + if i == len(card_list) and i > 15: checked = False + if checked: custom_sleep(1.5) - driver.execute_script("arguments[0].scrollIntoView();", liste[i]) + driver.execute_script("arguments[0].scrollIntoView();", card_list[i]) custom_sleep(1.5) - liste[i].click() + card_list[i].click() + if len(driver.window_handles) > 1: driver.switch_to.window(driver.window_handles[1]) try_play(driver.title) close_tab(driver.window_handles[1]) - try: - driver.refresh() - liste = driver.find_elements(By.CLASS_NAME, "c-card-content") - if ("mee-icon-AddMedium" in liste[i].get_attribute("innerHTML")): - warning(f"carte {i} not okay. Retrying.") + + try: + driver.refresh() + card_list = driver.find_elements(By.CLASS_NAME, "c-card-content") + + if "mee-icon-AddMedium" in card_list[i].get_attribute("innerHTML"): + driver.execute_script("arguments[0].scrollIntoView();", card_list[i]) + card_list[i].click() + driver.switch_to.window(driver.window_handles[1]) + custom_sleep(10) + log_error(f"Card {i} Can't be completed. Why MS ?") + try: + try_play(driver.title) # go back to the main page try: - liste[i].click() - except: - log_error("problème inconnu ? sauf si c'est un element obscure...") - driver.get("https://rewards.bing.com") - checked = ("mee-icon-AddMedium" in liste[i].get_attribute("innerHTML")) - driver.switch_to.window(driver.window_handles[1]) - try_play(driver.title) - close_tab(driver.window_handles[1]) - if ("mee-icon-AddMedium" in liste[i].get_attribute("innerHTML")): - driver.execute_script("arguments[0].scrollIntoView();", liste[i]) - log_error(f"Card {i} Can't be completed. Why MS ?") - liste[i].click() - driver.switch_to.window(driver.window_handles[1]) - log_error(f"Cart completion - log - 2") - custom_sleep(10) - log_error(f"Cart completion - log - 3 - after 10 sec") - try: - try_play(driver.title) # go back to the main page - try: - close_tab(driver.window_handles[1]) - except: - pass - except: - driver.get("https://rewards.bing.com") - except: - pass - else: - try: - welcome_tour(liste[i], driver) - except Exception as e: - error("no new windows" + format_error(e)) - driver.get("https://rewards.bing.com") + close_tab(driver.window_handles[1]) + except Exception as e: + debug(e) + except Exception as e: + debug(e) + driver.get("https://rewards.bing.com") + except Exception as err: + log_error(err) custom_sleep(3) @@ -762,7 +753,7 @@ def default_start(): def log_error(msg): - DiscordLogger(config) + DiscordLogger(config).send(msg) def check_updated(): diff --git a/version b/version index 85bccfe..e1bef45 100644 --- a/version +++ b/version @@ -1 +1 @@ -v6.8.21 +v6.8.22