bookstack/WikiUtils.py

59 lines
1.7 KiB
Python

import base64
import json
import requests
def get_auth_header(token_id, token_secret):
return {"Authorization": "Token {}:{}".format(token_id, token_secret)}
def get_page_export_html(page_id, wiki_url, token_id, token_secret):
headers = {}
headers.update(get_auth_header(token_id, token_secret))
r = requests.get(wiki_url + "/api/pages/{}/export/html".format(page_id), headers=headers)
return r.text
def get_attachments(wiki_url, token_id, token_secret):
headers = {}
headers.update(get_auth_header(token_id, token_secret))
r = requests.get(wiki_url + "/api/attachments/", headers=headers)
attachment_data = json.loads(r.text)['data']
return attachment_data
def get_attachment_id(attachment_name, page_id, wiki_url, token_id, token_secret):
attachment_data = get_attachments(wiki_url, token_id, token_secret)
for attachment in attachment_data:
if attachment['name'] == attachment_name and attachment['uploaded_to'] == page_id:
return attachment['id']
return None
def get_attachment_contents(attachment_id, wiki_url, token_id, token_secret):
headers = {}
headers.update(get_auth_header(token_id, token_secret))
r = requests.get(wiki_url + "/api/attachments/{}".format(attachment_id), headers=headers)
attachment_data_encoded = json.loads(r.text)['content']
attachment_data_decoded = base64.b64decode(attachment_data_encoded)
return attachment_data_decoded
def update_page(page_id, markdown, wiki_url, token_id, token_secret):
headers = {}
headers.update(get_auth_header(token_id, token_secret))
data = {}
data['markdown'] = markdown
requests.put(wiki_url + "/api/pages/{}".format(page_id), data=data, headers=headers)