forked from GCCree/sgizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgizmo.py
223 lines (155 loc) · 6.42 KB
/
sgizmo.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
import requests
import json
from time import sleep
from sgizmo import *
import pandas as pd
_HTTP = "https://"
_SITE = "surveygizmo.com"
DOMAIN = 'restapica'
VERSION = '5'
PARAMS = {"resultsperpage": 500,
"page": 1}
def _get_data(url, attempts=10, wait_sec=3):
attempt_count = 0
for i in range(0, attempts):
try:
attempt_count += 1
output = requests.get(url, verify=False)
if output.ok:
output = output.json()
return output
except KeyboardInterrupt:
pass
except requests.exceptions.RequestException as e:
if attempt_count >= attempts:
print("All attempts failed")
return
print(e, "\nTrying again in", wait_sec, "second(s)...")
sleep(wait_sec)
def _multi_get_data(url, api_token, obj_id='', subobj1='', domain=DOMAIN):
params = PARAMS
output_list = []
output = _get_data(url)
if output["total_pages"] == 1:
return output
else:
output_list.append(output)
for i in range(2, output["total_pages"] + 1):
params["page"] = i
url = make_url(api_token, obj_id=obj_id, subobj1=subobj1, params=params, domain=domain)
output = _get_data(url)
output_list.append(output)
return output_list
def get_survey_list(api_token, domain=DOMAIN):
url = make_url(api_token, domain=domain)
data = _get_data(url)
surveys = data["data"]
df = pd.DataFrame(surveys)
return df
def get_survey(api_token, survey_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, domain=domain)
data = _get_data(url)
survey = data["data"]
return survey
def get_questions(api_token, survey_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1="surveyquestion", domain=domain)
data = _get_data(url)
questions = data["data"]
return questions
def get_question_option(api_token, survey_id, question_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveyquestion',
subobj1_id=question_id, subobj2='surveyoption', domain=domain)
data = _get_data(url)
option = data["data"]
if len(option) > 0:
for opt in option:
opt["question_id"] = question_id
return option
def get_all_survey_options(api_token, survey_id, domain=DOMAIN, wait_sec=0):
questions = get_questions(api_token, survey_id, domain=domain)
options = []
for question in questions:
qid = question["id"]
option = get_question_option(api_token, survey_id, qid)
if len(option) > 0:
options.append(option)
sleep(wait_sec)
return options
def get_survey_responses(api_token, survey_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveyresponse', domain=domain)
data = _multi_get_data(url, api_token=api_token, obj_id=survey_id, subobj1='surveyresponse')
return data
def get_contact_lists(api_token, domain=DOMAIN):
url = make_url(api_token, obj='contactlist', domain=domain)
data = _get_data(url)
contact_lists = data["data"]
return contact_lists
def get_contact_list(api_token, list_id, domain=DOMAIN):
url = make_url(api_token, obj='contactlist', obj_id=list_id, domain=domain)
data = _get_data(url)
contact_list = data["data"]
return contact_list
def get_contacts(api_token, contact_list_id, domain=DOMAIN):
url = make_url(api_token, obj='contactlist', obj_id=contact_list_id, subobj1='contactlistcontact')
data = _get_data(url)
contacts = data['data']
return contacts
def get_contact(api_token, contact_list_id, contact_id, domain=DOMAIN):
url = make_url(api_token, obj='contactlist', obj_id=contact_list_id, subobj1='contactlistcontact', subobj1_id=contact_id, domain=domain)
data = _get_data(url)
contact = data['data']
return contact
def get_campaigns(api_token, survey_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveycampaign', domain=domain)
data = _get_data(url)
campaigns = data['data']
return campaigns
def get_campaign(api_token, survey_id, campaign_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveycampaign', subobj1_id=campaign_id, domain=domain)
data = _get_data(url)
campaign = data['data']
return campaign
def get_campaign_emails(api_token, survey_id, campaign_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveycampaign', subobj1_id=campaign_id, subobj2='emailmessage', domain=domain)
data = _get_data(url)
emails = data['data']
return emails
def get_campaign_email(api_token, survey_id, campaign_id, email_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveycampaign', subobj1_id=campaign_id, subobj2='emailmessage', subobj2_id=email_id, domain=domain)
data = _get_data(url)
email = data['data']
return email
def get_response_info(api_token, survey_id, campaign_id, domain=DOMAIN):
url = make_url(api_token, obj_id=survey_id, subobj1='surveycampaign', subobj1_id=campaign_id, subobj2='surveycontact', domain=domain)
data = _get_data(url)
return data
def make_url(api_token, domain=DOMAIN, version=VERSION, obj='survey',
obj_id='', subobj1 = '', subobj1_id='', subobj2='', subobj2_id='', params=PARAMS):
base_url = _HTTP + domain + '.' + _SITE + '/v' + version + '/'
if api_token[0] == '?':
api_token = api_token[1:]
endpoints = ''
if obj:
endpoints = endpoints + str(obj)
if obj_id:
endpoints = endpoints + '/' + str(obj_id)
if subobj1:
endpoints = endpoints + '/' + str(subobj1)
if subobj1_id:
endpoints = endpoints + '/' + str(subobj1_id)
if subobj2:
endpoints = endpoints + '/' + str(subobj2)
if subobj2_id:
endpoints = endpoints + '/' + str(subobj2_id)
if domain == 'restapieu':
base_url = base_url.replace("surveygizmo.com", "surveygizmo.eu")
url = base_url + endpoints + '/?'
url = url + api_token
params_list = []
for key in params.keys():
s = str(key) + '=' + str(params[key])
params_list.append(s)
param_str = '&'.join(params_list)
param_str = '&' + param_str
url = url + param_str
return url