-
Notifications
You must be signed in to change notification settings - Fork 23
/
confluence-markdown-export.py
209 lines (165 loc) · 8.21 KB
/
confluence-markdown-export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import argparse
from urllib.parse import urlparse, urlunparse
import requests
import bs4
from markdownify import MarkdownConverter
from atlassian import Confluence
ATTACHMENT_FOLDER_NAME = "attachments"
DOWNLOAD_CHUNK_SIZE = 4 * 1024 * 1024 # 4MB, since we're single threaded this is safe to raise much higher
class ExportException(Exception):
pass
class Exporter:
def __init__(self, url, username, token, out_dir, space, no_attach):
self.__out_dir = out_dir
self.__parsed_url = urlparse(url)
self.__username = username
self.__token = token
self.__confluence = Confluence(url=urlunparse(self.__parsed_url),
username=self.__username,
password=self.__token)
self.__seen = set()
self.__no_attach = no_attach
self.__space = space
def __sanitize_filename(self, document_name_raw):
document_name = document_name_raw
for invalid in ["..", "/"]:
if invalid in document_name:
print("Dangerous page title: \"{}\", \"{}\" found, replacing it with \"_\"".format(
document_name,
invalid))
document_name = document_name.replace(invalid, "_")
return document_name
def __dump_page(self, src_id, parents):
if src_id in self.__seen:
# this could theoretically happen if Page IDs are not unique or there is a circle
raise ExportException("Duplicate Page ID Found!")
page = self.__confluence.get_page_by_id(src_id, expand="body.storage")
page_title = page["title"]
page_id = page["id"]
# see if there are any children
child_ids = self.__confluence.get_child_id_list(page_id)
content = page["body"]["storage"]["value"]
# save all files as .html for now, we will convert them later
extension = ".html"
if len(child_ids) > 0:
document_name = "index"
else:
document_name = page_title
# make some rudimentary checks, to prevent trivial errors
sanitized_filename = self.__sanitize_filename(document_name) + extension
sanitized_parents = list(map(self.__sanitize_filename, parents))
page_location = sanitized_parents + [sanitized_filename]
page_filename = os.path.join(self.__out_dir, *page_location)
page_output_dir = os.path.dirname(page_filename)
os.makedirs(page_output_dir, exist_ok=True)
print("Saving to {}".format(" / ".join(page_location)))
with open(page_filename, "w", encoding="utf-8") as f:
f.write(content)
# fetch attachments unless disabled
if not self.__no_attach:
ret = self.__confluence.get_attachments_from_content(page_id, start=0, limit=500, expand=None,
filename=None, media_type=None)
for i in ret["results"]:
att_title = i["title"]
download = i["_links"]["download"]
att_url = urlunparse(
(self.__parsed_url[0], self.__parsed_url[1], "/wiki/" + download.lstrip("/"), None, None, None)
)
att_sanitized_name = self.__sanitize_filename(att_title)
att_filename = os.path.join(page_output_dir, ATTACHMENT_FOLDER_NAME, att_sanitized_name)
att_dirname = os.path.dirname(att_filename)
os.makedirs(att_dirname, exist_ok=True)
print("Saving attachment {} to {}".format(att_title, page_location))
print("Using url: {}".format(att_url))
r = requests.get(att_url, auth=(self.__username, self.__token), stream=True)
if 400 <= r.status_code:
if r.status_code == 404:
print("Attachment {} not found (404)!".format(att_url))
continue
# this is a real error, raise it
r.raise_for_status()
with open(att_filename, "wb") as f:
for buf in r.iter_content(chunk_size=DOWNLOAD_CHUNK_SIZE):
f.write(buf)
self.__seen.add(page_id)
# recurse to process child nodes
for child_id in child_ids:
self.__dump_page(child_id, parents=sanitized_parents + [page_title])
def __dump_space(self, space):
space_key = space["key"]
print("Processing space", space_key)
if space.get("homepage") is None:
print("Skipping space: {}, no homepage found!".format(space_key))
print("In order for this tool to work there has to be a root page!")
raise ExportException("No homepage found")
else:
# homepage found, recurse from there
homepage_id = space["homepage"]["id"]
self.__dump_page(homepage_id, parents=[space_key])
def dump(self):
ret = self.__confluence.get_all_spaces(start=0, limit=500, expand='description.plain,homepage')
if ret['size'] == 0:
print("No spaces found in confluence. Please check credentials")
for space in ret["results"]:
if self.__space is None or space["key"] == self.__space:
self.__dump_space(space)
class Converter:
def __init__(self, out_dir):
self.__out_dir = out_dir
def recurse_findfiles(self, path):
for entry in os.scandir(path):
if entry.is_dir(follow_symlinks=False):
yield from self.recurse_findfiles(entry.path)
elif entry.is_file(follow_symlinks=False):
yield entry
else:
raise NotImplemented()
def __convert_atlassian_html(self, soup):
for image in soup.find_all("ac:image"):
url = None
for child in image.children:
url = child.get("ri:filename", None)
break
if url is None:
# no url found for ac:image
continue
# construct new, actually valid HTML tag
srcurl = os.path.join(ATTACHMENT_FOLDER_NAME, url)
imgtag = soup.new_tag("img", attrs={"src": srcurl, "alt": srcurl})
# insert a linebreak after the original "ac:image" tag, then replace with an actual img tag
image.insert_after(soup.new_tag("br"))
image.replace_with(imgtag)
return soup
def convert(self):
for entry in self.recurse_findfiles(self.__out_dir):
path = entry.path
if not path.endswith(".html"):
continue
print("Converting {}".format(path))
with open(path, "r", encoding="utf-8") as f:
data = f.read()
soup_raw = bs4.BeautifulSoup(data, 'html.parser')
soup = self.__convert_atlassian_html(soup_raw)
md = MarkdownConverter().convert_soup(soup)
newname = os.path.splitext(path)[0]
with open(newname + ".md", "w", encoding="utf-8") as f:
f.write(md)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("url", type=str, help="The url to the confluence instance")
parser.add_argument("username", type=str, help="The username")
parser.add_argument("token", type=str, help="The access token to Confluence")
parser.add_argument("out_dir", type=str, help="The directory to output the files to")
parser.add_argument("--space", type=str, required=False, default=None, help="Spaces to export")
parser.add_argument("--skip-attachments", action="store_true", dest="no_attach", required=False,
default=False, help="Skip fetching attachments")
parser.add_argument("--no-fetch", action="store_true", dest="no_fetch", required=False,
default=False, help="This option only runs the markdown conversion")
args = parser.parse_args()
if not args.no_fetch:
dumper = Exporter(url=args.url, username=args.username, token=args.token, out_dir=args.out_dir,
space=args.space, no_attach=args.no_attach)
dumper.dump()
converter = Converter(out_dir=args.out_dir)
converter.convert()