-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_pages.py
More file actions
47 lines (37 loc) · 1.24 KB
/
export_pages.py
File metadata and controls
47 lines (37 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
import os
import sys
from dotenv import load_dotenv
from bookstack_client import BookStackClient
def main():
load_dotenv(".env")
out_dir = 'exports'
token_id = os.getenv("BOOKSTACK_TOKEN_ID")
token_secret = os.getenv("BOOKSTACK_TOKEN_SECRET")
base_url = os.getenv("BOOKSTACK_URL")
if not token_id or not token_secret or not base_url:
print("Missing BOOKSTACK_TOKEN_ID or BOOKSTACK_TOKEN_SECRET or BOOKSTACK_URL", file=sys.stderr)
return 1
client = BookStackClient(base_url, token_id, token_secret)
os.makedirs(out_dir, exist_ok=True)
try:
payload = client.list_pages()
except RuntimeError as e:
print(str(e), file=sys.stderr)
return 1
for item in payload.get("data", []):
page_id = item.get("id")
if page_id is None:
continue
try:
pdf = client.export_page_pdf(page_id)
except RuntimeError as e:
print(str(e), file=sys.stderr)
continue
out_file = os.path.join(out_dir, f"page-{page_id}.pdf")
with open(out_file, "wb") as f:
f.write(pdf)
print(f"Saved {out_file}")
return 0
if __name__ == "__main__":
raise SystemExit(main())