import requests
from requests.auth import HTTPBasicAuth
import re
# Placeholder variables for user inputs
ARTICLE_ID =
HELPJUICE_API_KEY =
XWIKI_USERNAME =
XWIKI_PASSWORD =
# Constants
XWIKI_URL =
XWIKI_SPACE = 'Main'
# Constants
XWIKI_URL =
XWIKI_SPACE = 'Main'
def get_helpjuice_article(article_id, api_key):
url = f"https://*.helpjuice.com/api/v3/articles/{article_id}"
headers = {
'Authorization': api_key
}
response = requests.get(url, headers=headers)
# Print the raw response text for debugging
print("Response Text:", response.text)
try:
response.raise_for_status()
article_data = response.json()
# Extracting title and content correctly based on the structure
title = article_data['article']['name']
content = article_data['article']['answer']['body']
return title, content
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
return None, None
except requests.exceptions.RequestException as req_err:
print(f"Request error occurred: {req_err}")
return None, None
except ValueError as json_err:
print(f"JSON decode error: {json_err}")
return None, None
def get_form_token(session, xwiki_url):
# Perform a GET request to obtain the form token
response = session.get(f"{xwiki_url}/bin/view/Main/")
# Debugging: Print all response headers
print("Response Headers:", response.headers)
# Retrieve the form token from headers
form_token = response.headers.get('XWiki-Form-Token')
if not form_token:
raise Exception("Failed to obtain form token")
return form_token
def html_to_xwiki_syntax(html_content):
xwiki_content = html_content
xwiki_content = re.sub(r'
(.*?)
', r'= \1 =', xwiki_content)
xwiki_content = re.sub(r'(.*?)
', r'== \1 ==', xwiki_content)
xwiki_content = re.sub(r'(.*?)
', r'\1\n', xwiki_content)
xwiki_content = re.sub(r'', r'\1', xwiki_content, flags=re.DOTALL)
xwiki_content = re.sub(r'(.*?)', r'* \1\n', xwiki_content)
return xwiki_content
def migrate_to_xwiki(xwiki_url, username, password, space, page_name, content):
session = requests.Session()
session.auth = HTTPBasicAuth(username, password)
form_token = get_form_token(session, xwiki_url)
session.headers.update({
'XWiki-Form-Token': form_token,
'Origin': xwiki_url,
'X-Requested-With': 'XMLHttpRequest'
})
page_url = f"{xwiki_url}/rest/wikis/xwiki/spaces/{space}/pages/{page_name}"
headers = {
'Content-Type': 'text/plain',
'XWiki-Form-Token': form_token,
'X-Requested-With': 'XMLHttpRequest'
}
data = {
'content': content,
'formtoken': form_token # Ensure the form token is included in data if required by API
}
response = session.get(page_url, headers=headers)
if response.status_code == 200:
response = session.put(page_url, headers=headers, data=data)
elif response.status_code == 404:
space_url = f"{xwiki_url}/rest/wikis/xwiki/spaces/{space}/pages"
response = session.post(space_url, headers=headers, data=data, params={'name': page_name})
response.raise_for_status()
if __name__ == "__main__":
try:
# Get article data from HelpJuice
title, html_content = get_helpjuice_article(ARTICLE_ID, HELPJUICE_API_KEY)
if title and html_content:
# Convert HTML content to XWiki syntax
xwiki_content = html_to_xwiki_syntax(html_content)
# Migrate content to XWiki
migrate_to_xwiki(XWIKI_URL, XWIKI_USERNAME, XWIKI_PASSWORD, XWIKI_SPACE, title.replace(" ", ""), xwiki_content)
print(f"Successfully migrated '{title}' to XWiki.")
else:
print("Failed to retrieve article data.")
except Exception as e:
print(f"An error occurred: {str(e)}")