2022-02-19 00:19:31 +00:00
|
|
|
import base64
|
2022-02-18 23:47:42 +00:00
|
|
|
import json
|
2022-02-18 21:22:00 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
def get_auth_header(token_id, token_secret):
|
|
|
|
return {"Authorization": "Token {}:{}".format(token_id, token_secret)}
|
|
|
|
|
|
|
|
|
2022-02-24 18:23:54 +00:00
|
|
|
def get_page_html(wiki_url, token_id, token_secret, page_id):
|
2022-02-18 21:22:00 +00:00
|
|
|
headers = {}
|
|
|
|
headers.update(get_auth_header(token_id, token_secret))
|
|
|
|
|
|
|
|
r = requests.get(wiki_url + "/api/pages/{}/export/html".format(page_id), headers=headers)
|
|
|
|
|
2022-02-18 21:48:47 +00:00
|
|
|
return r.text
|
2022-02-18 23:47:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_attachments(wiki_url, token_id, token_secret):
|
|
|
|
headers = {}
|
|
|
|
headers.update(get_auth_header(token_id, token_secret))
|
|
|
|
|
2022-02-24 18:23:54 +00:00
|
|
|
r = requests.get(wiki_url + "/api/attachments", headers=headers)
|
2022-02-18 23:47:42 +00:00
|
|
|
|
|
|
|
attachment_data = json.loads(r.text)['data']
|
|
|
|
|
|
|
|
return attachment_data
|
|
|
|
|
|
|
|
|
2022-02-24 18:23:54 +00:00
|
|
|
def get_attachment_id(wiki_url, token_id, token_secret, attachment_name, page_id):
|
2022-02-18 23:47:42 +00:00
|
|
|
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
|
2022-02-19 00:19:31 +00:00
|
|
|
|
|
|
|
|
2022-02-25 20:42:59 +00:00
|
|
|
def post_attachment(wiki_url, token_id, token_secret, filename, filedata, page_id):
|
|
|
|
headers = {}
|
|
|
|
headers.update(get_auth_header(token_id, token_secret))
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data["name"] = filename
|
|
|
|
data["uploaded_to"] = page_id
|
|
|
|
|
|
|
|
files = {}
|
|
|
|
files["file"] = filedata
|
|
|
|
|
|
|
|
requests.post(wiki_url + "/api/attachments", data=data, files=files, headers=headers)
|
|
|
|
|
|
|
|
|
2022-02-24 18:23:54 +00:00
|
|
|
def get_attachment(wiki_url, token_id, token_secret, attachment_id):
|
2022-02-19 00:19:31 +00:00
|
|
|
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
|
2022-02-19 13:49:48 +00:00
|
|
|
|
|
|
|
|
2022-09-05 23:05:54 +01:00
|
|
|
def update_attachment(wiki_url, token_id, token_secret, attachment_id, file_name, file_data, page_id):
|
|
|
|
headers = {}
|
|
|
|
headers.update(get_auth_header(token_id, token_secret))
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
# Workaround for updating files: https://github.com/BookStackApp/BookStack/issues/3666
|
|
|
|
data["_method"] = "PUT"
|
|
|
|
data["name"] = file_name
|
|
|
|
data["uploaded_to"] = page_id
|
|
|
|
|
|
|
|
files = {}
|
|
|
|
files["file"] = file_data
|
|
|
|
|
|
|
|
requests.post(wiki_url + "/api/attachments/{}".format(attachment_id), headers=headers, data=data, files=files)
|
|
|
|
|
|
|
|
|
2022-02-25 20:42:59 +00:00
|
|
|
def delete_attachment(wiki_url, token_id, token_secret, attachment_id):
|
|
|
|
headers = {}
|
|
|
|
headers.update(get_auth_header(token_id, token_secret))
|
|
|
|
|
|
|
|
requests.delete(wiki_url + "/api/attachments/{}".format(attachment_id), headers=headers)
|
|
|
|
|
|
|
|
|
2022-02-26 14:43:50 +00:00
|
|
|
def update_page(wiki_url, token_id, token_secret, page_id, markdown=None, html=None):
|
2022-02-19 13:49:48 +00:00
|
|
|
headers = {}
|
|
|
|
headers.update(get_auth_header(token_id, token_secret))
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['markdown'] = markdown
|
2022-02-26 14:43:50 +00:00
|
|
|
data['html'] = html
|
2022-02-19 13:49:48 +00:00
|
|
|
|
|
|
|
requests.put(wiki_url + "/api/pages/{}".format(page_id), data=data, headers=headers)
|