This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwiw_upload.py
72 lines (62 loc) · 2.14 KB
/
wiw_upload.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
import argparse
from wheniwork import WhenIWork, NoLoginError
import data
import json
from requests import HTTPError
from pathlib import Path
import pickle
import os.path
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('--email')
parser.add_argument('--password')
parser.add_argument('--userid')
parser.add_argument('--apikey')
args = parser.parse_args()
tokenfile_path = 'wiwtoken.pickle'
wiwcreds = None
# wiwtoken.pickle stores the WIW token.
# If it exists, assume we've already got a token, and use that to make requests.
if os.path.exists(tokenfile_path):
with open(tokenfile_path, 'rb') as wiwtokenfile:
wiwcreds = pickle.load(wiwtokenfile)
else: # Authenticate manually using password
if None in (args.email, args.password, args.userid, args.apikey):
raise NoLoginError() # Arguments are not specified
wiwcreds = WhenIWork.get_token(args.apikey, args.email, args.password)
wiwcreds['user_id'] = args.userid
with open(tokenfile_path, 'wb') as wiwtokenfile:
pickle.dump(wiwcreds, wiwtokenfile)
account_id = wiwcreds['person']['id']
# We've authenticated, time to make requests.
wiw = WhenIWork(wiwcreds['token'], wiwcreds['user_id'])
users = wiw.get_users()
location_id = wiw.get_users()['locations'][0]['id'] # Assume there's just one
# Load shifts to upload
with open(args.filename, 'r') as shiftfile:
shifts = json.load(shiftfile)
uploaded = []
failed = []
# Upload shifts
for shift in shifts:
try:
uploaded_shift = wiw.create_shift(
location=location_id,
start=shift['start_time'],
end=shift['end_time'],
user_id=shift['user_id'],
position_id=shift['position_id']
) # Create new shift
uploaded.append((shift, uploaded_shift))
except HTTPError as e:
print(str(e))
failed.append((shift, e))
# Stats
if len(uploaded) > 0:
print('Successfully uploaded shift ids:')
for shift, upload in uploaded:
print(upload['shift']['id'])
if len(failed) > 0:
print('Failed to upload:')
for shift, error in failed:
print(json.dumps(shift)+' Error: '+ str(error))