Files
Gainsight/Scripts/Backup_Notes_Scripts/main_md2confluence_allfiles.py

121 lines
4.7 KiB
Python
Raw Permalink Normal View History

import markdown
import requests
from requests.auth import HTTPBasicAuth
import json
import os
2023-05-10 10:17:32 -04:00
def findCompanies():
2023-05-10 10:17:32 -04:00
rootdir = "/Users/normrasmussen/Documents/Work/CustomerNotes/"
files = os.listdir(rootdir)
for fileName in files:
if fileName.startswith(".") or fileName.startswith("ima"):
pass
else:
company = fileName[:-3]
2023-05-10 10:17:32 -04:00
print(company)
readFile(company)
2023-05-10 10:17:32 -04:00
def readFile(company):
2023-05-10 10:17:32 -04:00
rootdir = "/Users/normrasmussen/Documents/Work/CustomerNotes/"
with open(rootdir + company + ".md", "r") as companyfile:
notes = companyfile.read()
notes = markdown.markdown(notes)
createNewPage(company, notes)
2023-05-10 10:17:32 -04:00
def createNewPage(company, notes):
url = "https://northpass.atlassian.net/wiki/rest/api/content/"
2023-05-10 10:17:32 -04:00
auth = HTTPBasicAuth("nrasmussen@northpass.com",
"ATATT3xFfGF0QZUomC1s3hvD4Hiqh_usOVFAVLsT1n8lt6gzD7wfL8D8x5ner3SE24JD4E590xoT9PKPIi1Eppanx12q5ALzMHKce-KrcIZRT23BvO8MDXwyvbzAO2R4hALc8ZUTI_8-OM-x9o_tjbCHLxEMFOr6QFDYprwdHGZjAxpviSwXrCQ=218AC438")
headers = {
"X-Atlassian-Token": "no-check",
"Accept": "application/json",
2023-05-10 10:17:32 -04:00
"Content-Type": "application/json",
}
2023-05-10 10:17:32 -04:00
payload = json.dumps(
{
"type": "page",
"title": company,
"ancestors": [{"id": 2210463745}],
"space": {"key": "~350535240"},
"body": {"storage": {"value": notes, "representation": "storage"}},
}
)
2023-05-10 10:17:32 -04:00
response = requests.request("POST", url, data=payload, headers=headers, auth=auth)
# print("createNewPage function has run")
response = json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
exists = "already exists"
2023-05-10 10:17:32 -04:00
# This if statement checks if the response from the call includes that the page already exists.
# If the page does exist, it will get the ID and Version of the page and then run a PUT call to update the page.
if exists in response:
2023-05-10 10:17:32 -04:00
print("This page exists! Updating page instead.")
getContent(company, notes)
2023-05-10 10:17:32 -04:00
def getContent(company, notes):
url = "https://northpass.atlassian.net/wiki/rest/api/content/search?cql=parent=2210463745&expand=body.storage,version"
# Found the answer to this URL here: https://community.atlassian.com/t5/Confluence-questions/How-can-i-get-the-page-version-using-a-specific-page-id/qaq-p/898721
2023-05-10 10:17:32 -04:00
auth = HTTPBasicAuth("nrasmussen@northpass.com",
"ATATT3xFfGF0QZUomC1s3hvD4Hiqh_usOVFAVLsT1n8lt6gzD7wfL8D8x5ner3SE24JD4E590xoT9PKPIi1Eppanx12q5ALzMHKce-KrcIZRT23BvO8MDXwyvbzAO2R4hALc8ZUTI_8-OM-x9o_tjbCHLxEMFOr6QFDYprwdHGZjAxpviSwXrCQ=218AC438")
headers = {
"Accept": "application/json",
2023-05-10 10:17:32 -04:00
}
response = requests.request("GET", url, headers=headers, auth=auth)
jsonResponse = response.json()
2023-05-10 10:17:32 -04:00
text = json.dumps(
json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")
)
listCompanies = []
2023-05-10 10:17:32 -04:00
for response in jsonResponse["results"]:
if response["title"] == company:
print(f"{company} Found.")
version = int(response["version"]["_links"]["self"][-1])
if version == "":
version = 1
else:
pass
2023-05-10 10:17:32 -04:00
id = response["id"]
# print(id)
# print(version)
updatePage(company, notes, id, version)
else:
listCompanies.append(response["title"])
pass
2023-05-10 10:17:32 -04:00
# print("Other Companies, not pertinent right now. List of companies:")
# print(listCompanies)
def updatePage(company, notes, id, version):
url = f"https://northpass.atlassian.net/wiki/rest/api/content/{id}"
2023-05-10 10:17:32 -04:00
newVersion = version + 1
auth = HTTPBasicAuth("nrasmussen@northpass.com",
"ATATT3xFfGF0QZUomC1s3hvD4Hiqh_usOVFAVLsT1n8lt6gzD7wfL8D8x5ner3SE24JD4E590xoT9PKPIi1Eppanx12q5ALzMHKce-KrcIZRT23BvO8MDXwyvbzAO2R4hALc8ZUTI_8-OM-x9o_tjbCHLxEMFOr6QFDYprwdHGZjAxpviSwXrCQ=218AC438")
headers = {
"X-Atlassian-Token": "no-check",
"Accept": "application/json",
"Content-Type": "application/json",
2023-05-10 10:17:32 -04:00
"User-Agent": "python-requests/2.28.1",
}
2023-05-10 10:17:32 -04:00
payload = json.dumps(
{
"version": {"number": newVersion},
"type": "page",
"title": company,
"status": "current",
"ancestors": [{"id": 2210463745}],
"body": {"storage": {"value": notes, "representation": "storage"}},
}
)
2023-05-10 10:17:32 -04:00
response = requests.request("PUT", url, data=payload, headers=headers, auth=auth)
response = json.dumps(
json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")
)
print("updatePage function has run")
if __name__ == "__main__":
findCompanies()