-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-api.py
110 lines (91 loc) · 3.5 KB
/
app-api.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
import json
import datetime as dt
import apprise
import requests
def toggle_keka_attendance(org, headers, locationAddress, punchStatus):
method = "POST"
url = f"https://{org}.keka.com/k/dashboard/api/mytime/attendance/webclockin"
payload = {
"timestamp": (dt.datetime.now() - dt.timedelta(hours=5, minutes=30)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
"attendanceLogSource": 1,
"locationAddress": locationAddress,
"manualClockinType": 1,
"note": "",
"originalPunchStatus": punchStatus # 0 for clock-in, 1 for clock-out
}
response = requests.request(
method,
url,
headers=headers,
json=payload
)
return response
def keka_attendance(org, access_token, locationAddress=None):
headers = {
'authority': f'{org}.keka.com',
'accept': 'application/json, text/plain, */*',
'accept-language': 'en-US,en;q=0.9',
'authorization': f'Bearer {access_token}',
'cache-control': 'no-cache',
'content-type': 'application/json; charset=UTF-8',
'origin': f'https://{org}.keka.com',
'pragma': 'no-cache',
'referer': f'https://{org}.keka.com/',
'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
'x-requested-with': 'XMLHttpRequest'
}
method = "GET"
url = f"https://{org}.keka.com/k/dashboard/api/mytime/attendance/attendancedayrequests"
response = requests.request(
method,
url,
headers=headers,
)
if response.status_code == requests.codes.ok:
data = response.json()
else:
print(response.status_code)
print(response.text)
apobj.notify(title="Keka Attendance", body="Token Expired")
raise Exception("Token Expired")
# with open("response_data.json", "w") as f:
# f.write(json.dumps(data, indent=4))
if data['data']['webclockinLastEntry']:
punchStatus = data['data']['webclockinLastEntry']['punchStatus']
else:
punchStatus = 1
# if punchStatus:
# print("Keka already clocked-out, clocking in")
# else:
# print("Keka already clocked-in, clocking out")
response = toggle_keka_attendance(org, headers, locationAddress, not punchStatus)
return response, punchStatus
with open('config.json') as f:
config = json.load(f)
apobj = apprise.Apprise()
for item in config.values():
if item.get("pbul_access_token"):
apobj.add(f"pbul://{item['pbul_access_token']}", tag='pbul')
response, punchStatus = keka_attendance(
org=item['org'],
access_token=item['access_token'],
locationAddress=item.get('locationAddress')
)
if response.status_code == requests.codes.ok:
if punchStatus:
print("clocked-in")
apobj.notify(title="Keka Attendance", body="clocked-in")
else:
print("clocked-out")
apobj.notify(title="Keka Attendance", body="clocked-out")
with open('response_data.json', 'w') as f:
json.dump(response.json(), f, indent=4)
else:
print('failed')
apobj.notify(title="Keka Attendance", body="Keka attendance bot failed")