102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
import os
|
|
import urllib.parse
|
|
from etherpad_lite import EtherpadLiteClient, EtherpadException
|
|
import logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
#SERVER_URL = settings.ETHERPAD_CLIENT["exturl"]
|
|
logger.info("loading api.py")
|
|
class EtherpadLiteLazyAPI():
|
|
"""This is a small wrapper for EtherpadLiteClient
|
|
url is the url with port the client should connect to.
|
|
keyfile is the file path to the APIKEY
|
|
"""
|
|
def __init__(self,url="http://etherpad", exturl="https://bot.2020.fet.at", keyfile=None, groupmapper="mygroup"):
|
|
self.url=url
|
|
self.keyfile=keyfile
|
|
self.groupmapper=groupmapper
|
|
self.epc=None
|
|
self.group=None
|
|
self.connect()
|
|
self.exturl=exturl
|
|
#self.epc, self.group = self.get_ep_client()
|
|
|
|
@property
|
|
def isConnected(self):
|
|
if self.epc and self.group: return True
|
|
return False
|
|
|
|
def connect(self):
|
|
""" Try to establish the connection.
|
|
This sets the internal variables self.epc = Etherpad Client
|
|
and self.group which represents the group this instance is associated with
|
|
"""
|
|
if self.epc and self.group: return True # if connection exists return
|
|
logger.info("connecting")
|
|
try:
|
|
with open(os.path.abspath(self.keyfile), "r") as f:
|
|
apikey = f.read().rstrip()
|
|
epc = EtherpadLiteClient(
|
|
base_params={'apikey': apikey, },
|
|
base_url=urllib.parse.urljoin(self.url, "api"),
|
|
api_version='1.2.14',
|
|
)
|
|
group = epc.createGroupIfNotExistsFor(groupMapper=self.groupmapper)
|
|
except Exception as e:
|
|
logger.error(f"Can't get connection to Etherpad Server. Error: {e}")
|
|
return None
|
|
self.epc=epc
|
|
self.group= group
|
|
return True
|
|
|
|
|
|
def padExists(self, padID=None):
|
|
if not padID: return False # padID needed
|
|
if not self.connect(): return None
|
|
try:
|
|
lists = self.epc.listAllPads()
|
|
except Exception as e:
|
|
raise e
|
|
return None
|
|
string = self.group["groupID"] + "$" + str(padID)
|
|
if string in lists["padIDs"]: return True
|
|
return False
|
|
|
|
def createPadifNotExists(self,padID):
|
|
if not self.connect(): return None
|
|
# Pad doesn't exist
|
|
if not self.padExists(padID=padID):
|
|
try:
|
|
self.epc.createGroupPad(groupID=self.group["groupID"], padName=padID, text="helloworld")
|
|
except EtherpadException as e:
|
|
logger.error("Can't create Pad '{}'. EtherpadException: {}".format(padID, e))
|
|
return None
|
|
except Exception as e:
|
|
raise e
|
|
return None
|
|
return padID
|
|
|
|
def getPadHTML(self,padID):
|
|
if not self.connect():
|
|
return None
|
|
try:
|
|
text = self.epc.getHTML(padID=self.group["groupID"] + "$" + padID)["html"]
|
|
except EtherpadException as e:
|
|
logger.error("Can't get HTML from padID '{}'. EtherpadException: {}".format(padID, e))
|
|
return None
|
|
except Exception as e:
|
|
raise e
|
|
return None
|
|
return text
|
|
|
|
|
|
def setPadHTML(self,padID, html):
|
|
if not self.connect(): return None
|
|
self.epc.setHTML(padID=self.group["groupID"] + "$" + padID, html=html)
|
|
return html
|
|
|
|
def getPadLink(self,padID):
|
|
if padID is None: return "#"
|
|
if not self.connect(): return "#"
|
|
return urllib.parse.urljoin(self.exturl, 'p/' + self.group["groupID"] + '$' + str(padID))
|