-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailchimpAPI.py
More file actions
281 lines (209 loc) · 8.1 KB
/
Copy pathmailchimpAPI.py
File metadata and controls
281 lines (209 loc) · 8.1 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Code for the MailChimp API for the Hack-Rice App.
Author:
Hamza Nauman
"""
import requests
import json
# import sys
import hashlib
# API_KEY = '5b304c282231deed394636cca6ff15e8-us14'
API_KEY = 'b91c082742d8f1ae8006737167647517-us12' # Hack Rice's API key
HOST = "https://us12.api.mailchimp.com/3.0/"
REMINDER_LIST_ID = '785a935423';
REMINDER_ENDPOINT = "lists/" + REMINDER_LIST_ID + "/members"
REMINDER_URL = "%s%s" % (HOST, REMINDER_ENDPOINT)
def create_new_list(name, company, address1, city, state, zip, permission_reminder, from_name, from_email, subject):
"""
Creates a new empty list.
:param name: The name of the list.
:param company: The company name for the list.
:param address1: The street address for the list contact.
:param city: The city for the list contact.
:param state: The state for the list contact.
:param zip: The postal or zip code for the list contact.
:param permission_reminder: Permission reminder for the list - description of why a subscriber is receiving email
:param from_name: The default from name for campaigns sent to this list.
:param from_email: The default from email for campaigns sent to this list.
:param subject: The default subject line for campaigns sent to this list.
:return:
"""
ENDPOINT = "lists"
url = "%s%s" % (HOST, ENDPOINT)
params = {
"name": name,
"contact": {
"company": company,
"address1": address1,
"city": city,
"state": state,
"zip": zip,
"country": "US"
},
"permission_reminder": permission_reminder,
"campaign_defaults": {
"from_name": from_name,
"from_email": from_email,
"subject": subject,
"language": "en"
},
"email_type_option": False
}
response = requests.post(url, auth=('', API_KEY), data=json.dumps(params))
return response
# print(create_new_list("Testing With API", "Hamza Inc", "6320 Main St", "Houston", "Texas", "77005", "Because!",
# "Hamza Nauman", "lalahamza@lala.com", "Hello Hello"))
def convert_email_to_md5(email):
"""
Converts an email address of a user to hash values using MD5 Algorithm. Used as a helper function for
delete_from_list.
"""
m = hashlib.md5()
m.update(email.lower().encode())
return m.hexdigest()
def delete_from_list(list_id, email):
"""
Delete a given email address from the list with the given list ID.
"""
subscriber_hash = convert_email_to_md5(email)
ENDPOINT = "lists/" + list_id + "/members/" + subscriber_hash
url = "%s%s" % (HOST, ENDPOINT)
response = requests.delete(url, auth=('', API_KEY))
return response
# print(delete_from_list("d0c0c7514d", "hamzanauman@hotmail.com"))
def add_to_list(list_id, email, first_name, last_name):
"""
Adds a subscriber to the specified list.
"""
ENDPOINT = "lists/" + list_id + "/members"
url = "%s%s" % (HOST, ENDPOINT)
params = {}
params["status"] = "subscribed"
params["email_address"] = email
params["merge_fields"] = {"FNAME": first_name, "LNAME": last_name}
response = requests.post(url, auth=('', API_KEY), data=json.dumps(params))
return response.json()
# print (add_to_list("d0c0c7514d","hamzanauman@hotmail.com","Hamza","Nauman"))
def get_list_emails(response):
return [member['email_address'] for member in response.json()['members']]
def add_to_reminder_list(email):
response = requests.get(REMINDER_URL, auth=('', API_KEY));
post_response = None
# Check if the email is not in the list already
if email not in get_list_emails(response):
post_response = add_to_list(REMINDER_LIST_ID, email, "Hack Rice ", "Organizer")
return post_response
# print (add_to_reminder_list("spencerc99@gmail.com"))
def remove_from_reminder_list(email):
"""
Outputs None on success.
"""
response = requests.get(REMINDER_URL, auth=('', API_KEY))
post_response = None
# Check if the email is in the list to be able to delete it
if email in get_list_emails(response):
post_reponse = delete_from_list(REMINDER_LIST_ID, email)
return post_response
def get_lists_info():
"""
Returns a list of dictionaries, each containing information about each list of subscribers. Used as a helper
function to get specific info about a list.
"""
ENDPOINT = "lists"
url = "%s%s" % (HOST, ENDPOINT)
response = requests.get(url, auth=('', API_KEY))
return response.json()["lists"]
# print (get_lists_info())
def get_list_id(list_name):
"""
Returns the unique ID for a list given its name (letter case does not matter).
"""
lists_info = get_lists_info()
for list in lists_info:
if list["name"].lower() == list_name.lower():
return list["id"]
# print(get_list_id("testing with api"))
def create_new_html_campaign(list_id, subject_line, title, from_name, reply_to):
"""
Creates a new campaign with an HTML type, i.e. the content is in HTML. Returns campaign ID.
"""
ENDPOINT = "campaigns"
url = "%s%s" % (HOST, ENDPOINT)
params = {
"type": "regular",
"recipients": {
"list_id": list_id
},
"settings": {
"subject_line": subject_line,
"title": title,
"from_name": from_name,
"reply_to": reply_to
}
}
response = requests.post(url, auth=('', API_KEY), data=json.dumps(params))
if "id" in response:
return response.json()["id"]
else:
return response
# print(create_new_html_campaign("d0c0c7514d", "Hello", "Test Campaign", "Hamza", "hn9@rice.edu"))
def set_campaign_content_html(campaign_id, html):
"""
Set the content of a campaign given the HTML it will contain.
"""
ENDPOINT = "campaigns/" + campaign_id + "/content"
url = "%s%s" % (HOST, ENDPOINT)
params = {
"html": html
}
response = requests.put(url, auth=('', API_KEY), data=json.dumps(params))
return response
#hello
def get_campaign_id(name):
"""
Returns the Campaign ID of the campaign with the given name. Letter case does not matter.
"""
ENDPOINT = "campaigns"
url = "%s%s" % (HOST, ENDPOINT)
response = requests.get(url, auth=('', API_KEY))
for campaign in response.json()["campaigns"]:
if campaign["settings"]["title"].lower() == name.lower():
return campaign["id"]
# print(get_campaign_id("Apply Invite"))
# print(get_campaign_id("Test Campaign"))
# print(set_campaign_content_html(get_campaign_id("Test Campaign"), "<p>Message goes here <p>"))
#
def send_campaign(campaign_id):
"""
Sends out a campaign with the given Campaign ID.
"""
ENDPOINT = "campaigns/" + campaign_id + "/actions/send"
url = "%s%s" % (HOST, ENDPOINT)
response = requests.post(url, auth=('', API_KEY))
return response
# print(send_campaign(get_campaign_id("Test Campaign")))
def schedule_campaign(campaign_id,date,hour):
"""
Schedules a given campaign to be sent at a particular data and hour. Note that campaigns can only be
sent at each hour, and not in between, using this method.
:param campaign_id: Campaign ID of the campaign to be scheduled.
:param date: Date when campaign needs to be sent, in format YYYY-MM-DD
:param hour: Hour of the day when campaign is sent, in 24-hour format. 1PM would be 13.
"""
ENDPOINT = "campaigns/" + campaign_id + "/actions/schedule"
url = "%s%s" % (HOST, ENDPOINT)
params = {
"schedule_time": date + "T" + str(hour) + ":00:00+00:00"
}
response = requests.post(url, auth=('', API_KEY), data=json.dumps(params))
return response.json()
# print(schedule_campaign(get_campaign_id("Test Campaign"), "2017-02-02", 2))
def unschedule_campaign(campaign_id):
"""
Unschedule an already scheduled campaign with the given Campaign ID.
"""
ENDPOINT = "campaigns/" + campaign_id + "/actions/unschedule"
url = "%s%s" % (HOST, ENDPOINT)
response = requests.post(url, auth=('', API_KEY))
return response
# print(unschedule_campaign(get_campaign_id("Test Campaign")))