script using etherpad api
parent
77d25461fa
commit
dee2e0f786
@ -0,0 +1,2 @@
|
||||
etherpad-api/api.key
|
||||
Jinja-prototype/index.html
|
@ -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)
|
Loading…
Reference in New Issue