-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCS_SS_Sync.py
executable file
·82 lines (64 loc) · 2.21 KB
/
RCS_SS_Sync.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
"""Handles primary functions for SharpSpring throughout the project."""
import requests
import json
from os import urandom
from base64 import b64encode
import MySqlInsert
account_id = 'Account_ID_GOES HERE'
secret_key = 'Secret_Key_GOES HERE'
db_user = 'Database username '
db_pass = 'Database Password'
db_host = 'Database IP'
database = 'Database Name'
mysql = MySqlInsert.sql_monster(db_user, db_pass, db_host, database)
def send(m, p):
"""Transfer requests to SharpSpring.
SharpSpring method and params outlined at:
https://YOURAPP.marketingautomation.services/settings/pubapireference#apimethods
"""
request = urandom(24)
requestID = b64encode(request).decode('utf-8')
data = {
'method': m,
'params': p,
'id': requestID
}
# The Sharpspring reference will show encoding the URL with
# http_build_query, but this is the output
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)
# The requests library replaces cURL used in PHP - much simpler.
# Note that all SharpSpring API calls use the POST method
r = requests.post(
url,
data=dataj
)
response = r.json()
return response
def sync(table, db_table, record):
"""Send information through to database."""
count = 0
page = 0
while True:
p = {'where': {}, 'offset': page}
contacts = send(table, p)
contacts = contacts['result'][record]
mysql.insert_sql(db_table, contacts)
for contact in contacts:
count += 1
if len(contacts) < 500:
break
elif page >= 8500:
break
else:
page += 500
print('{} Records processed for {}'.format(count, db_table))
return count
if __name__ == '__main__':
sync('getLeads', 'TABLENAME', 'lead')
sync('getOpportunities', 'TABLENAME', 'opportunity')
sync('getAccounts', 'TABLENAME', 'account')
sync('getOpportunityLeads', 'TABLENAME', 'getWhereopportunityLeads')
mysql.close_cnx()