Skip to content

Instantly share code, notes, and snippets.

@mjcarnaje
Created June 3, 2024 03:41
Show Gist options
  • Save mjcarnaje/93ded7bd1fa1a3767bce2205888fdece to your computer and use it in GitHub Desktop.
Save mjcarnaje/93ded7bd1fa1a3767bce2205888fdece to your computer and use it in GitHub Desktop.
connecteam time in
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import json
import schedule
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Add file handler to save logs to a file
file_handler = logging.FileHandler('/Users/mjcarnaje/Documents/codes/automations/connecteam-timein/logfile.txt')
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logging.getLogger().addHandler(file_handler)
def setup_driver():
logging.info("Setting up Chrome driver options.")
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("detach", True)
options.add_argument("user-data-dir=/Users/mjcarnaje/Library/Application Support/Google/Chrome")
options.add_argument("profile-directory=Profile 1")
driver = webdriver.Chrome(options=options)
logging.info("Driver setup complete!")
return driver
def load_cookies(driver, cookies_file):
logging.info(f"Loading cookies from {cookies_file}.")
try:
with open(cookies_file, 'r', newline='') as file:
cookies = json.load(file)
valid_sameSite_values = ["Strict", "Lax", "None"]
for cookie in cookies:
if 'sameSite' in cookie and cookie['sameSite'] not in valid_sameSite_values:
logging.warning(f"Invalid sameSite value {cookie['sameSite']} for cookie {cookie['name']}. Setting to 'None'.")
cookie['sameSite'] = 'None'
driver.add_cookie(cookie)
logging.info("Cookies loaded successfully.")
except Exception as e:
logging.error(f"Failed to load cookies: {e}")
def main():
logging.info("Starting script...")
driver = setup_driver()
base_url = "https://app.connecteam.com"
url = "https://app.connecteam.com/index.html#/punchclock/punchclock/5296685/5579581/6232887"
# Open the website and wait for it to load
logging.info(f"Navigating to {base_url}.")
driver.get(base_url)
sleep(5)
# Load cookies and refresh the page
load_cookies(driver, '/Users/mjcarnaje/Documents/codes/automations/connecteam-timein/cookies.json')
sleep(5)
logging.info("Refreshing the page after loading cookies.")
driver.refresh()
sleep(5)
# Open the specific URL
logging.info(f"Navigating to {url}.")
driver.get(url)
logging.info("Granting geolocation permissions.")
driver.execute_cdp_cmd("Browser.grantPermissions", {
"origin": url,
"permissions": ["geolocation"]
})
logging.info("Setting geolocation override.")
driver.execute_cdp_cmd(
"Emulation.setGeolocationOverride",
{
"latitude": 8.239943,
"longitude": 124.253459,
"accuracy": 100
}
)
sleep(5)
# Try to find and click the punch button
try:
logging.info("Trying to find the punch button.")
punch_button = driver.find_element(By.CLASS_NAME, "punch-clock-big-button")
if punch_button:
logging.info("Punch button found. Attempting to click.")
punch_button.click()
logging.info("Punch button clicked successfully!")
else:
logging.warning("Punch button not found.")
except Exception as e:
logging.error(f"Error finding or clicking punch button: {e}")
sleep(10)
logging.info("Script execution completed.")
schedule.every().day.at("05:00").do(main)
while True:
schedule.run_pending()
sleep(60)
logging.info("Waiting for scheduled task...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment