-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSharpspring_Common.py
354 lines (299 loc) · 10 KB
/
Sharpspring_Common.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""Provides standard functions for interacting with SharpSpring API."""
import requests
import json
from os import urandom
from base64 import b64encode
import datetime
import csv
import sys
import time
import pandas as pd
account_id = 'Account_ID'
secret_key = 'Secret_Key'
def send(m, p):
request = urandom(24)
requestID = b64encode(request).decode('utf-8')
data = {
'method': m,
'params': p,
'id': requestID
}
url = "http://api.sharpspring.com/pubapi/v1/?accountID={}&secretKey={}"\
.format(account_id, secret_key)
# Important - all requests must be sent in JSON format
dataj = json.dumps(data)
# Note that all SharpSpring API calls use the POST method
r = requests.post(
url,
data=dataj
)
try:
response = r.json()
except Exception:
print('JSON Error')
print(r)
raise Exception('JSON Error')
if response['error']:
try:
if response['error'][0]['code'] == 301:
print("Duplicate hit")
return response
else:
print('API Level error: ')
print(response)
raise Exception('System Error')
except KeyError:
print(response)
return response
def de_dupe(contacts):
existing_contacts = getContacts()
old_emails = []
output = []
for old in existing_contacts:
old_emails.append(old['emailAddress'])
for contact in contacts:
if contact['emailAddress'] in old_emails:
pass
else:
output.append(contact)
print(len(output))
return output
def de_dupe_ops(opportunities):
existing_opportunities = getOpportunities()
old_index = []
output = []
for old in existing_opportunities:
old_index.append(old['CUSTOM ID FIELD'])
for op in opportunities:
if op['CUSTOM ID FIELD'] in old_index:
pass
else:
output.append(op)
print(len(output))
return output
def de_dupe_opleads(opleads):
existing_opleads = getOpleads()
current_opleads = []
output = []
for old in existing_opleads:
current_opleads.append({'leadID': old['leadID'], 'opportunityID': old['opportunityID']})
for oplead in opleads:
if oplead in current_opleads:
pass
else:
output.append(oplead)
print(len(output))
return output
def batch_send_objects(m, data):
results = list()
batches = [data[i:i+450] for i in range(0, len(data), 450)]
for batch in batches:
objects = {'objects': batch}
temp = send(m, objects)
results.append(temp)
time.sleep(1)
return results
def prep_from_file(filename):
with open(filename) as f:
reader = csv.DictReader(f)
output = []
for row in reader:
temp = {}
for x in row:
temp[x] = row[x]
output.append(temp)
return output
def prep_ops(filename):
with open(filename) as f:
reader = csv.DictReader(f)
output = []
for row in reader:
temp = {}
for x in row:
temp[x] = row[x]
temp['closeDate'] = datetime.datetime.strptime(temp['closeDate'], '%m/%d/%Y')
temp['closeDate'] = datetime.datetime.strftime(temp['closeDate'], '%Y-%m-%d')
output.append(temp)
return output
# Updates accounts, identified by account ID and containing dictionary with fields to be updated.
def upload_accounts(accounts):
if len(accounts) > 100:
return(batch_send_objects('createAccounts', accounts))
else:
data = {'objects': accounts}
return(send('createAccounts', data))
def upload_opportunities(ops):
filtered = de_dupe_ops(ops)
if len(filtered) > 100:
return(batch_send_objects('createOpportunities', filtered))
else:
data = {'objects': filtered}
return(send('createOpportunities', data))
def upload_opleads(opleads):
filtered = de_dupe_opleads(opleads)
if len(filtered) > 100:
return(batch_send_objects('createOpportunityLeads', filtered))
else:
data = {'objects': filtered}
return(send('createOpportunityLeads', data))
def upload_contacts(contacts):
filtered = de_dupe(contacts)
print(len(filtered))
if len(filtered) > 100:
return(batch_send_objects('createOpportunities', filtered))
elif len(filtered) > 0:
data = {'objects': filtered}
return(send('createOpportunities', data))
else:
return 'nothing to send!'
def update_accounts(accounts):
if len(accounts) > 100:
return(batch_send_objects('updateAccounts', accounts))
else:
data = {'objects': accounts}
return(send('updateAccounts', data))
def update_contacts(new_contacts):
if len(new_contacts) > 100:
return(batch_send_objects('updateLeads', new_contacts))
else:
data = {'objects': new_contacts}
return(send('updateLeads', data))
def get_contacts():
method = 'getLeads'
offset = 0
records = list()
while True:
params = {'where': {}, 'offset': offset}
result = send(method, params)
for lead in result['result']['lead']:
records.append(lead)
if len(result['result']['lead']) < 500:
break
elif offset > 10000:
break
offset += 500
return(records)
def get_accounts():
method = 'getAccounts'
offset = 0
records = list()
while True:
params = {'where': {}, 'offset': offset}
result = send(method, params)
for account in result['result']['account']:
records.append(account)
if len(result['result']['account']) < 500:
break
elif offset > 10000:
break
offset += 500
return(records)
def get_opleads():
method = 'getOpportunityLeads'
offset = 0
records = list()
while True:
params = {'where': {}, 'offset': offset}
result = send(method, params)
for oplead in result['result']['getWhereopportunityLeads']:
records.append(oplead)
if len(result['result']['getWhereopportunityLeads']) < 500:
break
elif offset > 10000:
break
offset += 500
return(records)
def get_opportunities():
method = 'getOpportunities'
offset = 0
records = list()
while True:
params = {'where': {}, 'offset': offset}
result = send(method, params)
for opportunity in result['result']['opportunity']:
records.append(opportunity)
if len(result['result']['opportunity']) < 500:
break
elif offset > 10000:
break
offset += 500
return(records)
def link_opleads(opportunities, contacts):
count = 0
opleads = []
for contact in contacts:
for op in opportunities:
if (contact['CUSTOM FIELD'] ==
op['CUSTOM FIELD']):
count += 1
temp = {'opportunityID': op['id'], 'leadID': contact['id']}
opleads.append(temp)
data_final = {'objects': opleads}
print('{} opleads generated'.format(count))
return(opleads)
def link_accounts(accounts, contacts):
count = 0
new_contacts = []
for contact in contacts:
for account in accounts:
if (contact['CUSTOM FIELD'] ==
account['accountName']):
count += 1
temp = {'accountID': account['id'], 'id': contact['id']}
new_contacts.append(temp)
print('{} accounts linked'.format(count))
return(new_contacts)
def link_account_ops(ops, accounts):
count = 0
error_count = 0
good_results = []
bad_results = []
for op in ops:
for account in accounts:
if account['CUSTOM FIELD'] is None:
account['CUSTOM FIELD'] = ''
elif op['CUSTOM FIELD'] is None:
op['CUSTOM FIELD'] = ''
if op['CUSTOM FIELD'] == account['CUSTOM FIELD']:
op['accountID'] = account['id']
good_results.append(op)
count += 1
if op['accountID'] == '':
error_count += 1
bad_results.append(op)
elif op['accountID'] is None:
print('WARNING unlinked op: ' + op['accountID'])
print('{} opportunities linked to accounts, with {} errors'
.format(count, error_count))
return good_results, bad_results
def delete_records(records, m):
to_kill = []
for x in records:
temp = {'id': x['id']}
to_kill.append(temp)
if len(new_contacts) > 400:
return(batch_send_objects('updateLeads', new_contacts))
else:
data = {'objects': new_contacts}
return(send('updateLeads', data))
if __name__ == '__main__':
""" Note- it is strongly recommended to use a jupyter notebook following
the below pattern instead of just running this file in case any issues
arise. """
contacts = prep_from_file('CONTACTS FILE')
upload_contacts(contacts)
contacts = get_contacts()
# At time of writing, SharpSpring account creation is not working with
# custom fields, and must be done through the CRM import tool.
# accounts = prep_from_file('ACCOUNTS FILE')
# upload_accounts(accounts)
accounts = get_accounts()
new_contacts = link_accounts(accounts, contacts)
update_contacts(new_contacts)
ops = prep_ops('OPPORTUNITIES FILE')
ops, issues = link_account_ops(ops, accounts)
# You will want to do something with the issues here
upload_opportunities(ops)
ops = get_opportunities()
link_opleads(ops, contacts)
print('Done')