-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
87 lines (68 loc) · 3.26 KB
/
data_loader.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
"""
This script will bootstrap a guide in Builder
with all of currently active employees in Zenefits
Be sure all of the settings in settings.py are up
to date before running this.
"""
import json
import requests
import os
import settings
from customlist_data_builder import CustomlistItemDataBuilder
from builder_client import BuilderClient
def load_employee_data():
# Initialize a Session for Builder and Zenefits
builder_client = BuilderClient(settings.builder_api_key)
zenefits_session = requests.Session()
zenefits_session.headers.update(
{"Authorization": f"Bearer {settings.zenefits_app_key}"}
)
next_url = (
f"https://api.zenefits.com/core/companies/{settings.zenefits_company_id}/people"
)
employee_list = []
while next_url is not None:
response = zenefits_session.get(next_url)
response.raise_for_status()
data = response.json()["data"]
for guide_id, employee_customlist_id in settings.guide_and_list_ids:
customlist_data_builder = CustomlistItemDataBuilder(
guide_id, settings.zenefits_app_key
)
for employee in data["data"]:
# Only add active employees to the list
if not _is_active_employee(employee, zenefits_session):
continue
customlist_data = customlist_data_builder.build(employee)
employee_custom_list_items_url = f"https://builder.guidebook.com/open-api/v1/custom-list-items/?guide={guide_id}&custom_lists={employee_customlist_id}"
# Download and add employee photo to the builder post request if the photo is available
photo_available = False
if employee.get('photo_url'):
img_response = requests.get(employee['photo_url'])
photo_available = True if img_response.status_code == 200 else False
if photo_available:
with open('image.jpg', 'wb') as handler:
handler.write(img_response.content)
with open('image.jpg', 'rb') as handler:
response = builder_client.post(employee_custom_list_items_url, customlist_data, {"thumbnail": handler})
os.remove('image.jpg')
else:
response = builder_client.post(employee_custom_list_items_url, customlist_data)
# Attach the new custom list item to the custom list
relations_data = {
"custom_list": employee_customlist_id,
"custom_list_item": response.json()["id"],
}
builder_client.post("https://builder.guidebook.com/open-api/v1/custom-list-item-relations/", relations_data)
print("Added {} to Builder".format(customlist_data["name"]))
next_url = data["next_url"]
def _is_active_employee(employee, zenefits_session):
response = zenefits_session.get(employee["employments"]["url"])
response.raise_for_status()
employment_list = response.json()["data"]["data"]
for employment in employment_list:
if employment["termination_date"] is None:
return True
return False
if __name__ == "__main__":
load_employee_data()