From 78c0a2a85c24e8a8df7667d2b68770b26f9766fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Hern=C3=A1ndez=20Garc=C3=ADa?= Date: Wed, 20 Nov 2024 20:45:55 +0100 Subject: [PATCH 1/3] Add "test" argument mode. Add half vacation days support --- .gitignore | 1 + .vscode/launch.json | 15 +++++++++++++++ README.md | 12 ++++++++++++ fuckWoffu.py | 37 +++++++++++++++++++++++++++++++------ src/SignInWoffu.py | 27 +++++++++++++++++---------- 5 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index d0d7ee9..401a4ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ logs/fuckWoffu.log secrets.json +config/secrets.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..600cec6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Current File with Arguments", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "args": [ + "--test" + ] + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index d1fc77e..cb347ef 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,15 @@ Directly by composer: ```sh docker compose up ``` + +Here's an improved version of your text: + +--- + +# Testing the Connection + +To verify the connection to Woffu without registering any inputs, you can use the `--test` argument. This operation ensures that your secrets are properly configured without making any changes in Woffu. + +```bash +python3 fuckWoffu.py --test +``` \ No newline at end of file diff --git a/fuckWoffu.py b/fuckWoffu.py index 65fad3f..700562c 100644 --- a/fuckWoffu.py +++ b/fuckWoffu.py @@ -1,3 +1,4 @@ +import argparse import sched import time import logging @@ -15,11 +16,13 @@ Every 60 seconds I check my enter/leave time and execute request ''' TIME_TO_CHECK = 60 - +ERROR_MESSAGE = 'Error maybe something should be done ¯\(ツ)/¯ ' +SO_FAR_SO_GOOD_MESSAGE = 'So far, so good!' +ERROR_TOKEN = '¯\(ツ)/¯' def main(scheduler): email, password, sign_times, company_name = get_json_data() - if is_sign_hour(sign_times): + if True: sign_in_app = SignInWoffu(email, password, company_name) holidays = sign_in_app.get_holiday() if not is_holidays(holidays): @@ -28,15 +31,37 @@ def main(scheduler): notify('Sign in/out succesfully') logging.warning('Sign in succesfully') else: - logging.error('Error maybe something should be done ¯\(ツ)/¯ ') + logging.error() else: logging.warning('I am on holiday, no check in') # Restart the timer scheduler.enter(TIME_TO_CHECK, 1, main, (scheduler,)) +def testConection(): + email, password, sign_times, company_name = get_json_data() + try: + sign_in_app = SignInWoffu(email, password, company_name) + if sign_in_app.token == ERROR_TOKEN: + logging.error(ERROR_MESSAGE) + print(ERROR_MESSAGE) + else: + logging.info(SO_FAR_SO_GOOD_MESSAGE) + print(SO_FAR_SO_GOOD_MESSAGE) + except Exception as e: + message = f'An error occurred during sign-in: {e}' + logging.error(message) + print(message) + if __name__ == "__main__": - scheduler = sched.scheduler(time.time, time.sleep) - scheduler.enter(TIME_TO_CHECK, 1, main, (scheduler,)) - scheduler.run() + parser = argparse.ArgumentParser(description='Woffu Auto Check-In script') + parser.add_argument('--test', help='Optional test parameter', action='store_true') + args = parser.parse_args() + + if args.test: + testConection() + else: + scheduler = sched.scheduler(time.time, time.sleep) + scheduler.enter(TIME_TO_CHECK, 1, main, (scheduler,)) + scheduler.run() diff --git a/src/SignInWoffu.py b/src/SignInWoffu.py index 7b8be6d..db2fb41 100644 --- a/src/SignInWoffu.py +++ b/src/SignInWoffu.py @@ -53,20 +53,27 @@ def _get_pto_holiday(self): return holidays_list def _calculate_vacation_range(self, day, holidays_list): - requested_days = int(day['RequestedFormatted']['Values'][0]) - first_day = datetime.strptime( - day['StartDate'], '%Y-%m-%dT%H:%M:%S.%f') - if requested_days == 1: - holidays_list.append(first_day) - return + requested_days = float(day['RequestedFormatted']['Values'][0]) + first_day = datetime.strptime(day['StartDate'], '%Y-%m-%dT%H:%M:%S.%f') - # Detect vacation range: - last_day = datetime.strptime( - day['EndDate'], '%Y-%m-%dT%H:%M:%S.%f') + # Append the first day + holidays_list.append(first_day) + + # Calculate the entire vacation range + last_day = datetime.strptime(day['EndDate'], '%Y-%m-%dT%H:%M:%S.%f') time_difference = (last_day - first_day).days + 1 - for d in range(time_difference): + + # Append all full days + for d in range(1, int(requested_days)): annother_day = first_day + timedelta(days=d) holidays_list.append(annother_day) + + # Handle fractional day at the end + if requested_days % 1 != 0: + partial_day = first_day + timedelta(days=int(requested_days)) + holidays_list.append(partial_day) + + return holidays_list def _get_token(self, email, password): url = "https://" + self.company_name + ".woffu.com/token" From 869256a8937eeb5937c5bc76c50f927c246031c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Hern=C3=A1ndez=20Garc=C3=ADa?= Date: Wed, 20 Nov 2024 20:47:33 +0100 Subject: [PATCH 2/3] Update README.md --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index cb347ef..d06867b 100644 --- a/README.md +++ b/README.md @@ -47,14 +47,10 @@ Directly by composer: docker compose up ``` -Here's an improved version of your text: - ---- - # Testing the Connection To verify the connection to Woffu without registering any inputs, you can use the `--test` argument. This operation ensures that your secrets are properly configured without making any changes in Woffu. ```bash python3 fuckWoffu.py --test -``` \ No newline at end of file +``` From 3bd504c7f46e698129724f2446c8c3e4f6ae631d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Hern=C3=A1ndez=20Garc=C3=ADa?= Date: Wed, 20 Nov 2024 20:53:35 +0100 Subject: [PATCH 3/3] Restored original sign hour check --- fuckWoffu.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fuckWoffu.py b/fuckWoffu.py index 700562c..c1e03fe 100644 --- a/fuckWoffu.py +++ b/fuckWoffu.py @@ -22,7 +22,7 @@ def main(scheduler): email, password, sign_times, company_name = get_json_data() - if True: + if is_sign_hour(sign_times): sign_in_app = SignInWoffu(email, password, company_name) holidays = sign_in_app.get_holiday() if not is_holidays(holidays):