forked from wozhub/lime-py-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlimesurvey.py
executable file
·233 lines (194 loc) · 9.07 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
try:
import urllib.request as urllib2
except ImportError:
import urllib2
from csv import DictReader
from time import sleep
class Api:
def __init__(self, url, user, pw):
self.url = url
self._user = user
self._password = pw
data = """{ "id": 1,
"method": "get_session_key",
"params": { "username": "%s",
"password": "%s" } } """ % (user, pw)
self.session_key = self._get_json(data)['result']
def _get_json(self, data):
req = urllib2.Request(url=self.url, data=data)
req.add_header('content-type', 'application/json')
req.add_header('connection', 'Keep-Alive')
try:
f = urllib2.urlopen(req)
response = f.read()
return json.loads(response)
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._get_json(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._get_json(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._get_json(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._get_json(data)['result']
def list_surveys(self):
json_list_surveys = self._list_surveys()
surveys = []
for e in json_list_surveys:
survey = e['sid'], e['surveyls_title']
surveys.append(survey)
return surveys
def _list_surveys(self):
"""Devuelve el JSON ENTERO"""
data = """{ "id": 1,
"method": "list_surveys",
"params": { "sSessionKey": "%s" } }""" % (self.session_key)
return self._get_json(data)['result']
def activate_survey(self, sid):
data = """{ "id": 1,
"method": "activate_survey",
"params": { "sSessionKey": "%s",
"SurveyID": %s } }""" % (self.session_key, sid)
return self._get_json(data)['result']
def import_survey(self, datos, titulo, sid, tipo='lss'):
data = """{ "id": 1,
"method": "import_survey",
"params": { "sSessionKey": "%s",
"sImportData": "%s",
"sImportDataType": "%s",
"sNewSurveyName": "%s",
"DestSurveyID": %d } }""" \
% (self.session_key, datos, tipo, titulo, sid)
return self._get_json(data)['result']
def release_session_key(self):
data = """ { "method": "release_session_key",
"params": { "sSessionKey" : "%s"},
"id":1}' }""" % (self.session_key)
return self._get_json(data)['result']
def export_responses(self, sid):
data = """ { "id" : 1,
"method":"export_responses",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"sDocumentType": "json",
"sLanguageCode": "ca",
"sCompletionStatus":"all",
"sHeadingType": "code",
"sResponseType": "long"
} } """ % (self.session_key, sid)
return self._get_json(data)['result']
def export_responses_by_token(self, sid, token, language_code):
data = """ { "id" : 1,
"method":"export_responses_by_token",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"sDocumentType": "json",
"sToken": "%s",
"$sLanguageCode": "%s",
"sCompletationStatus": "all",
"sHeadingType": "code",
"sResponseType": "long"
} } """ % (self.session_key, sid, token, language_code)
return self._get_json(data)['result']
def _add_response(self, sid, response_data):
data = """ { "id": 1,
"method":"add_response",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"aResponseData": %s }
} """ % (self.session_key, sid, response_data)
return self._get_json(data)['result']
'''Update Response '''
def update_response(self, sid, response_data):
data = """ { "id": 1,
"method":"update_response",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"aResponseData": %s }
} """ % (self.session_key, sid, response_data)
return self._get_json(data)['result']
def impor_from_file(self, sid, file):
"""Esto no funciona!"""
with open(file) as csv:
datos = []
for line in csv.readlines():
datos.append(line.rstrip().split('\t'))
columns = datos[1]
for d in datos[2:]:
r = dict(zip(columns, 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._get_json(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._get_json(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