Skip to content

Instantly share code, notes, and snippets.

@mootoh
Created March 8, 2024 03:38
Show Gist options
  • Save mootoh/1ff3d184b85bea6bcf5d2a6ec275a9d1 to your computer and use it in GitHub Desktop.
Save mootoh/1ff3d184b85bea6bcf5d2a6ec275a9d1 to your computer and use it in GitHub Desktop.
import requests
import urllib.parse
import json
from datetime import datetime
# Load configuration
with open('config.json', 'r') as f:
config = json.load(f)
# Confluence API endpoint URLs
base_url = config['confluence_base_url']
auth = (config['confluence_username'], config['confluence_api_token'])
space_id = config['space_id']
label_id = config['latest_label_id']
body_format="atlas_doc_format"
url = f"{base_url}/wiki/api/v2/labels/{label_id}/pages?spaceId={space_id}&limit=1&sort=-created-date&body-format={body_format}"
#print(url)
# Get the previous page content
response = requests.get(url, auth=auth)
response.raise_for_status()
prev_page = response.json()['results'][0]
parentId = prev_page['parentId']
# Create a new page
date = datetime.now()
formatted_date = date.strftime("%d %b %Y")
new_page_title = f"Copied page - {formatted_date}"
new_page_content = prev_page['body']['atlas_doc_format']['value']
#print (new_page_content)
new_page_data = {
"spaceId": space_id,
"title": new_page_title,
"parentId": parentId,
"body": {
"representation": body_format,
"value": new_page_content,
}
}
post_response = requests.post(f"{base_url}/wiki/api/v2/pages", auth=auth, json=new_page_data)
post_response.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment