-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccai_contacts.py
More file actions
119 lines (102 loc) · 4.74 KB
/
ccai_contacts.py
File metadata and controls
119 lines (102 loc) · 4.74 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
"""CCAI contacts fetcher."""
import os
import requests
from dotenv import load_dotenv
load_dotenv()
class CCAIContactsFetcher:
def __init__(self):
self.client_id = os.getenv("CCAI_CLIENT_ID")
self.api_key = os.getenv("CCAI_API_KEY")
self.core_url = os.getenv("CCAI_CORE_URL")
if not all([self.client_id, self.api_key, self.core_url]):
raise ValueError("Missing CCAI credentials in .env file")
def get_contacts(self, limit=100):
"""Fetch contacts from CCAI."""
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
"clientId": self.client_id,
"accountId": "1223",
}
# Try different possible endpoints
endpoints = [
f"{self.core_url}/api/v1/contacts",
f"{self.core_url}/api/contacts",
f"{self.core_url}/contacts",
f"{self.core_url}/api/v1/accounts",
f"{self.core_url}/api/accounts",
]
for url in endpoints:
try:
print(f"🔍 Trying endpoint: {url}")
response = requests.get(url, headers=headers, params={"limit": limit})
if response.status_code == 200:
data = response.json()
print(
f"✅ Success! Response keys: {list(data.keys()) if isinstance(data, dict) else 'Not a dict'}"
)
# Try to find contacts in different response structures
contacts = []
if isinstance(data, list):
contacts = data
elif isinstance(data, dict):
# Handle paginated response
contacts = data.get(
"content",
data.get(
"contacts", data.get("accounts", data.get("data", []))
),
)
if contacts:
# Convert CCAI format to CSV-like format
formatted_contacts = []
for contact in contacts[:limit]:
formatted_contact = {
"First Name": contact.get(
"firstName", contact.get("first_name", "")
),
"Last Name": contact.get(
"lastName", contact.get("last_name", "")
),
"Email": contact.get("email", ""),
"Company": contact.get("company", ""),
"Person Linkedin Url": contact.get(
"linkedinUrl", contact.get("linkedin_url", "")
),
"Title": contact.get(
"title", contact.get("job_title", "")
),
}
formatted_contacts.append(formatted_contact)
print(
f"✅ Fetched {len(formatted_contacts)} contacts from CCAI"
)
return formatted_contacts
else:
print(
f"⚠️ No contacts found in response. Available keys: {list(data.keys()) if isinstance(data, dict) else 'N/A'}"
)
if isinstance(data, dict) and "content" in data:
print(f"Content length: {len(data['content'])}")
if data["content"]:
print(
f"Sample content item keys: {list(data['content'][0].keys())}"
)
else:
print(f"❌ Status {response.status_code}: {response.text[:200]}")
except requests.exceptions.RequestException as e:
print(f"❌ Error with {url}: {e}")
continue
print("❌ No working endpoint found for contacts")
# Return empty list but don't fail completely
return []
if __name__ == "__main__":
fetcher = CCAIContactsFetcher()
contacts = fetcher.get_contacts(10)
print(f"Found {len(contacts)} contacts")
for contact in contacts[:3]: # Show first 3
print(f"- {contact['First Name']} {contact['Last Name']} ({contact['Email']})")
print(f" LinkedIn: {contact['Person Linkedin Url']}")
print(f" Company: {contact['Company']}")
print()