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..d06867b 100644 --- a/README.md +++ b/README.md @@ -46,3 +46,11 @@ Directly by composer: ```sh docker compose up ``` + +# 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 +``` diff --git a/fuckWoffu.py b/fuckWoffu.py index 65fad3f..c1e03fe 100644 --- a/fuckWoffu.py +++ b/fuckWoffu.py @@ -1,3 +1,4 @@ +import argparse import sched import time import logging @@ -15,7 +16,9 @@ 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() @@ -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"