Skip to content

Instantly share code, notes, and snippets.

@codingisacopingstrategy
Created July 23, 2014 09:25
Show Gist options
  • Save codingisacopingstrategy/e4c87cde0138b8c103a2 to your computer and use it in GitHub Desktop.
Save codingisacopingstrategy/e4c87cde0138b8c103a2 to your computer and use it in GitHub Desktop.
Delete all the pads on an Etherpad instance that have 0 revisions (they often appear when you type the wrong url by hand)
# -*- coding: utf-8 -*-
import sys
import json
import urllib
import urllib2
"""
Delete all the pads on an Etherpad instance that have 0 revisions (they often appear when you type the wrong url by hand)
"""
class EtherpadLiteClient:
# Code taken from http://github.com/devjones/PyEtherpadLite
"""Client to talk to EtherpadLite API."""
API_VERSION = "1.2.1" # This I had to make higher than in from pyetherpadlite,
# Otherwise listAllPads does not work
CODE_OK = 0
CODE_INVALID_PARAMETERS = 1
CODE_INTERNAL_ERROR = 2
CODE_INVALID_FUNCTION = 3
CODE_INVALID_API_KEY = 4
TIMEOUT = 20
apiKey = "APIKEYHERE"
baseUrl = "http://your-url/api"
def __init__(self, apiKey=None, baseUrl=None):
if apiKey:
self.apiKey = apiKey
if baseUrl:
self.baseUrl = baseUrl
def call(self, function, arguments=None):
"""Create a dictionary of all parameters"""
url = '%s/%s/%s' % (self.baseUrl, self.API_VERSION, function)
params = arguments or {}
params.update({'apikey': self.apiKey})
data = urllib.urlencode(params, True)
try:
opener = urllib2.build_opener()
request = urllib2.Request(url=url, data=data)
response = opener.open(request, timeout=self.TIMEOUT)
result = response.read()
response.close()
except urllib2.HTTPError:
raise
result = json.loads(result)
if result is None:
raise ValueError("JSON response could not be decoded")
return self.handleResult(result)
def handleResult(self, result):
"""Handle API call result"""
if 'code' not in result:
raise Exception("API response has no code")
if 'message' not in result:
raise Exception("API response has no message")
if 'data' not in result:
result['data'] = None
if result['code'] == self.CODE_OK:
return result['data']
elif result['code'] == self.CODE_INVALID_PARAMETERS or result['code'] == self.CODE_INVALID_API_KEY:
raise ValueError(result['message'])
elif result['code'] == self.CODE_INTERNAL_ERROR:
raise Exception(result['message'])
elif result['code'] == self.CODE_INVALID_FUNCTION:
raise Exception(result['message'])
else:
raise Exception("An unexpected error occurred whilst handling the response")
def listAllPads(self): # This function is not in pyetherpadlite.
"""creates a new group"""
return self.call("listAllPads")
def getRevisionsCount(self, padID):
"""returns the number of revisions of this pad"""
return self.call("getRevisionsCount", {
"padID": padID
})
def deletePad(self, padID):
"""deletes a pad"""
return self.call("deletePad", {
"padID": padID
})
def getText(self, padID, rev=None):
"""returns the text of a pad"""
params = {"padID": padID}
if rev is not None:
params['rev'] = rev
return self.call("getText", params)
if __name__ == '__main__':
dry = True # Change to False to actually delete the pads
ep = EtherpadLiteClient()
padIDs = ep.listAllPads()['padIDs']
padIDs = filter(None, padIDs)
i = 0
for padID in padIDs:
try:
res = ep.getRevisionsCount(padID)
except Exception as e:
print "%s gives error %s, skipped" % (padID, e)
continue
if res['revisions'] == 0:
print "deleting %s" % padID
if dry:
print "with text:"
print ep.getText(padID)
else:
try:
ep.deletePad(padID)
i += 1
except Exception as e:
print "Deletion failed, maybe the server has overheated?"
print "Uptil now, weve deleted %s pads" % i
print "Stopping for now, you can try to run the script again after checking the server status."
sys.exit()
print "from %s pads deleted %s" % (len(padIDs), i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment