import pprint import csv import requests import Apikeys BASEURL = "https://api.northpass.com/v2" APIKEY = Apikeys.ANTHOLOGY HEADERS = {"accept": "*/*","X-Api-Key": APIKEY,"content-type": "application/json"} pp=pprint.PrettyPrinter(indent=4) def main(): count = 0 new_full_list = [] while True: count += 1 getpplprops = f"{BASEURL}/properties/people?limit=100&page={count}" # getppl = f"{BASEURL}/people?limit=100&page={count}" getpplreq = requests.get(getpplprops, headers=HEADERS).json() nextlink = getpplreq["links"] for name in getpplreq['data']: uuid = name['id'] subscription_levels = name['attributes']['properties']['subscription_levels'] if subscription_levels is not None:# or "Core" not in subscription_levels: if "Core" not in subscription_levels: try: subs = subscription_levels.split(", ") except AttributeError as e: print(e) subs = subscription_levels # print(name["attributes"]["properties"]["email"]) new_subscriptions = change_property_values(uuid, subs) paired_with_uuid = (uuid, new_subscriptions) new_full_list.append(paired_with_uuid) print(count) if "next" not in nextlink or count == 5: break return new_full_list def change_property_values(uuid, subs): strip_list = [] subs = list(filter(lambda x: x != '', subs)) subs = list(filter(lambda x: x != ' ', subs)) # print("\n") # print(subs) # print(len(subs)) for item in subs: stripped = item.strip() x = "" if "Enhanced+" in stripped: x = stripped.replace('Enhanced+', 'Premium') strip_list.append(x) else: if "Enhanced" in stripped: x = stripped.replace("Enhanced", "Core") elif "Essential" in stripped: x = stripped.replace("Essential", "Core") strip_list.append(x) strip_list = list(filter(lambda x: x != '', strip_list)) strip_list = set(strip_list) # print(strip_list) # print(len(strip_list)) return strip_list def chunk_and_push(new_full_list): fill_props_url = f"{BASEURL}/properties/people/bulk" if len(new_full_list) > 10: for chunk in range(0, len(new_full_list), 10): i = chunk mediumload = [] to_push = new_full_list[i:i+10] for individuals in to_push: subscripts = individuals[1] subscripts = str(subscripts).replace('{', '').replace('}','').replace("'",'') print(subscripts) miniload = { "attributes": { "properties": { "subscription_level": subscripts } }, "id": individuals[0], "type": "person_properties" }, mediumload.append(miniload) payload = { "data": mediumload } print(payload) def backup_current_props(new_full_list): for pers_props in new_full_list: with open('/Users/normrasmussen/Downloads/new_props_backup.csv', 'a') as file: backup = csv.writer(file, delimiter=',') backup.writerow(pers_props) if __name__ == "__main__": new_full_list = main() # backup_current_props(new_full_list) chunk_and_push(new_full_list) # str1 = "Anthology Encompass: Essential" # str2 = str1.replace("Essential", "Core") # print(str1) # print(str2) """ Anthology 101: Essential Anthology Encompass: Essential Anthology Insight: Essential Blackboard Learn: Essential Anthology Planning: Essential Power BI: Essential Anthology Student: Essential Anthology Student: Enhanced Anthology Student: Enhanced+ Anthology National University: Enhanced+ """