-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (106 loc) · 4.75 KB
/
main.py
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
120
121
122
import requests
import csv
import json
import os
from datetime import date, datetime # Import both date and datetime
from dotenv import load_dotenv
load_dotenv() # Load API key and base URL from .env file
API_KEY = os.getenv("API_KEY")
BASE_URL = os.getenv("BASE_URL")
today = date.today().strftime("%Y-%m-%d") # Format date as YYYY-MM-DD
def check_existing_email(email):
url = f"{BASE_URL}/v3/contacts/{email}?startDate={today}&endDate={today}"
headers = {
'Accept': 'application/json',
'api-key': API_KEY
}
try:
response = requests.get(url, headers=headers)
# Check if the response is successful
if response.status_code == 200:
print(f"API request successful for email: {email}")
log_error(email, f"Contact already exist: {response.text}")
return True
else:
print(f"Contact doesn't exist: {email}")
log_error(email, f"API request failed: {response.text}")
return False
except requests.exceptions.RequestException as e:
print(f"Request exception for email: {email}, Error: {e}")
return False
def process_csv_data(csv_file):
existing_emails = set() # Create a set to store existing emails
with open(csv_file, "r") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
email = row["email"]
f_name = row["f_name"]
l_name = row["l_name"]
sic = row["sic"]
company = row["company"]
title = row["title"]
state = row["state"]
industry = row["industry"]
country = row["country"]
city = row["city"]
zip = row["zip"]
county = row["county"]
id = row["id"]
optin = row["optin"]
sms = row["sms"]
landline = row["landline"]
# Check if email already found in the set
if email not in existing_emails:
if not check_existing_email(email):
existing_emails.add(email) # Add email to existing list after confirmation
payload = json.dumps({
"email": email,
"attributes": {
"FIRSTNAME": f_name,
"LASTNAME": l_name,
"SIC": sic,
"COMPANY": company,
"TITLE": title,
"STATE": state,
"INDUSTRY": industry,
"COUNTRY": country,
"CITY": city,
"ZIP": zip,
"COUNTY": county,
"ID": id,
"OPT_IN": optin,
"SMS": sms,
"LANDLINE_NUMBER": landline
},
# ... other fields as needed
})
try:
response = requests.post(
f"{BASE_URL}/v3/contacts",
headers={
'Content-Type': 'application/json',
'Accept': 'application/json',
'api-key': API_KEY
},
data=payload
)
# Check if the response is successful
if response.status_code == 400:
print(f"API request failed for email: {email}")
log_error(email, f"Error: {response.text}")
return False
else:
response.raise_for_status() # Raise an exception for non-200 status codes
print(f"API request successful for email: {email}")
with open("api_log.txt", "a") as logfile:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logfile.write(f"{timestamp} - Email: {email}\tData: {payload}\tResponse: {response.text}\n====================\n")
except requests.exceptions.RequestException as e:
log_error(email, f"API request failed: {e}")
def log_error(email, error_message):
with open("api_error_log.txt", "a") as error_log:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
error_log.write(f"{timestamp} - Email: {email}\tError: {error_message}\n====================\n")
if __name__ == "__main__":
csv_file = "data.csv" # Replace with your CSV file path
process_csv_data(csv_file)