-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackonio.py
201 lines (178 loc) · 7.33 KB
/
trackonio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import json
import os
import re
import sys
import uuid
import functools
from datetime import datetime, timedelta
from random import randint
import time
import requests
MAIN_URL = "https://"+str(os.environ.get('PERSONIO_HOST',"personio.personio.de"))
EMAIL = str(os.environ.get('PERSONIO_USERNAME'))
PASSWORD = str(os.environ.get('PERSONIO_PASSWORD'))
COOKIE = str(os.environ.get('PERSONIO_COOKIE'))
CALENDAR_ID = str(os.environ.get('CALENDAR_ID', '59097'))
STARTING_HOUR = str(os.environ.get('WORK_START_TIME', '09'))
BREAK_HOUR = str(os.environ.get('BREAK_START_TIME', '13'))
WORKING_HOURS = int(os.environ.get('WORK_DURATION', 8))
WORK_SATURDAY = bool(os.environ.get('WORK_SATURDAY', False))
WORK_SUNDAY = bool(os.environ.get('WORK_SUNDAY', False))
BREAK_TIME_MINUTES = int(os.environ.get('BREAK_DURATION', 60))
RANDOM_TIMES_DELTA = int(os.environ.get('RANDOM_TIMES_DELTA', 0))
RANDOM_DURATIONS_DELTA = int(os.environ.get('RANDOM_TIMES_DELTA', 0))
LOGIN_URL = f'{MAIN_URL}/login/index'
ATTENDANCE_URL = f'{MAIN_URL}/api/v1/attendances/periods'
HOLIDAYS_URL = f'{MAIN_URL}/api/v1/holidays?holiday_calendar_ids[]='
ABSENCES_URL = f'{MAIN_URL}/api/v1/employees'
def check_date(dateInput):
return re.fullmatch(r"\A([\d]{4})-([\d]{2})-([\d]{2})", dateInput)
def generate_attendance(
date, startingHour, breakHour, workingHours, breakDuration, employeeId, randomTimes, randomDurations):
# First Record
startTime = datetime.strptime(f"{date} {startingHour}", "%Y-%m-%d %H")
breakTime = datetime.strptime(f"{date} {breakHour}", "%Y-%m-%d %H")
workingDuration = (workingHours) * 60
# Randomize input times and durations
if randomTimes:
startTime += timedelta(minutes=randint(-randomTimes, randomTimes))
breakTime += timedelta(minutes=randint(-randomTimes, randomTimes))
if randomDurations:
workingDuration = randint(workingDuration - randomDurations, workingDuration + randomDurations)
breakDuration = randint(breakDuration - randomDurations, breakDuration + randomDurations)
if breakDuration == 0:
end_time = startTime + timedelta(minutes=workingDuration)
return [
{
"id": str(uuid.uuid1()),
"start": startTime.strftime("%Y-%m-%dT%H:%M:%SZ"),
"end": end_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"comment": "",
"project_id": None,
"employee_id": employeeId,
"activity_id": None,
},
]
else:
# Second Record
startTime2 = breakTime + timedelta(minutes=breakDuration)
workingDuration2 = int(
workingDuration - (breakTime - startTime).total_seconds() / 60
)
end_time = startTime2 + timedelta(minutes=workingDuration2)
return [
{
"id": str(uuid.uuid1()),
"start": startTime.strftime("%Y-%m-%dT%H:%M:%SZ"),
"end": breakTime.strftime("%Y-%m-%dT%H:%M:%SZ"),
"comment": "",
"project_id": None,
"employee_id": employeeId,
"activity_id": None,
},
{
"id": str(uuid.uuid1()),
"start": startTime2.strftime("%Y-%m-%dT%H:%M:%SZ"),
"end": end_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"comment": "",
"project_id": None,
"employee_id": employeeId,
"activity_id": None,
},
]
if __name__ == "__main__":
if len(sys.argv) == 1 or sys.argv[1] == "--help" or not check_date(sys.argv[1]):
print(
f"""
Error. No argument or wrong date format\n\n
Usage: {__file__} <date> [-f/--fast]\n
Note: Date format yyyy-mm-dd \n
"""
)
exit()
attendanceDate = sys.argv[1]
fastExec = "--slow"
if len(sys.argv) == 3:
fastExec = sys.argv[2]
# Create request session
session = requests.Session()
# Login into Personio
if COOKIE != 'None':
if os.path.exists('.session'):
with open('.session', 'r') as file:
session.cookies.set('personio_session', file.read().strip())
else:
session.cookies.set('personio_session', COOKIE)
print("Authenticating with following Cookie: " + str(session.cookies.get('personio_session')))
response = session.get(MAIN_URL)
# Waiting 1 minute to make sure the returned cookie will be valid in next iteration
if fastExec != "-f" and fastExec != "--fast":
time.sleep(60)
elif EMAIL != 'None' and PASSWORD != 'None':
print("Authenticating with Username/Password: " + EMAIL)
response = session.post(
LOGIN_URL,
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={"email": EMAIL, "password": PASSWORD},
)
if 'response' in locals():
if 'XSRF-TOKEN' in response.cookies:
XSRF_TOKEN=response.cookies['XSRF-TOKEN']
PROFILE_ID=response.text.split("EMPLOYEE={id:")[1].split("}")[0]
else:
print("Failed to login with provided credentials to "+MAIN_URL)
exit()
else:
print("No auth method provided. Please RTFM!")
exit()
# Check User Holiday
response = session.get(
f'{HOLIDAYS_URL}{CALENDAR_ID}&start_date={attendanceDate}&end_date={attendanceDate}'
)
isHoliday = len(json.loads(response.text)['data'])
# Check User Abscense
response = session.get(
f'{ABSENCES_URL}/{PROFILE_ID}/absences/types'
)
absenceTypes = ','.join([str(a['id']) for a in json.loads(response.text)['data'] if str(a['id']) != "2352878"]) # Removing Personio's On-Call absence, if existing
response = session.get(
f'{ABSENCES_URL}/{PROFILE_ID}/absences/periods?filter[startDate]={attendanceDate}&filter[endDate]={attendanceDate}&filter[absenceTypes]={absenceTypes}'
)
isAbsence = len(json.loads(response.text)['data'])
# Check Working Day or Weekend
weekday = datetime.strptime(attendanceDate, "%Y-%m-%d").weekday()
isWeekendLeave = (not WORK_SATURDAY and weekday == 5) or (not WORK_SUNDAY and weekday == 6)
if isHoliday or isAbsence or isWeekendLeave:
if isHoliday: excuse = " You're on Leave"
if isAbsence: excuse = " It's Holiday"
if isWeekendLeave: excuse = "It's weekend"
message = 'Not working day: ' + excuse
print(message)
exit()
# Log the attendance
response = session.post(
ATTENDANCE_URL,
headers={'x-csrf-token':XSRF_TOKEN},
json=generate_attendance(
attendanceDate,
STARTING_HOUR,
BREAK_HOUR,
WORKING_HOURS,
BREAK_TIME_MINUTES,
PROFILE_ID,
RANDOM_TIMES_DELTA,
RANDOM_DURATIONS_DELTA
),
)
data = json.loads(response.text)
personio_cookie = response.cookies.get('personio_session')
if personio_cookie:
with open('.session', 'w') as file:
file.write(personio_cookie)
print("Refreshed session cookie stored successfully")
try:
message = f"Error: {attendanceDate} - {data['error']['message']}"
except KeyError:
message = f"Success: attendance on {attendanceDate} registered!"
print(message)