-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
173 lines (140 loc) · 6.06 KB
/
app.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
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
"""
IMPORTANT:
Create a credentials.py file with the following vars:
MAILCHIMP_API_KEY = '' #INSTERT API KEY HERE
MAILCHIMP_SERVER_PREFIX = '' #INSERT SERVER PREFIX HERE
list_ID = "" #INSERT LIST ID HERE
"""
from flask import Flask, request, redirect, abort, url_for, Response
from flask_cors import CORS, cross_origin
from mailchimp_marketing.api_client import ApiClientError
import mailchimp_marketing as MCM
import hashlib, json, credentials, urllib, logging
import requests
logging.basicConfig(filename='app.log', level=logging.DEBUG)
def subscribeAnUnsubscribedUser(signup_email_hash, mailchimp):
try:
subscribe_response = mailchimp.lists.update_list_member(credentials.list_ID, signup_email_hash, body={
"status": "subscribed"
})
logging.info(subscribe_response)
return 'success'
except ApiClientError as error:
logging.error('ERROR: {}'.format(error.text)+ "FOR USER(hashed MD5) "+ signup_email_hash)
return 'error'
def addLabelToUser(signup_email_hash, mailchimp):
try:
#Add newsletter tag to user
tag_response = mailchimp.lists.update_list_member_tags(credentials.list_ID, signup_email_hash, body={
"tags": [{
"name": "newsletter",
"status": "active"
}]
})
logging.info(tag_response)
return 'success'
except ApiClientError as error:
logging.error('ERROR: {}'.format(error.text) + "FOR USER (hashed MD5) "+ signup_email_hash)
return 'error'
def addGivenNameToUser(signup_name, signup_email_hash, mailchimp):
try:
response = mailchimp.lists.update_list_member(credentials.list_ID, signup_email_hash, body={
"merge_fields": {
"NAME": signup_name
}
})
logging.info(response)
return 'success'
except ApiClientError as error:
logging.error('ERROR: {}'.format(error.text) + "FOR USER (MD5) "+ signup_email_hash)
return 'error'
def connectToMailchimp():
#Try connecting to mailchimp client
try:
mailchimp = MCM.Client()
mailchimp.set_config({
"api_key": credentials.MAILCHIMP_API_KEY,
"server": credentials.MAILCHIMP_SERVER_PREFIX
})
except ApiClientError as error:
logging.error('ERROR: {}'.format(error.text))
return 'error'
return mailchimp
## Get user's email if it exists
def getUserEmailFromMailChimp(email, email_hash, mailchimp):
if(not email):
return False
try:
response = mailchimp.lists.get_list_member(credentials.list_ID, email_hash)
logging.info(response)
return 'success'
except ApiClientError as error:
logging.error('ERROR: {}'.format(error.text))
return 'error'
## Check if the user is already subscribed
def checkIfSubscribed(mc_dat):
if('status' in mc_dat and mc_dat['status'] == 'subscribed'):
return True
else:
return False
def subscribeUser(signup_email, signup_name, mailchimp):
user = {
"email_address": signup_email,
"status": "subscribed",
"merge_fields": {
"NAME": signup_name
},
}
try:
add_response = mailchimp.lists.add_list_member(credentials.list_ID, user)
return (add_response)
except ApiClientError as error:
logging.error('ERROR: {}'.format(error.text) + "FOR USER "+ signup_email)
return 'error'
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-type'
@app.route("/")
@cross_origin()
def helloWorld():
return('HELLO')
@app.route('/signup', methods=['POST'])
@cross_origin(['https://ruhacks.com', 'https://www.ruhacks.com'])
def connectTo():
return_vars = {}
if(not hasattr(request, 'json') or not request.json.get('formData') or not request.json.get('formData').get('EMAIL') or not request.json.get('formData').get('NAME')):
return_vars['missing_data'] = 'True'
return Response(json.dumps(return_vars), status=404, mimetype='application/json')
mailchimp = connectToMailchimp() #Establish connection
signup_email = request.json.get('formData').get('EMAIL') #Get email from form
signup_email_hash = hashlib.md5(signup_email.encode('utf-8')).hexdigest() #Get Email hash
signup_name = request.json.get('formData').get('NAME') #Get name from form
if("@" not in signup_email):
return_vars['invalid_email'] = 'True'
return Response(json.dumps(return_vars), status=400, mimetype='application/json')
mc_dat = getUserEmailFromMailChimp(signup_email,signup_email_hash,mailchimp) #Get mailchimp respopnse
if(mc_dat != False and mc_dat != 'error'): #if user exists in our contacts
if(checkIfSubscribed(mc_dat)): #if user is already subscribed
addLabelToUser(signup_email_hash, mailchimp)
addGivenNameToUser(signup_name, signup_email, mailchimp)
return_vars['already_subscribed'] = 'True'
return Response(json.dumps(return_vars), status=200, mimetype='application/json')
else:
subscribeAnUnsubscribedUser(signup_email_hash, mailchimp)
addLabelToUser(signup_email_hash, mailchimp)
addGivenNameToUser(signup_name, signup_email_hash, mailchimp)
return_vars['added_subscription'] = 'True'
return Response(json.dumps(return_vars), status=201, mimetype='application/json')
else:
subscribeUser(signup_email, signup_name, mailchimp)
addLabelToUser(signup_email_hash, mailchimp)
return_vars['added_subscription'] = 'True'
return Response(json.dumps(return_vars), status=201, mimetype='application/json')
return Response(json.dumps(return_vars), status=302, mimetype='application/json')
#@app.route('/calEvents', methods=['GET'])
#def get_data():
# calendlyConnection = requests.get('https://calendly.com/api/v1/users/me/event_types', headers= {'X-TOKEN': credentials.CALENDLY_API_KEY})
# print(calendlyConnection.text)
# return('done')
if __name__ == "__main__":
app.run(host='0.0.0.0')