-
Notifications
You must be signed in to change notification settings - Fork 7
/
humanitydebug.py
139 lines (109 loc) · 3.85 KB
/
humanitydebug.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
import requests
import datetime
app_id = 'your app id'
app_secret = 'your app secret'
user = 'your humanity user'
pwd = 'your humanity password'
class HumanityData:
def __init__(self, app_id, app_secret, user, pwd):
self.app_id = app_id
self.app_secret = app_secret
self.user = user
self.pwd = pwd
self.token = ''
self.refresh_token = ''
self.today = 'Nobody'
self.tomorrow = 'Nobody'
self.data = None
self.count = 0
self.get_token()
def get_token(self):
"""get access token from humanity api"""
auth_url = 'https://www.humanity.com/oauth2/token.php'
auth_data = { "client_id": self.app_id,
"client_secret": self.app_secret,
"grant_type": "password",
"username": self.user,
"password": self.pwd }
try:
r = requests.post( auth_url, data = auth_data )
self.token = r.json()["access_token"]
self.refresh_token = r.json()["refresh_token"]
self.count = 0
except:
print('couldnt get the access token. check config')
def get_new_token(self):
auth_url = 'https://www.humanity.com/oauth2/token.php'
auth_data = { "client_id": self.app_id,
"client_secret": self.app_secret,
"grant_type": "refresh_token",
"refresh_token": self.refresh_token
}
try:
r = requests.post( auth_url, data = auth_data )
self.token = r.json()["access_token"]
self.refresh_token = r.json()["refresh_token"]
self.count = 0
except:
print('couldnt get access token from refresh_token')
print(r.text)
def check_count(self):
"""check if it's time to refresh token"""
self.count +=1
if self.count > 50:
self.get_new_token()
def get_me(self):
url = 'https://www.humanity.com/api/v2/me'
payload = { 'access_token': self.token }
try:
self.check_count()
r = requests.get( url, params = payload )
print(r.text)
except:
print('something went wrong')
def get_onnow(self):
url = 'https://www.humanity.com/api/v2/dashboard/onnow'
payload = { 'access_token': self.token }
try:
self.check_count()
r = requests.get( url, params = payload )
print(r.text)
except:
print('something went wrong')
def get_shifts(self):
"""get shifts from humnanit"""
url = 'https://www.humanity.com/api/v2/shifts'
# limit to only 3 days from now. can be changed later
start_date = "{:%Y-%m-%d}".format(datetime.datetime.now())
end_date = "{:%Y-%m-%d}".format(datetime.datetime.now() + datetime.timedelta(days=3))
payload = { 'start_date': start_date, 'end_date': end_date, 'access_token': self.token }
try:
self.check_count()
r = requests.get( url, params = payload )
shifts = r.json()['data']
except:
print('something went wrong while trying to get shifts')
shifts = []
return shifts
def get_date_shift(self, shifts, shift_date):
employees = []
for i in shifts:
if i['start_date']['day'] == shift_date.day and i['start_date']['month'] == shift_date.month and i['start_date']['year'] == shift_date.year:
if 'employees' in i.keys():
for j in i['employees']:
employees.append(j['name'])
if len(employees) == 0:
employees.append('Nobody')
return employees
def update(self):
shifts = self.get_shifts()
print(shifts)
print("getting today shifts")
self.today = self.get_date_shift(shifts, datetime.datetime.now())
print(self.today)
print("getting tomorrow shifts")
self.tomorrow = self.get_date_shift(shifts, datetime.datetime.now() + datetime.timedelta(days=1))
print(self.tomorrow)
if __name__ == "__main__":
h = HumanityData(app_id, app_secret, user, pwd)
h.update()