-
Notifications
You must be signed in to change notification settings - Fork 9
/
limesurvey.py
executable file
·204 lines (168 loc) · 7.79 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2
import json
import sys
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._obtenerJson(data)['result']
def _obtenerJson(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._obtenerJson(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._obtenerJson(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._obtenerJson(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._obtenerJson(data)['result']
def list_surveys(self):
json_list_surveys = self._list_surveys()
encuestas = []
for e in json_list_surveys:
encuesta = e['sid'], e['surveyls_title']
# Me quedo con el SID y el Titulo
encuestas.append(encuesta)
return encuestas
def _list_surveys(self):
"""Devuelve el JSON ENTERO"""
data = """{ "id": 1,
"method": "list_surveys",
"params": { "sSessionKey": "%s" } }""" % (self.session_key)
return self._obtenerJson(data)['result']
def activate_survey(self, sid):
data = """{ "id": 1,
"method": "activate_survey",
"params": { "sSessionKey": "%s",
"SurveyID": %s } }""" % (self.session_key, sid)
return self._obtenerJson(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._obtenerJson(data)['result']
def release_session_key(self):
data = """ { "method": "release_session_key",
"params": { "sSessionKey" : "%s"},
"id":1}' }""" % (self.session_key)
return self._obtenerJson(data)['result']
def export_responses(self, sid):
data = """ { "id" : 1,
"method":"export_responses",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"sDocumentType": "csv",
"sHeadingType": "full",
"sResponseType": "long"
} } """ % (self.session_key, sid)
return self._obtenerJson(data)['result']
def _add_response(self, sid, datos):
data = """ { "id": 1,
"method":"add_response",
"params": { "sSessionKey": "%s",
"iSurveyID": %s,
"aResponseData": %s }
} """ % (self.session_key, sid, datos)
return self._obtenerJson(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._obtenerJson(data)['result']
def list_groups(self, sid):
json_list_groups = self._list_groups(sid)
grupos = []
for g in json_list_groups:
grupo = g['id']['gid'], g['group_name']
grupos.append(grupo)
return grupos
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._obtenerJson(data)['result']
def list_questions(self, sid, gid):
json_list_questions = self._list_questions(sid, gid)
preguntas = []
for q in json_list_questions:
pregunta = q['id']['qid'], q['question']
preguntas.append(pregunta)
return preguntas