pyclock/pyclock.py
2018-10-26 18:21:10 -05:00

194 lines
5.5 KiB
Python

import argparse
import json
import getpass
import time
import datetime as dt
import os
from enum import IntEnum
from mail import send_email
from selen_help import *
class Clock(IntEnum):
In = 1
Out = 2
Meal = 3
def __str__(self):
return self.name
@staticmethod
def from_string(s):
try:
return Clock[s]
except KeyError:
raise ValueError()
def login(driver, username, password):
wait_for_internet_connection()
driver.get("https://accessuh.uh.edu")
find_element(driver, By.ID, "param1").send_keys(username)
find_element(driver, By.ID, "param2").send_keys(password)
find_element(driver, By.XPATH, "/html/body/main/div/section/div[2]/form/div/div[1]/i/input").click()
elem = find_element(driver, By.XPATH, "/html/body/main/div/div[2]/div/div[2]/div[2]/a")
driver.execute_script("arguments[0].setAttribute('target', '_self')",
elem)
driver.execute_script("arguments[0].click()",
elem)
find_element(driver, By.ID, "win0divPTNUI_LAND_REC_GROUPLET$0").click()
find_element(driver, By.ID, "PT_SIDE$PIMG").click()
def select_action(driver, val):
select = Select(find_element(driver, By.ID, 'TL_RPTD_TIME_PUNCH_TYPE$0'))
select.select_by_value(str(int(val)))
def submit_form(driver):
elem = find_element(driver, By.ID, "TL_WEB_CLOCK_WK_TL_SAVE_PB")
driver.execute_script("arguments[0].setAttribute('onclick', \"submitAction_win0(document.win0, 'TL_WEB_CLOCK_WK_TL_SAVE_PB')\")", elem)
time.sleep(2)
elem.click()
def clock(action, username, password, email_status):
driver = webdriver.Firefox()
login(driver, username, password)
select_action(driver, action)
submit_form(driver)
timeClocked = dt.datetime.now()
time.sleep(3)
if email_status:
send_email("./email.json")
time.sleep(3)
driver.quit()
return timeClocked
def clockAt(action, username, password, end_time, quiet, email_status):
accurate_wait_until(end_time - dt.timedelta(seconds=30), quiet)
driver = webdriver.Firefox()
login(driver, username, password)
select_action(driver, action)
accurate_wait_until(end_time, quiet)
submit_form(driver)
time.sleep(3)
if email_status:
send_email("./email.json")
time.sleep(3)
driver.quit()
def main():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-c', '--clock', type=Clock.from_string, choices=list(Clock))
group.add_argument('-b', '--both', action="store_true")
group.add_argument('-d', '--delayed', action="store_true")
group.add_argument("-i", "--interrupted", action="store_true")
group.add_argument("-n", "--navigate", action="store_true")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-u", "--username")
group.add_argument("-f", "--file")
parser.add_argument("-p", "--password")
group = parser.add_mutually_exclusive_group()
group.add_argument("-et", "--elapsed_time", type=float)
group.add_argument("-t", "--time")
parser.add_argument("-s", "--start")
parser.add_argument("-m", "--minutes", action="store_true", help="Use minutes instead of hours")
parser.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("-e", "--email", action="store_true", help="Specify if you want to send an email with a screenshot")
(args, extra) = parser.parse_known_args()
if(args.file is not None):
json_data = open(args.file).read()
data = json.loads(json_data)
username = data["username"]
password = data["password"]
else:
username = args.username
if args.password is None:
password = getpass.getpass()
else:
password = args.password
if args.clock is not None:
clock(args.clock, username, password)
elif args.navigate:
login(webdriver.Firefox(), username, password)
else:
if args.elapsed_time is not None:
if args.minutes:
elapse_time = dt.timedelta(minutes=args.elapsed_time)
else:
elapse_time = dt.timedelta(hours=args.elapsed_time)
startTime = dt.datetime.now()
if args.both:
startTime = clock(Clock.In, username, password, args.email)
if args.time:
split = list(map(lambda x: int(x), args.time.split(':')))
end_time = dt.datetime.now() \
.replace(hour=split[0],
minute=split[1],
second=split[2])
else:
if args.interrupted:
end_time = eval(open("time.txt", "r").read())
else:
end_time = startTime + elapse_time
f = open("time.txt", "w")
f.write(repr(end_time))
f.close()
clockAt(Clock.Out,
username, password,
end_time,
args.quiet,
args.email)
os.remove("time.txt")
def accurate_sleep(delta_time, quiet):
end_time = now + delta_time
accurate_wait_until(end_time, quiet)
def accurate_wait_until(end_time, quiet):
now = dt.datetime.now()
while(now < end_time):
if not quiet:
print("Time Left: " + str(end_time-now), end='\r')
time.sleep(1)
now = dt.datetime.now()
if __name__ == "__main__":
main()