Skip to content

Instantly share code, notes, and snippets.

@rohitkg98
Created January 28, 2020 05:52
Show Gist options
  • Save rohitkg98/a21fba7c763212e34db658a7777e3bf4 to your computer and use it in GitHub Desktop.
Save rohitkg98/a21fba7c763212e34db658a7777e3bf4 to your computer and use it in GitHub Desktop.
"""
Copyright 2020 Kaushal Rohit
Steps:
Install selenium and geckodriver.
Run the script as sudo with --setup argument:
$ sudo python --setup
Now, you have a service which runs at Boot and Shutdown.
Enable the Service.
$ sudo systemctl enable kredily.py
"""
import os
import json
import pathlib
import argparse
def notify(msg): os.system("notify-send '{}'".format(msg))
notify("Running Kredily")
cron = """
[Unit]
Description=Clock In and Out automatically at boot and shutdown
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart={0} {1}/kredily.py
ExecStop={0} {1}/kredily.py
[Install]
WantedBy=multi-user.target
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-s", "--setup", help="Setup new username and password", action="store_true"
)
args = parser.parse_args()
config_path = os.path.expanduser("~/kred_config.json")
data = dict()
if args.setup:
# Retrieve path by using "which python"
print(
"Enter path to the target Python\n"
"NOTE: your can retrive target python by running `which python`"
)
python_path = input().strip()
print("Please enter Kredily username")
data["username"] = input()
print("Please enter Kredily password")
data["password"] = input()
with open(config_path, "w") as f:
data = f.write(json.dumps(data, indent=4))
os.chmod(config_path, 0o666)
print("Wrote Username and Password to ~/kred_config.json")
with open("/etc/systemd/system/kredily.service", "w") as f:
script_path = pathlib.Path(__file__).parent.absolute()
f.write(cron.format(python_path, script_path))
print("Created service: /etc/systemd/system/kredily.service")
exit(0)
data = dict()
if not os.path.exists(config_path):
notify("Please run `sudo python kredily.py --setup` once")
exit(0)
with open(config_path, "r") as f:
data = json.loads(f.read())
# Clock In and Clock Out Logic
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
ff_options = Options()
ff_options.headless = True
driver = webdriver.Firefox(options=ff_options)
driver.get("https://infobizzs.kredily.com")
elem = driver.find_element_by_id("signInFormEmailAddress")
elem.send_keys(data["username"])
elem = driver.find_element_by_id("signInFormPassword")
elem.send_keys(data["password"])
driver.find_element_by_id("signinSubmitBtn").click()
btn = driver.find_element_by_id("clockInBtn")
btn.click()
if btn.find_element_by_class_name("clockedInLabel").is_displayed():
notify('You have been CLOCKED IN')
else:
notify('You have been CLOCKED OUT')
notify('Finished Kredily')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment