Versioner sammenlignet

Nøgle

  • Linjen blev tilføjet.
  • Denne linje blev fjernet.
  • Formatering blev ændret.

...

Kodeblok
from atlassian import Confluence
import convert
import requests
from requests.auth import HTTPBasicAuth
from xml.sax.saxutils import escape
import os
import config
from slugify import slugify

confluence = Confluence(
    url=config.conflunce_url,
    username=config.username,
    password=config.password)

def make_slug(title):

    slug = slugify(title)
    return slug

def create_xwki_page(confluence_page_id,title,parent_url,content):

    slugcont_bool = title.replace(" ","")False
    slug = make_slug(title)
    url = parent_url + "/spaces/" + slug + "/pages/WebHome"

    #print (title)
    print (url)
    
    xml = "<page xmlns=\"http://www.xwiki.org\">"
    xml = xml + "<title>" + title + "</title>"
    xml = xml + "<syntax>xwiki/2.1</syntax>"
    xml = xml + "<content>" +  escape(content) + "</content></page>"

    headers = {
        "Content-Type": "application/xml"
    }

    response = requests.put(url, data=xml, headers=headers, auth=HTTPBasicAuth(config.username,config.password))

    #Check the response
    if response.status_code == 201:
        print("Page " + title + " created")
        cont_bool = True
    else:
        if response.status_code == 202:
            print("Page " + title + " updated")
        else:
    cont_bool = True
        else:
            print("Status code:", response.status_code)
            print("Response:", response.text)


     #exit(0)

if cont_bool == True:
    
        #Children#Labels
        children#labels = confluence.get_page_child_by_type(confluence_labels(page_id, typeprefix='page'None, start=None, limit=None, expand=None)
    for   page in#if childrenlabels:

        page_id = page["id"]#    attach_labels_to_page(wiki_space,slug,labels)

        page1 = confluence.get#Attachments
        directory = "attachments/" + confluence_page_by_id(page_id=page_id,expand="body.storage")id
        os.makedirs(directory, exist_ok=True)

        contentattachments = page1["body"]["storage"]["value"]
        title = page['title']

        #Labels
        #labels = confluence.get_page_labels(page_id, prefix=None, start=None, limit=None)
confluence.download_attachments_from_page(confluence_page_id, path=directory)
   
         #if labelswith os.scandir(directory) as entries:
        #    for  attach_labels_to_page(wiki_space,slug,labels)

entry in entries:
        #Attachments
        directory = "attachments/" + page_id
if entry.is_file():
           os.makedirs(directory, exist_ok=True)
        (result, log) = convert.convert(contentattach_file_to_page(parent_url + "/spaces/" + slug + "/pages/WebHome",entry.path,entry.name)

    #Children
    xwiki_pagechildren = create_xwki_page(confluence.get_page_child_by_type(confluence_page_id,title,parent_url + "/spaces/" + slug,result) type='page', start=None, limit=None, expand=None)
    for page in children:

        page_id  #exit(0)= page["id"]

        #attachmentspage1 = confluence.downloadget_attachmentspage_fromby_id(page(confluence_id=page_id, path=directoryexpand="body.storage")
    
    content = page1["body"]["storage"]["value"]
  #with os.scandir(directory) as entries:
   title   = page['title']

  #    for entry inprint entries("Preparing:
    " + title)
  #      (result, log) if= entryconvert.is_fileconvert(content):
     
   #            attach_file_to_page(wiki_space,slug,entry.path,entry.name)




def attach_file_to_page(wiki_space,slug,file_path,filename):

    url = config.xwiki_url + "/rest/wikis/xwiki/spaces/" + wiki_space + "/pages/" + slugxwiki_page = create_xwki_page(page_id,title,parent_url + "/spaces/" + slug ,result)
        

def attach_file_to_page(url,file_path,filename):

    url = url + "/attachments/" + filename

    headers = {
        "Content-Type": "application/xml",
        "XWiki-AttachmentComment": "Uploaded by migrator"
    }
    
    with open(file_path, "rb") as f:
        #files = {"file": f}  # 
        response = requests.put(url, data=f, headers=headers, auth=HTTPBasicAuth(config.username,config.password))

    # Check the response
    if response.status_code == 201:
        print("Attachment " + filename + " created")
    else:
        if response.status_code == 202:
            print("Attachment " + filename + " updated" + url)
        else:
            print("Status code:", response.status_code)
            print("Response:", response.text)

def attach_labels_to_page(wiki_space,slug,labels):

    url = config.xwiki_url + "/rest/wikis/xwiki/spaces/" + wiki_space + "/pages/" + slug + "/tags"

    headers = {
        "Content-Type": "application/xml",
    }

    xml = "<tags xmlns=\"http://www.xwiki.org\">"

    for label in labels:
        
        xml = xml + "<tag>" + label + "</tag>"

    xml = xml + "</tag>"

    response = requests.put(url, data=xml, headers=headers, auth=HTTPBasicAuth(config.username,config.password))

    # Check the response
    if response.status_code == 201:
        print("Page " + title + " created")
    else:
        if response.status_code == 202:
            print("Page " + title + " updated")
        else:
            print("Status code:", response.status_code)
            print("Response:", response.text)


#Start on the XWiki
wiki_space = "Sandbox" 

#Start Space in Confluence
space_key = "ATLASSIAN"

space = confluence.get_space(space_key, expand='description.plain,homepage')
homepage_id=space['homepage']['id']
prop = confluence.get_page_by_id(homepage_id,expand="body.storage")
content = prop["body"]["storage"]["value"]
title = prop['title']
slug = title.replace(" ","")

(result, log) = convert.convert(content)

xwiki_page = create_xwki_page(homepage_id,title, config.xwiki_url + "/rest/wikis/xwiki/spaces/SandboxHome",result)