From 3978c44bbc6673a5defb0943a2c5126cba264f70 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 10 Apr 2024 11:35:46 +0200 Subject: [PATCH] Use chrome profile to change language --- V6.py | 6 +---- modules/driver_tools.py | 59 ++++++++++++++++++++++------------------- version | 2 +- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/V6.py b/V6.py index 73e7ee7..05fca9f 100755 --- a/V6.py +++ b/V6.py @@ -9,7 +9,6 @@ from modules.db import add_to_database from modules.driver_tools import * from modules.error import * -import os from selenium.common.exceptions import NoSuchElementException # create a webdriver @@ -25,9 +24,7 @@ def create_driver(mobile=False): "Chrome/22 Mobile Safari/537.36" ) - # Profile dir - chrome_profile_dir = "/app/MsRewards-Reborn/user_data/profile/"+config.UserCredentials.get_mail() - os.makedirs(chrome_profile_dir, exist_ok=True) + chrome_profile_dir = init_profile(config.UserCredentials.get_mail()) # Full list on https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md arguments = [ @@ -53,7 +50,6 @@ def create_driver(mobile=False): chrome_options.add_argument(arg) driver = uc.Chrome(options=chrome_options) - set_language(driver) return driver diff --git a/modules/driver_tools.py b/modules/driver_tools.py index 7147515..a4943d6 100644 --- a/modules/driver_tools.py +++ b/modules/driver_tools.py @@ -1,3 +1,5 @@ +import os +import json from random import uniform from selenium.common import TimeoutException @@ -10,33 +12,36 @@ from modules.Tools.logger import debug from modules.Tools.tools import * -def set_language(ldriver): - ldriver.get("chrome://settings/languages") - action = ActionChains(ldriver) - action.reset_actions() - # select language - x_coord = 1200 - y_coord = 150 - action.move_by_offset(x_coord, y_coord).click().perform() - sleep(0.5) - # scroll down - action.reset_actions() - elm = ldriver.find_element(By.XPATH, "/html/body") - ActionChains(ldriver) \ - .send_keys("french") \ - .pause(0.5) \ - .send_keys(Keys.TAB + Keys.TAB + Keys.ENTER + Keys.TAB + Keys.TAB + Keys.ENTER) \ - .perform() - x_coord = 1163 - y_coord = 717 - action.move_by_offset(x_coord, y_coord).click().perform() - # put to the top - sleep(0.5) - action.reset_actions() - x_coord = 1257 - y_coord = 328 - action.move_by_offset(x_coord, y_coord).click().perform() - action.click().perform() +def init_profile(mail): + chrome_profile_dir = "/app/MsRewards-Reborn/user_data/profile/"+mail + os.makedirs(chrome_profile_dir, exist_ok=True) + + preferences_file = os.path.join(chrome_profile_dir, "Default", "Preferences") + if not os.path.exists(preferences_file): + os.makedirs(os.path.join(chrome_profile_dir, "Default"), exist_ok=True) + with open(preferences_file, "w") as f: + json.dump( + { + "intl": { + "accept_languages": "fr-FR,en-US,en", + "selected_languages": "fr-FR,en-US,en" + } + }, f + ) + else: + with open(preferences_file, "r") as f: + settings = json.load(f) + + if "intl" not in settings: + settings["intl"] = {} + + settings["intl"]["accept_languages"] = "fr-FR,en-US,en" + settings["intl"]["selected_languages"] = "fr-FR,en-US,en" + + with open(preferences_file, "w") as f: + json.dump(settings, f) + + return chrome_profile_dir # Deal with RGPD popup as well as some random popup like 'are you satisfied' one diff --git a/version b/version index a92bb77..4a4d266 100644 --- a/version +++ b/version @@ -1 +1 @@ -v6.8.43 +v6.8.44