Skip to content

Instantly share code, notes, and snippets.

@guinslym
Created September 7, 2024 17:15
Show Gist options
  • Save guinslym/a213e1d97dcbb267c253253d9918d084 to your computer and use it in GitHub Desktop.
Save guinslym/a213e1d97dcbb267c253253d9918d084 to your computer and use it in GitHub Desktop.
#Python 3
import urllib.request
import urllib.error
import json
import sys
# Define functions to make requests to the remote control API
def get_session_key():
breakpoint()
data = json.dumps({
"method": "get_session_key",
"params": ["myusername", "mypassword"],
"id": 1
}).encode("utf-8")
req = urllib.request.Request(url='https://survey.scholarsportal.info/index.php/admin/remotecontrol', data=data)
req.add_header('Content-Type', 'application/json')
req.add_header('Connection', 'Keep-Alive')
try:
with urllib.request.urlopen(req) as f:
myreturn = f.read().decode('utf-8')
j = json.loads(myreturn)
return j['result']
except urllib.error.URLError as e:
print("Error while connecting to Limesurvey")
print(f"<p>Error: {e}</p>")
return None
def get_question_properties(skey, QuestionID):
data = json.dumps({
"method": "get_question_properties",
"params": [skey, QuestionID, ["gid", "type", "help", "language", "sid", "question_order", "question", "subquestions"]],
"id": 1
}).encode("utf-8")
req = urllib.request.Request(url='https://myurl/index.php/admin/remotecontrol', data=data)
req.add_header('Content-Type', 'application/json')
req.add_header('Connection', 'Keep-Alive')
try:
with urllib.request.urlopen(req) as f:
myreturn = f.read().decode('utf-8')
j = json.loads(myreturn)
return j['result']
except urllib.error.URLError as e:
print(f"<p>Error: {e}</p>")
return None
def release_session_key(relkey):
data = json.dumps({
"method": "release_session_key",
"params": [relkey],
"id": 1
}).encode("utf-8")
req = urllib.request.Request(url='https://myurl/index.php/admin/remotecontrol', data=data)
req.add_header('Content-Type', 'application/json')
req.add_header('Connection', 'Keep-Alive')
try:
with urllib.request.urlopen(req) as f:
myreturn = f.read().decode('utf-8')
j = json.loads(myreturn)
return j['result']
except urllib.error.URLError as e:
print(f"<p>Error: {e}</p>")
return None
def export_responses2(skey, sid):
data = json.dumps({
"method": "export_responses",
"params": [skey, sid, "csv", "de", "full"],
"id": 1
}).encode("utf-8")
req = urllib.request.Request(url='https://myurl/index.php/admin/remotecontrol', data=data)
req.add_header('Content-Type', 'application/json')
req.add_header('Connection', 'Keep-Alive')
try:
with urllib.request.urlopen(req) as f:
myreturn = f.read().decode('utf-8')
j = json.loads(myreturn)
return j['result']
except urllib.error.URLError as e:
print(f"<p>Error: {e}</p>")
return None
# Example usage of the functions
mykey = get_session_key()
if mykey:
print(mykey)
print(export_responses2(mykey, '566237'))
print(get_question_properties(mykey, '574'))
print(release_session_key(mykey))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment