forked from fabiore/lime-py-api
-
Notifications
You must be signed in to change notification settings - Fork 6
/
limesurvey.py
executable file
·218 lines (189 loc) · 9.18 KB
/
limesurvey.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from base64 import b64decode
import json
import sys
import requests
from time import sleep
class Api:
def __init__(self, url, user, key):
self.url = url
self._user = user
self._password = key
data = """{ "id": 1,
"method": "get_session_key",
"params": { "username": "%s",
"password": "%s" } } """ % (user, key)
self.session_key = self._getJSON(data)['result']
# Standard post request
def _getJSON(self, data):
headers = {'content-type': 'application/json',
'connection': 'Keep-Alive'}
try:
req = requests.post(self.url, data=data, headers=headers)
return(req.json())
except:
e = sys.exc_info()[0]
print ("<p>Error: %s</p>" % e)
def delete_survey(self, sid):
data = """{ "id": 1,
"method": "delete_survey",
"params": { "sSessionKey": "%s",
"iSurveyID": %s } }""" % (self.session_key,
sid)
return self._getJSON(data)['result']
def set_survey_property(self, sid, prop, value):
data = """{ "id": 1,
"method": "set_survey_properties",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"aSurveySettings": { "%s": "%s" }
} }""" % (self.session_key, sid, prop, value)
return self._getJSON(data)['result']
def get_survey_properties(self, sid, settings=None):
if settings is None:
settings = """ [
"sid","savetimings","allowprev","tokenanswerspersistence",
"showgroupinfo","showwelcome","owner_id","template","printanswers",
"assessments","shownoanswer","showprogress","admin","language",
"ipaddr","usecaptcha","showqnumcode","allowjumps","active",
"additional_languages","refurl","usetokens","bouncetime",
"navigationdelay","expires","datestamp","datecreated",
"bounce_email","bounceprocessing","nokeyboard","startdate",
"usecookie","publicstatistics","attributedescriptions",
"bounceaccounttype","alloweditaftercompletion","adminemail",
"allowregister","publicgraphs","emailresponseto",
"bounceaccounthost","googleanalyticsstyle","anonymized",
"allowsave","listpublic","emailnotificationto","bounceaccountpass",
"googleanalyticsapikey","faxto","autonumber_start","htmlemail",
"tokenlength","bounceaccountencryption","format","autoredirect",
"sendconfirmation","showxquestions","bounceaccountuser" ] """
data = """{ "id": 1,
"method": "get_survey_properties",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"aSurveySettings": %s
} }""" % (self.session_key, sid, settings)
return self._getJSON(data)['result']
def get_summary(self, sid):
data = """{ "id": 1,
"method": "get_summary",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"sStatname": "all" } }""" % (self.session_key,
sid)
return self._getJSON(data)['result']
def activate_survey(self, sid):
data = """{ "id": 1,
"method": "activate_survey",
"params": { "sSessionKey": "%s",
"SurveyID": %s } }""" % (self.session_key, sid)
return self._getJSON(data)['result']
def import_survey(self, idata, title, sid, type='lss'):
data = """{ "id": 1,
"method": "import_survey",
"params": { "sSessionKey": "%s",
"sImportData": "%s",
"sImportDataType": "%s",
"sNewSurveyName": "%s",
"DestSurveyID": %d } }""" \
% (self.session_key, idata, type, title, sid)
return self._getJSON(data)['result']
def release_session_key(self):
data = """ { "method": "release_session_key",
"params": { "sSessionKey" : "%s"},
"id":1}' }""" % (self.session_key)
return self._getJSON(data)['result']
def export_responses(self, sid, status='all', heading='code', response='short', fields=''):
data = """ { "id" : 1,
"method":"export_responses",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"sDocumentType": "json",
"sLanguageCode": "en",
"sCompletionStatus": "%s",
"sHeadingType": "%s",
"sResponseType": "%s",
"aFields": "%s"
} } """ % (self.session_key, sid, status, heading, response, fields)
print(data)
out = b64decode(self._getJSON(data)['result']).decode('utf-8')
return json.loads(out)
def export_responses_by_token(self, sid, token):
data = """ { "id" : 1,
"method":"export_responses_by_token",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"sDocumentType": "json",
"sToken": "%s",
"$sLanguageCode": "en",
"sCompletationStatus": "all",
"sHeadingType": "code",
"sResponseType": "short"
} } """ % (self.session_key, sid, token)
return self._getJSON(data)['result']
def _add_response(self, sid, rdata):
data = """ { "id": 1,
"method":"add_response",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"aResponseData": %s }
} """ % (self.session_key, sid, rdata)
return self._getJSON(data)['result']
def importar_desde_archivo(self, sid, archivo):
"""Esto no funciona!"""
with open(archivo) as csv:
datos = []
for linea in csv.readlines():
datos.append(linea.rstrip().split('\t'))
columnas = datos[1]
for d in datos[2:]:
r = dict(zip(columnas, d))
r['id'] = ""
self._add_response(sid, json.dumps(r))
sleep(1)
def _list_groups(self, sid):
data = """ { "method":"list_groups",
"params": { "sSessionKey": "%s",
"iSurveyID": %s },
"id": 1 } """ % (self.session_key, sid)
return self._getJSON(data)['result']
def list_groups(self, sid):
json_list_groups = self._list_groups(sid)
groups = []
for g in json_list_groups:
group = g['id']['gid'], g['group_name']
groups.append(group)
return groups
def _list_questions(self, sid, gid):
data = """ { "method":"list_questions",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"iGroupID": %s },
"id": 1 } """ % (self.session_key, sid, gid)
return self._getJSON(data)['result']
def list_questions(self, sid, gid):
json_list_questions = self._list_questions(sid, gid)
questions = []
for q in json_list_questions:
question = q['id']['qid'], q['question']
questions.append(question)
return questions
def list_surveys(self, sUser=''):
data = """ { "id" : 1,
"method":"list_surveys",
"params": { "sSessionKey": "%s", "sUsername": "%s"}
} """ % (self.session_key, sUser)
return self._getJSON(data)['result']
def list_participants(self, sid, iStart=0, iLimit=1000000, bUnused='true', aAttributes='true', aConditions='array()'):
data = """ { "id" : 1,
"method":"list_participants",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"iStart": %s,
"iLimit": %s,
"bUnused": "%s",
"aAttributes": "%s",
"aConditions": "%s"
} } """ % (self.session_key, sid, iStart, iLimit, bUnused, aAttributes, aConditions)
return self._getJSON(data)['result']