From dee2e0f786f84e7c8740eba917932191f6306d72 Mon Sep 17 00:00:00 2001 From: Castro0o Date: Tue, 5 Mar 2019 14:35:01 +0100 Subject: [PATCH] script using etherpad api --- .gitignore | 2 + etherpad-api/etherpad-requests.py | 78 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 etherpad-api/etherpad-requests.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ae96af --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +etherpad-api/api.key +Jinja-prototype/index.html \ No newline at end of file diff --git a/etherpad-api/etherpad-requests.py b/etherpad-api/etherpad-requests.py new file mode 100644 index 0000000..49d3690 --- /dev/null +++ b/etherpad-api/etherpad-requests.py @@ -0,0 +1,78 @@ +import urllib.request +import urllib.parse +import json +from datetime import datetime + + +# REQUIRES: Etherpad API KEY stored in api.key + +with open('api.key', 'r') as apikey_f: + apikey = apikey_f.read() + +pad_urlbase = "https://pad.xpub.nl/api/1/" # xpub pad +pad_urlmethods = {"get": "getText", + "set": "setText", + "authors": "listAuthorsOfPad"} + +# see more methods in: https://etherpad.org/doc/v1.5.7/#index_http_api + + +def createurl(urlbase, urlmethods, method, pad, key, text=''): + # creates well formated urls + # to query/edit the etherpad + url_vars = {'apikey': key, 'padID': pad} + if len(text) > 0: # if no text is input + url_vars['text'] = text + url_vars_dict = urllib.parse.urlencode(url_vars) + url = "{base}{method}?{vars}".format(base=urlbase, + method=urlmethods[method], + vars=url_vars_dict) + return url + + +def padrequest(url): + # upon being invoked opens the url + # and loads the JSON response + # returning the text + request = urllib.request.urlopen(url) + request_read = request.read().decode('utf-8') + request_dict = json.loads(request_read) # response to JSON dictionary + return request_dict + + +# GET TEXT FROM PAD +# from foo pad https://pad.xpub.nl/p/foo +# by creating url: +url = createurl(urlbase=pad_urlbase, + urlmethods=pad_urlmethods, + method='get', + pad='foo', + key=apikey) +response = padrequest(url) # requesting url and get response + +print('----> get request', '\n', + url, '\n', + response['data']['text']) + + + + +# ADD TEXT TO THE PAD +# to foo pad (https://pad.xpub.nl/p/foo) + +now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # using current time +new_text = "this is now: " + now +# added old_text + new_text to +old_new_text = response['data']['text'] + '\n' + new_text + +# by creating url: w/ set method and and text +url = createurl(urlbase=pad_urlbase, + urlmethods=pad_urlmethods, + method='set', + pad='foo', + key=apikey, + text=old_new_text) + +# create a request +response = padrequest(url) +print('----> set URL', '\n', url)