whu_migration_scripts/boostack_create_pages.py

90 lines
2.1 KiB
Python
Raw Normal View History

2026-04-16 10:04:58 +02:00
import requests
import sys
# ==========================
# CONFIGURATION
# ==========================
BOOKSTACK_API_URL = "https://wiki-warmachine.ungol.fr/api"
API_TOKEN_ID = "VOTRE_TOKEN_ID"
API_TOKEN_SECRET = "VOTRE_TOKEN_SECRET"
PAGES_FILE = "pages.txt"
DEFAULT_CONTENT = """
<p><em>Page restaurée automatiquement depuis l'ancien wiki.</em></p>
"""
# ==========================
# HEADERS
# ==========================
HEADERS = {
"Authorization": f"Token {API_TOKEN_ID}:{API_TOKEN_SECRET}",
"Content-Type": "application/json"
}
# ==========================
# FUNCTIONS
# ==========================
def create_page(title, chapter_id, content=DEFAULT_CONTENT):
"""Create a page in BookStack"""
url = f"{BOOKSTACK_API_URL}/pages"
payload = {
"name": title,
"html": content,
"chapter_id": int(chapter_id)
}
response = requests.post(url, headers=HEADERS, json=payload)
if response.status_code == 200:
page_id = response.json().get("id")
print(f"[OK] Page créée : '{title}' (ID {page_id})")
return page_id
else:
print(f"[ERREUR] Impossible de créer '{title}'")
print(response.status_code, response.text)
return None
def load_pages(filename):
"""Load pages list from file"""
pages = []
with open(filename, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
try:
title, chapter_id = line.split("|")
pages.append((title.strip(), chapter_id.strip()))
except ValueError:
print(f"[IGNORÉ] Ligne invalide : {line}")
return pages
# ==========================
# MAIN
# ==========================
def main():
pages = load_pages(PAGES_FILE)
if not pages:
print("Aucune page à créer.")
sys.exit(0)
print(f"{len(pages)} pages à créer...\n")
for title, chapter_id in pages:
create_page(title, chapter_id)
print("\nImport terminé.")
if __name__ == "__main__":
main()