-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrack.py
executable file
·88 lines (66 loc) · 2.17 KB
/
track.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
import argparse
import json
import requests
from datetime import datetime
from requests_hawk import HawkAuth
USER_ID = '<user_id>'
API_KEY = '<api_key>'
TIMEZONE_NAME = 'CET'
TIMEZONE = '+0100'
def get_time():
(dt, micro) = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f').split('.')
return '%s.%03dZ' % (dt, int(micro) / 1000)
def do_start():
payload = {
'userId': USER_ID,
'start': get_time(),
'end': None,
'timezoneName': TIMEZONE_NAME,
'timezone': TIMEZONE,
'type': 'work'
}
url = 'https://app.absence.io/api/v2/timespans/create'
data = json.dumps(payload)
hawk_auth = HawkAuth(id=USER_ID, key=API_KEY, server_url=url)
request_response = requests.post(url, auth=hawk_auth, data=data, headers={'Content-Type': 'application/json'})
return request_response
def do_stop():
payload = {
'filter': {
'userId': USER_ID,
'end': {'$eq': None}
},
'limit': 10,
'skip': 0
}
url = 'https://app.absence.io/api/v2/timespans'
data = json.dumps(payload)
hawk_auth = HawkAuth(id=USER_ID, key=API_KEY, server_url=url)
request_response = requests.post(url, auth=hawk_auth, data=data, headers={'Content-Type': 'application/json'})
if request_response.ok:
request_response = json.loads(request_response.text)
entry = request_response['data'][0]
else:
return False
payload = {
'start': entry['start'],
'end': get_time(),
'timezoneName': TIMEZONE_NAME,
'timezone': TIMEZONE
}
url = 'https://app.absence.io/api/v2/timespans/{}'.format(entry['_id'])
data = json.dumps(payload)
hawk_auth = HawkAuth(id=USER_ID, key=API_KEY, server_url=url)
request_response = requests.put(url, auth=hawk_auth, data=data, headers={'Content-Type': 'application/json'})
return request_response
parser = argparse.ArgumentParser()
parser.add_argument('do', help='start/stop')
args = parser.parse_args()
if args.do == 'start':
response = do_start()
else:
response = do_stop()
if response is not False and response.ok:
print('Done.')
else:
print('Error: ' + response.text)