3
3
import json
4
4
import requests
5
5
from dotenv import load_dotenv
6
- from datetime import date , datetime # Import both date and datetime
7
-
6
+ from datetime import date , datetime
7
+ import tqdm
8
+ import time
8
9
9
10
def process_csv_data (csv_file ):
10
- """
11
- Processes data from a CSV file and sends it to the Brevo API endpoint.
11
+ """
12
+ Processes data from a CSV file and sends it to the Brevo API endpoint.
13
+ """
14
+ # Load environment variables
15
+ load_dotenv ()
16
+ base_url = os .getenv ("BREVO_API_URL" )
17
+ api_key = os .getenv ("BREVO_API_KEY" )
18
+
19
+ today = date .today ().strftime ("%Y-%m-%d" )
20
+ first_iteration = True
21
+
22
+ # Open log files
23
+ success_log = open ("api_success_log.txt" , "a" )
24
+ error_log = open ("api_log_error.txt" , "a" )
12
25
13
- Args:
14
- csv_file (str): Path to the CSV file.
15
- """
16
- # Load environment variables from .env file
17
- load_dotenv ()
18
- base_url = os .getenv ("BREVO_API_URL" )
19
- api_key = os .getenv ("BREVO_API_KEY" )
26
+ # Process the CSV file
27
+ with open (csv_file , "r" ) as csvfile :
28
+ reader = csv .DictReader (csvfile )
29
+ total_rows = sum (1 for _ in reader ) # Count rows
20
30
21
- today = date .today ().strftime ("%Y-%m-%d" ) # Format date as YYYY-MM-DD
31
+ with open (csv_file , "r" ) as csvfile :
32
+ reader = csv .DictReader (csvfile )
22
33
23
- success_log = open ("api_success_log.txt" , "a" )
24
- error_log = open ("api_log_error.txt" , "a" ) # Open error log file for appending
34
+ with tqdm .tqdm (total = total_rows , desc = "Processing Contacts" , dynamic_ncols = True ) as progress_bar :
35
+ for row in reader :
36
+ if first_iteration :
37
+ time .sleep (0 )
38
+ first_iteration = False
39
+ else :
40
+ time .sleep (60 )
25
41
26
- with open (csv_file , "r" ) as csvfile :
27
- reader = csv .DictReader (csvfile )
28
- for row in reader :
29
- # Extract data from each row
30
- email = row ["email" ]
31
- fname = row .get ("FNAME" , "" )
32
- lname = row .get ("LNAME" , "" )
33
- # ... Extract other data from row as needed ...
34
- timestamp = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
42
+ # Extract data
43
+ email = row ["email" ]
44
+ fname = row .get ("FNAME" , "" )
45
+ lname = row .get ("LNAME" , "" )
46
+ # ... Extract other data from row as needed ...
35
47
48
+ timestamp = datetime .now ().strftime ("%Y-%m-%d %H:%M:%S" )
49
+ # Build payload dictionary
50
+ payload = {
51
+ "email" : email ,
52
+ "includeListIds" : [13 ],
53
+ "redirectionUrl" : "https://www.4sgm.com/category/422/Thank-You.html?utm_source=brevo&utm_campaign=2024-03-batch1-E&utm_medium=email" ,
54
+ "templateId" : 46 ,
55
+ "attributes" : {
56
+ "FNAME" : fname ,
57
+ "LNAME" : lname ,
58
+ # ... Add other attributes from row data ...
59
+ },
60
+ "excludeListIds" : [6 ],
61
+ }
36
62
37
- # Build payload dictionary
38
- payload = {
39
- "email" : email ,
40
- "includeListIds" : [13 ],
41
- "redirectionUrl" : "https://www.4sgm.com/category/422/Thank-You.html?utm_source=brevo&utm_campaign=2024-03-batch1-E&utm_medium=email" ,
42
- "templateId" : 46 ,
43
- "attributes" : {
44
- "FNAME" : fname ,
45
- "LNAME" : lname ,
46
- # ... Add other attributes from row data ...
47
- },
48
- "excludeListIds" : [6 ],
49
- }
63
+ headers = {
64
+ 'Content-Type' : 'application/json' ,
65
+ 'Accept' : 'application/json' ,
66
+ 'api-key' : api_key
67
+ }
50
68
51
- headers = {
52
- 'Content-Type' : 'application/json' ,
53
- 'Accept' : 'application/json' ,
54
- 'api-key' : api_key
55
- }
69
+ try :
70
+ url = f"{ base_url } /v3/contacts/doubleOptinConfirmation"
71
+ response = requests .post (url , headers = headers , json = payload )
72
+ response .raise_for_status ()
56
73
57
- try :
58
- url = f"{ base_url } /v3/contacts/doubleOptinConfirmation" # Combine base URL with endpoint
59
- response = requests . post ( url , headers = headers , json = payload )
60
- response . raise_for_status ( )
74
+ # Successful connection - log data
75
+ success_log . write ( f"{ timestamp } - Sent data for email: { email } \t " )
76
+ success_log . write ( f"Payload: { payload } \n " )
77
+ print ( f"Successfully sent data for email: { email } " )
61
78
62
- # Successful connection - log data
63
- success_log . write ( f" { timestamp } - Sent data for email: { email } \t " )
64
- success_log . write ( f"Payload: { payload } \n " )
65
- print ( f"Successfully sent data for email: { email } " )
79
+ except requests . exceptions . RequestException as e :
80
+ error_message = f"Error sending data for email: { email } - { e } "
81
+ print ( error_message )
82
+ error_log . write ( f" { timestamp } - { error_message } \n " )
66
83
67
- except requests .exceptions .RequestException as e :
68
- error_message = f"Error sending data for email: { email } - { e } "
69
- print (error_message ) # Print to console for visibility
70
- error_log .write (f"{ timestamp } - { error_message } \n " )
84
+ progress_bar .update (1 ) # Update progress bar in-place
71
85
72
86
success_log .close ()
73
- error_log .close () # Close the error log file
87
+ error_log .close ()
74
88
75
89
if __name__ == "__main__" :
76
- csv_file = "data.csv" # Replace with your CSV file path
77
- process_csv_data (csv_file )
90
+ csv_file = "data.csv" # Replace with your CSV file path
91
+ process_csv_data (csv_file )
0 commit comments