import requests
import pandas as pd

apiKey = "HWxj6VTNPwbc3WghFTPzr7SjE"
# Wild Health

jan_groupid = "504c4771-223a-447f-9ec6-08e51bc9ca23"
july_groupid = "a988c9b0-d9f7-400e-a859-48a0fb167da7"
groupurl = f"https://api.northpass.com/v2/groups/{jan_groupid}/memberships"


def getemailsfromGroup(apiKey, jan_groupid, groupurl):
    x = 0
    emaillist = []
    while True:
        x += 1
        url = f"https://api.northpass.com/v2/groups/{jan_groupid}/memberships?page={x}"
        headers = {"accept": "application/json", "X-Api-Key": apiKey}
        response = requests.get(url, headers=headers)
        jsonResponse = response.json()
        next = jsonResponse["links"]

        for emails in jsonResponse["included"]:
            email = emails["attributes"]["email"]
            print(email)
            emaillist.append(email)

        if "next" not in next:
            break
    print(emaillist)
    pd.DataFrame(emaillist).to_csv('/Users/normrasmussen/Downloads/emails_in_group.csv')


if __name__ == "__main__":
    getemailsfromGroup(apiKey, jan_groupid, groupurl)
