98 lines
2.9 KiB
Python
98 lines
2.9 KiB
Python
|
|
import requests
|
||
|
|
import pandas as pd
|
||
|
|
from pathlib import Path
|
||
|
|
import Apikeys
|
||
|
|
import os
|
||
|
|
import ts_lstdict
|
||
|
|
|
||
|
|
basefile = "/Users/normrasmussen/Downloads/ts-mca.csv"
|
||
|
|
api_key = Apikeys.TALKSPACE_1099
|
||
|
|
uuid_url = "https://api.northpass.com/v2/people?filter[email][eq]="
|
||
|
|
prop_url = "https://api.northpass.com/v2/properties/people/"
|
||
|
|
headers = {
|
||
|
|
"accept": "*/*",
|
||
|
|
"x-api-key": api_key,
|
||
|
|
"content-type": "application/json",
|
||
|
|
}
|
||
|
|
LISTDICT = ts_lstdict.LISTDICT
|
||
|
|
|
||
|
|
|
||
|
|
def load_file(basefile):
|
||
|
|
dict_list = []
|
||
|
|
row_dict = {}
|
||
|
|
basefile = Path(basefile)
|
||
|
|
completions = pd.read_csv(basefile)
|
||
|
|
curr_index = 0
|
||
|
|
parsed_list = []
|
||
|
|
try:
|
||
|
|
if os.path.isfile(basefile):
|
||
|
|
print(f"File found! Importing {basefile}")
|
||
|
|
for email in completions.itertuples():
|
||
|
|
row_num = email[0]
|
||
|
|
name = email[1]
|
||
|
|
email = email[3]
|
||
|
|
row_dict = {
|
||
|
|
"row_num": row_num,
|
||
|
|
"name": name,
|
||
|
|
"userid": "",
|
||
|
|
"email": email,
|
||
|
|
}
|
||
|
|
dict_list.append(row_dict)
|
||
|
|
print(len(dict_list))
|
||
|
|
tempdf = pd.DataFrame(dict_list)
|
||
|
|
tempdf.drop_duplicates(subset=["name"], keep="first", inplace=True)
|
||
|
|
dict_list = tempdf.to_dict("records")
|
||
|
|
print(len(dict_list))
|
||
|
|
except KeyError as e:
|
||
|
|
print(f"{e} Error")
|
||
|
|
finally:
|
||
|
|
make_calls(dict_list)
|
||
|
|
|
||
|
|
|
||
|
|
def make_calls(dict_list):
|
||
|
|
final_list = []
|
||
|
|
for row_dict in dict_list:
|
||
|
|
name = row_dict["name"]
|
||
|
|
url = uuid_url + f"{row_dict['email']}"
|
||
|
|
response = requests.get(url, headers=headers)
|
||
|
|
print(f"running api call for {name}")
|
||
|
|
if response.status_code == 200:
|
||
|
|
response = response.json()
|
||
|
|
try:
|
||
|
|
uuid = response["data"][0]["id"]
|
||
|
|
url2 = prop_url + f"{uuid}"
|
||
|
|
response = requests.get(url2, headers=headers)
|
||
|
|
data = response.json()
|
||
|
|
userid = data["data"]["attributes"]["properties"]["user_id"]
|
||
|
|
row_dict["userid"] = userid
|
||
|
|
except IndexError as e:
|
||
|
|
print(f"{e} Second error!")
|
||
|
|
finally:
|
||
|
|
pass
|
||
|
|
if row_dict["userid"] is None:
|
||
|
|
pass
|
||
|
|
else:
|
||
|
|
final_list.append(row_dict)
|
||
|
|
print(len(final_list))
|
||
|
|
|
||
|
|
def add_to_csv(LISTDICT):
|
||
|
|
completions = pd.read_csv(basefile)
|
||
|
|
for info in LISTDICT:
|
||
|
|
print(info)
|
||
|
|
row = info["name"]
|
||
|
|
pid = info["userid"]
|
||
|
|
# print(f"PID IS {pid}")
|
||
|
|
completions.loc[completions.index[row], "userid"] = pid
|
||
|
|
#completions = completions[completions["userid"] != "0"]
|
||
|
|
# completions = completions.iloc[:, 0:]
|
||
|
|
print(completions)
|
||
|
|
completions.to_csv(
|
||
|
|
"/Users/normrasmussen/Downloads/Talkspace1099-Completions-All-IDS.csv",
|
||
|
|
index=False,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
# load_file(basefile)
|
||
|
|
add_to_csv(LISTDICT)
|