244 lines
9.1 KiB
Python
244 lines
9.1 KiB
Python
|
|
import requests
|
||
|
|
import json
|
||
|
|
import csv
|
||
|
|
import pandas as pd
|
||
|
|
import time
|
||
|
|
import Apikeys
|
||
|
|
from termcolor import colored, cprint
|
||
|
|
|
||
|
|
|
||
|
|
KNOWLEDGEGROUPS = [
|
||
|
|
'7395831e-4325-4b16-85bb-36c94f68aec0',
|
||
|
|
'180571e8-f943-4980-8997-b3eed2a0c141',
|
||
|
|
'2b69f2f8-d567-48c3-8bb3-22e0dc8819bd',
|
||
|
|
'3f8dc68e-1458-4199-9641-6781960e085e',
|
||
|
|
'8585fe89-a050-4dbb-beb8-6ebd7358a970',
|
||
|
|
'd2542667-0dbf-4680-a5af-042d70f24a55',
|
||
|
|
'2b5267b2-ce87-4e77-ad88-5cfec80496b9',
|
||
|
|
'483c3416-ddfb-43fe-983b-08abb6b50c62',
|
||
|
|
'1582d056-55fb-403b-8a65-f3b641c96b69',
|
||
|
|
'be95bdcc-e72a-4132-8a67-9dde9bad5e2a',
|
||
|
|
'0a3412da-5f73-4738-8364-15d5919750f3',
|
||
|
|
'197da27d-0497-40b5-b2f8-cec4124d32f6',
|
||
|
|
'a031d9a8-e433-45cf-826a-8881644f8eac',
|
||
|
|
'02702bf3-261c-41e0-a22d-26d3e90493a3',
|
||
|
|
'3b149bfe-31c5-4991-bd6c-ba4c760089d4',
|
||
|
|
'b6ae5e37-db6a-4b79-949f-be73b216f677',
|
||
|
|
'bfb708e4-18eb-47b5-afde-737f16721e9a',
|
||
|
|
'f02032d3-3d60-4cb1-acac-855c229646c3',
|
||
|
|
'96b24666-85f2-4f70-ae59-f5a924cc045f',
|
||
|
|
'f7701275-cebc-482b-ac31-9cfcd93937c3',
|
||
|
|
'fcfe4ee2-b247-4244-8cfc-f3d98d219fea',
|
||
|
|
'c6b6d415-323e-46c1-859e-be86fd36ec48',
|
||
|
|
'e53216bf-9815-42c7-89c1-953a7b1289a3',
|
||
|
|
'5eeef2ff-1616-43bb-a0c1-aa84ad551824',
|
||
|
|
'59ccfdeb-8a8a-4693-b4fa-27034192071c',
|
||
|
|
'849f1551-604a-4b5c-9b5d-e2771eed488c',
|
||
|
|
'cf5d1920-9618-43f3-8dac-53954d19a956',
|
||
|
|
'0a5c0100-9500-46a5-a7be-40d03fc5dfe9',
|
||
|
|
'4d0bf08e-3dda-4a2e-8213-72a020873a03',
|
||
|
|
'e48c8995-6a64-45c1-ae62-ba96fcc01542',
|
||
|
|
'0ef5fdd2-718c-47d2-88bc-2d0193b18530',
|
||
|
|
'604dd8b8-175a-4a74-93d2-28760f1d1835',
|
||
|
|
'26c5277c-440a-4dea-b625-beb986cff673',
|
||
|
|
'8e33adf0-5932-4535-90c7-10fa04e97201',
|
||
|
|
'1ef34494-4d48-4b69-9819-a22c5870fc24',
|
||
|
|
'b2b8d7aa-06e8-4ed5-bc9b-cb9ce0e81309',
|
||
|
|
'e4017ee0-6141-4145-816f-ed68ee6931bc',
|
||
|
|
'84d32175-8cb8-4fb0-95cc-6ae13d40aaaa',
|
||
|
|
'27489e34-b04c-410e-99a2-0d93e2e42fbf',
|
||
|
|
'e5e8565f-80e2-4462-b687-56f6d64f95e4',
|
||
|
|
'27accc37-c3fd-465f-99cd-3e131081aeca',
|
||
|
|
'32e112bd-5495-4399-85dd-1925e1ccbba5',
|
||
|
|
'dc50ca43-5071-45b3-bf42-e1e64416ffd0',
|
||
|
|
'950a6345-5a13-4931-8d82-eac6adef03e3',
|
||
|
|
'700640e7-0de3-49dc-b441-4efff8ad33ba',
|
||
|
|
'5f35e542-a8cf-4422-8e87-466cdca62864',
|
||
|
|
'f50cb362-2f86-44eb-89e6-bea6ecbaf89f',
|
||
|
|
'31a7cbe0-6aa6-403b-a561-6bc4fa81c0b1',
|
||
|
|
'853de4bd-6f6a-4d1d-980a-b67eb1b0e876',
|
||
|
|
'cd0fa4e0-2d24-4b35-918a-33baa736015e',
|
||
|
|
'933baf03-3664-4c33-bd97-208a9f7ab78b',
|
||
|
|
'55bae3db-5f62-4be3-823a-bcb429b8a2b2',
|
||
|
|
'4754b85b-e7a6-41a8-b0e9-5e02c58ebc38',
|
||
|
|
'33f4fc73-102d-492e-9b0a-383d0b0f68b0',
|
||
|
|
'fa8914be-0986-460c-884d-9973a9622045',
|
||
|
|
'b9f734fa-de0d-4a0b-9ce2-c092126e1d8d',
|
||
|
|
'448f3335-cf11-4e7a-9939-c734861d16e3',
|
||
|
|
'106775db-a00d-4956-bf27-97ea269bb001',
|
||
|
|
'594cd6c0-17db-4241-be56-ad28a8db4f7b',
|
||
|
|
'95f7b67d-3ba8-4d18-bcbb-3e02f7bfaf7a',
|
||
|
|
'0323339c-b92b-481c-9249-651ef0273ad7',
|
||
|
|
'38a42a60-f6e6-4beb-95f1-51c5469dc4c6',
|
||
|
|
'd694804c-ae1a-4db0-b5fc-2497e43abb6f',
|
||
|
|
'a008a4e6-e026-4a1c-8aef-eea78c41b029',
|
||
|
|
'4575114c-1e63-41b1-8953-67d3ce3ed3e6',
|
||
|
|
]
|
||
|
|
|
||
|
|
PERSON_IDS = [
|
||
|
|
]
|
||
|
|
|
||
|
|
APIKEY = Apikeys.ANTHOLOGY
|
||
|
|
BASEURL = "https://api.northpass.com/v2/"
|
||
|
|
HEADERS = {"accept": "application/json", "X-Api-Key": APIKEY}
|
||
|
|
GROUPS = []
|
||
|
|
BASEFILE = "/Users/normrasmussen/Downloads/Anthology_master_backup.csv"
|
||
|
|
|
||
|
|
|
||
|
|
def grab_person_group_ids():
|
||
|
|
rowdict = {"knowledgestate.edu": KNOWLEDGEGROUPS}
|
||
|
|
grab_ppl_ids(rowdict)
|
||
|
|
|
||
|
|
|
||
|
|
def grab_ppl_ids(rowdict):
|
||
|
|
cprint(f"The dictionary is grabbing all the groups. Here's the dict: {rowdict}", 'green')
|
||
|
|
page_count = 0
|
||
|
|
person_list = []
|
||
|
|
while True:
|
||
|
|
# for domain, group_list in rowdict.items():
|
||
|
|
# Grab all people
|
||
|
|
page_count += 1
|
||
|
|
url = (
|
||
|
|
BASEURL
|
||
|
|
+ f"people/?filter[email][cont]=@knowledgestate.edu&limit=100&page={page_count}"
|
||
|
|
)
|
||
|
|
response = requests.get(url, headers=HEADERS)
|
||
|
|
resp = response.json()
|
||
|
|
nextlink = resp["links"]
|
||
|
|
|
||
|
|
for data in resp["data"]:
|
||
|
|
if data["attributes"]["registration_status"] == "activated":
|
||
|
|
person = data["id"]
|
||
|
|
person_list.append(person)
|
||
|
|
else:
|
||
|
|
pass
|
||
|
|
|
||
|
|
if "next" not in nextlink:
|
||
|
|
break
|
||
|
|
|
||
|
|
if len(person_list) > 0:
|
||
|
|
cprint(f"Person list for knowledgestate.edu has {len(person_list)} people.", 'blue')
|
||
|
|
# bulk_remove_and_enroll(person_list, KNOWLEDGEGROUPS)
|
||
|
|
add_group_to_people(person_list)
|
||
|
|
else:
|
||
|
|
cprint(f"Person list for knowledgestate.edu has {len(person_list)} people.", 'blue')
|
||
|
|
cprint(f"Skipping the bulk function.", 'yellow')
|
||
|
|
|
||
|
|
def add_group_to_people(person_list):
|
||
|
|
""" Currently set to add the 3 Ally - (Tx) groups to the knowledgestate people"""
|
||
|
|
addpayload = { "data": [
|
||
|
|
{
|
||
|
|
"id": "7395831e-4325-4b16-85bb-36c94f68aec0",
|
||
|
|
"type": "membership-groups"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"id": "180571e8-f943-4980-8997-b3eed2a0c141",
|
||
|
|
"type": "membership-groups"
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"id": "2b69f2f8-d567-48c3-8bb3-22e0dc8819bd",
|
||
|
|
"type": "membership-groups"
|
||
|
|
}
|
||
|
|
] }
|
||
|
|
for person_uuid in person_list:
|
||
|
|
addurl = f"{BASEURL}people/{person_uuid}/relationships/groups"
|
||
|
|
try:
|
||
|
|
addreq = requests.post(addurl, headers=HEADERS, json=addpayload)
|
||
|
|
good_status_codes = [202, 204, 200, 203]
|
||
|
|
if addreq.status_code in good_status_codes:
|
||
|
|
print("Passed!")
|
||
|
|
print(addreq.status_code)
|
||
|
|
else:
|
||
|
|
cprint(f"Error: {addreq.status_code} with {person_uuid}", 'red')
|
||
|
|
except Exception as e:
|
||
|
|
print(e)
|
||
|
|
finally:
|
||
|
|
pass
|
||
|
|
|
||
|
|
def bulk_remove_and_enroll(person_list, group_list):
|
||
|
|
cprint("Moving people and groups into bulk function.", 'green')
|
||
|
|
COUNT = 0
|
||
|
|
FINISH_SIGNAL = len(person_list)
|
||
|
|
# Get people with groups and remove them from those groups
|
||
|
|
for person in person_list:
|
||
|
|
print(person)
|
||
|
|
COUNT += 1
|
||
|
|
url = BASEURL + f"people/{person}"
|
||
|
|
response = requests.get(url, headers=HEADERS)
|
||
|
|
print(response.status_code)
|
||
|
|
data = response.json()
|
||
|
|
groups = data["data"]["relationships"]["groups"]
|
||
|
|
name = data["data"]["attributes"]["full_name"]
|
||
|
|
print(name)
|
||
|
|
del_group_list = []
|
||
|
|
for group in groups["data"]:
|
||
|
|
del_payload_var = {"id": group["id"], "type": "membership-groups"}
|
||
|
|
del_group_list.append(del_payload_var)
|
||
|
|
del_payload_base = {"data": del_group_list}
|
||
|
|
if not del_payload_base["data"]:
|
||
|
|
print("nothing in del_payload_base")
|
||
|
|
pass
|
||
|
|
else:
|
||
|
|
try:
|
||
|
|
durl = BASEURL + f"people/{person}/relationships/groups"
|
||
|
|
dresponse = requests.delete(durl, headers=HEADERS, json=del_payload_base)
|
||
|
|
print(dresponse.status_code)
|
||
|
|
good_status_codes = [202, 204, 200, 203]
|
||
|
|
if dresponse.status_code in good_status_codes:
|
||
|
|
pass
|
||
|
|
else:
|
||
|
|
cprint(f"Error: {response.status_code} with {name}", 'red')
|
||
|
|
except Exception as e:
|
||
|
|
print(e)
|
||
|
|
finally:
|
||
|
|
pass
|
||
|
|
|
||
|
|
if COUNT == FINISH_SIGNAL:
|
||
|
|
# Since we're de-enrolling one by one, let's sleep and wait.
|
||
|
|
|
||
|
|
cprint("Sleeping for 2 seconds.", 'yellow')
|
||
|
|
time.sleep(2)
|
||
|
|
cprint("Sleep Complete. Hold on to your butts!", 'yellow')
|
||
|
|
|
||
|
|
# Re-enroll everyone back into all the groups by creating subset groups
|
||
|
|
# Trying to do everyone at once (143 ppl * 54 groups) resulted in payloads that are too big
|
||
|
|
# Doing the subsets of 25 ppl each did not yield any errors.
|
||
|
|
composite_list = [person_list[x:x+25] for x in range(0, len(person_list),25)]
|
||
|
|
for people_subset in composite_list:
|
||
|
|
payload = {"payload": {"person_ids": people_subset, "group_ids": group_list}}
|
||
|
|
cprint(f"{payload}", 'green')
|
||
|
|
url = BASEURL + "bulk/people/membership/"
|
||
|
|
|
||
|
|
# The above is commented out because I kept getting a 413 error of too much content
|
||
|
|
# Changing this to enroll each person.... one at a time.
|
||
|
|
# But this is slow and didn't work. It actually just stopped working after around 30 people.
|
||
|
|
# miniload = []
|
||
|
|
# for groupuuid in group_list:
|
||
|
|
# tmpload = {"type":"membership-groups","id":groupuuid}
|
||
|
|
# miniload.append(tmpload)
|
||
|
|
#
|
||
|
|
# print(len(person_list))
|
||
|
|
# for person in person_list:
|
||
|
|
# url = BASEURL + f"people/{person}/relationships/groups"
|
||
|
|
# payload = { "data": miniload }
|
||
|
|
try:
|
||
|
|
cprint(f"Trying for person: {person}", 'green')
|
||
|
|
cprint(f"With payload: {payload}", 'blue')
|
||
|
|
response = requests.post(url, headers=HEADERS, json=payload)
|
||
|
|
response.raise_for_status()
|
||
|
|
except requests.exceptions.HTTPError as err:
|
||
|
|
cprint(f"Error: {response.status_code}. Exception: {err}", 'red')
|
||
|
|
except requests.exceptions.Timeout:
|
||
|
|
cprint("Timeout Error", 'red')
|
||
|
|
except requests.exceptions.TooManyRedirects:
|
||
|
|
cprint("Too Many Redirects Error", 'red')
|
||
|
|
except requests.exceptions.ChunkedEncodingError as ex:
|
||
|
|
cprint(f"Invalid chunk encoding {str(ex)}", 'yellow')
|
||
|
|
finally:
|
||
|
|
cprint(response.status_code, 'yellow')
|
||
|
|
cprint(response.text, 'yellow')
|
||
|
|
cprint("Okay, let's see how that went.", 'red')
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
grab_person_group_ids()
|