added new date format, changed chromedriver realpath, always create log in script folder, always search for login json in script path. Removed all prints in favor of log

This commit is contained in:
Pakobbix 2023-02-24 07:54:07 +01:00
parent b28f696e5e
commit e82b117c8b

View File

@ -1,3 +1,10 @@
#!/usr/bin/python3
import os
import sys
import json
import time
import logging
import platform import platform
import subprocess import subprocess
from selenium import webdriver from selenium import webdriver
@ -6,13 +13,15 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.action_chains import ActionChains
import json
import time # Get the directory of the currently executing script
import logging script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
# now we will Create and configure logger # now we will Create and configure logger
logging.basicConfig(filename="claim.log", log_file = os.path.join(script_dir, 'claim.log')
logging.basicConfig(filename=log_file,
format='%(asctime)s %(message)s', format='%(asctime)s %(message)s',
datefmt='%d.%m.%Y %H:%M:%S',
filemode='w') filemode='w')
# Creates an object # Creates an object
@ -22,7 +31,9 @@ logger = logging.getLogger()
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
# Load username and password from JSON file # Load username and password from JSON file
with open('honeygainlogin.json', 'r') as file: json_file = os.path.join(script_dir, 'honeygainlogin.json')
with open(json_file, 'r') as file:
login_data = json.load(file) login_data = json.load(file)
username = login_data['username'] username = login_data['username']
@ -40,8 +51,6 @@ chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']) chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.binary_location = chrome_binary chrome_options.binary_location = chrome_binary
chrome_options.add_argument('--headless') chrome_options.add_argument('--headless')
#chrome_options.add_argument('--log-level=3')
#chrome_options.add_argument('--silent')
""" """
Start the ChromeDriver process with stdout Start the ChromeDriver process with stdout
@ -51,7 +60,7 @@ if platform.system() == 'Windows':
devnull = subprocess.DEVNULL devnull = subprocess.DEVNULL
else: else:
devnull = open('/dev/null', 'w') devnull = open('/dev/null', 'w')
process = subprocess.Popen(['chromedriver'], stdout=devnull, stderr=devnull) process = subprocess.Popen(['/usr/local/bin/chromedriver'], stdout=devnull, stderr=devnull)
# Create a new Chrome driver using chromedriver and the Chrome options # Create a new Chrome driver using chromedriver and the Chrome options
driver = webdriver.Chrome(options=chrome_options) driver = webdriver.Chrome(options=chrome_options)
@ -59,8 +68,6 @@ driver = webdriver.Chrome(options=chrome_options)
# Set the window size to 2560x1440 pixels # Set the window size to 2560x1440 pixels
driver.set_window_size(2560, 1440) driver.set_window_size(2560, 1440)
# logger.info("Check for Honeygain Luckypot")
# Navigate to the login page # Navigate to the login page
driver.get('https://dashboard.honeygain.com/login') driver.get('https://dashboard.honeygain.com/login')
@ -70,7 +77,6 @@ try:
(By.XPATH, "//button[contains(., 'Login with email')]")) (By.XPATH, "//button[contains(., 'Login with email')]"))
) )
except TimeoutException: except TimeoutException:
print("Timeout: Failed to load page within 10 seconds")
logger.error("Timeout: Failed to load page within 10 seconds") logger.error("Timeout: Failed to load page within 10 seconds")
driver.quit() driver.quit()
@ -100,7 +106,6 @@ try:
(By.XPATH, "//div[contains(text(), 'Total earnings')]")) (By.XPATH, "//div[contains(text(), 'Total earnings')]"))
) )
except TimeoutException: except TimeoutException:
print("Timeout: Failed to load page within 10 seconds")
logger.error("Timeout: Failed to load page within 10 seconds") logger.error("Timeout: Failed to load page within 10 seconds")
driver.quit() driver.quit()
@ -119,7 +124,6 @@ try:
current_time = driver.find_element( current_time = driver.find_element(
By.CSS_SELECTOR, 'p.sc-jOiSOi.jgbyVL' By.CSS_SELECTOR, 'p.sc-jOiSOi.jgbyVL'
).text ).text
print('Nächster Pot Verfügbar in:', current_time)
logger.info("Nächster Pot Verfügbar in: %s", current_time) logger.info("Nächster Pot Verfügbar in: %s", current_time)
except TimeoutException: except TimeoutException:
# If the timer element is not present, click on the lucky pot button # If the timer element is not present, click on the lucky pot button
@ -137,7 +141,6 @@ except TimeoutException:
lucky_pot_button.click() lucky_pot_button.click()
logger.info('Glückwunsch! Ich habe für dich gerade den Lucky Pot geöffnet') logger.info('Glückwunsch! Ich habe für dich gerade den Lucky Pot geöffnet')
except TimeoutException: except TimeoutException:
print("The 'Open Lucky Pot' button was not found. Exiting...")
logger.error("The 'Open Lucky Pot' button was not found. Exiting...") logger.error("The 'Open Lucky Pot' button was not found. Exiting...")
driver.quit() driver.quit()