forked from dernorberto/confluenceDumpWithPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfluenceDumpWithPython.py
More file actions
421 lines (387 loc) · 22 KB
/
Copy pathconfluenceDumpWithPython.py
File metadata and controls
421 lines (387 loc) · 22 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os.path
import argparse
import logging
import myModules
"""Dump Confluence content using Python
Args:
mode: Download mode
site: Site to export from
space: Space to export from
page: Page to export
outdir: Folder to export to (optional)
sphinx: Sphinx compatible folder structure (optional)
notags: Do not add tags to rst files (optional)
Returns:
HTML and RST files inside the default or custom output folder
"""
parser = argparse.ArgumentParser()
parser.add_argument('--mode', '-m', dest='mode',
choices=['single', 'space', 'bylabel', 'pageprops', 'recursive'],
help='Chose a download mode', required=True)
parser.add_argument('--site', '-S', type=str,
help='Atlassian Site', required=True)
parser.add_argument('--space', '-s', type=str,
help='Space Key')
parser.add_argument('--page', '-p', type=int,
help='Page ID')
parser.add_argument('--label', '-l', type=str,
help='Page label')
parser.add_argument('--outdir', '-o', type=str, default='output',
help='Folder for export', required=False)
parser.add_argument('--sphinx', '-x', action='store_true', default=False,
help='Sphinx compatible folder structure', required=False)
parser.add_argument('--confluence', '-c', action='store_true', default=False,
help='Confluence compatible folder and file naming structure', required=False)
parser.add_argument('--tags', action='store_true', default=False,
help='Add labels as .. tags::', required=False)
parser.add_argument('--html', action='store_true', default=False,
help='Include .html file in export (default is only .rst)', required=False)
parser.add_argument('--no-rst', action='store_false', dest="rst", default=True,
help='Disable .rst file in export', required=False)
parser.add_argument('--showlabels', action='store_true', default=False,
help='Export .rst files with the page labels at the bottom', required=False)
parser.add_argument('--relativelinks', action='store_true', default=False,
help='Replace the a href links with relative links within the current space when exporting to html.', required=False)
parser.add_argument('--loglevel', default='debug',
choices=['critical', 'error', 'warning', 'info', 'debug'],
help='Provide logging level. Example --loglevel debug, default=warning')
parser.add_argument('--logformat',
help='Provide the logging format. See the documentation of the logging module for more details.')
parser.add_argument('--logfile',
help='Specifies the file that will be used for the logging rather than the standard output stream.')
args = parser.parse_args()
if args.logfile is None:
logging.basicConfig(level=args.loglevel.upper(), format=args.logformat)
else:
logging.basicConfig(level=args.loglevel.upper(), format=args.logformat, filename=args.logfile)
atlassian_site = args.site
sphinx_tags = args.tags
sphinx_compatible = args.sphinx
confluence_compatible = args.confluence
if args.mode == 'single':
logging.info(f"Exporting a single page (Sphinx set to {args.sphinx})")
page_id = args.page
elif args.mode == 'space':
logging.info(f"Exporting a whole space (Sphinx set to {args.sphinx})")
space_key = args.space
elif args.mode == 'recursive':
logging.info(f"Exporting a single page recursively (Sphinx set to {args.sphinx})")
elif args.mode == 'bylabel':
logging.info(f"Exporting all pages with a common label (Sphinx set to {args.sphinx})")
elif args.mode == 'pageprops':
logging.info(f"Exporting a Page Properties page with all its children (Sphinx set to {args.sphinx})")
my_attachments = []
my_embeds = []
my_embeds_externals = []
my_emoticons = []
my_emoticons_list = []
user_name = os.environ["atlassianUserEmail"]
api_token = os.environ["atlassianAPIToken"]
logging.debug("Sphinx set to " + str(sphinx_compatible))
logging.debug(f"Confluence compatible set to : {confluence_compatible}")
atlassian_site = args.site
my_outdir_base = args.outdir
relative_links = args.relativelinks
if args.mode == 'single':
############
## SINGLE ##
############
page_id = args.page
page_name = myModules.get_page_name(atlassian_site,page_id,user_name,api_token)
my_body_export_view = myModules.get_body_export_view(atlassian_site,page_id,
user_name,api_token).json()
my_body_export_view_html = my_body_export_view['body']['export_view']['value']
my_body_export_view_title = my_body_export_view['title'].replace("/","-")\
.replace(",","").replace("&","And").replace(":","-")
server_url = f"https://{atlassian_site}.atlassian.net/wiki/api/v2/spaces/?limit=250"
page_url = f"{my_body_export_view['_links']['base']}{my_body_export_view['_links']['webui']}"
page_parent = myModules.get_page_parent(atlassian_site,page_id,user_name,api_token)
space_key = myModules.get_page_space_key(atlassian_site,page_id,user_name,api_token)
if relative_links:
space_id = myModules.get_page_space_id(atlassian_site,page_id,user_name,api_token)
all_pages_full = myModules.get_pages_from_space(atlassian_site,space_id,user_name,api_token)
all_pages_short = []
i = 0
for n in all_pages_full:
i = i + 1
all_pages_short.append({
'page_id' : n['id'],
'pageTitle' : n['title'],
'parentId' : n['parentId'],
'space_id' : n['spaceId'],
}
)
if confluence_compatible:
my_outdir_base = os.path.join(my_outdir_base,space_key)
else:
my_outdir_base = os.path.join(my_outdir_base,f"{page_id}-{my_body_export_view_title}") # sets outdir to path under page_name
my_outdir_content = my_outdir_base
# if args.sphinx is False:
# my_outdir_base = os.path.join(my_outdir_base,f"{page_id}-{my_body_export_view_title}") # sets outdir to path under page_name
# my_outdir_content = my_outdir_base
# else:
# my_outdir_content = my_outdir_base
my_outdirs = []
my_outdirs = myModules.mk_outdirs(my_outdir_base, page_id, confluence_compatible) # attachments, embeds, scripts
my_page_labels = myModules.get_page_labels(atlassian_site,page_id,user_name,api_token)
logging.debug(f"Base export folder is \"{my_outdir_base}\" and the Content goes to \"{my_outdir_content}\"")
myModules.dump_html(atlassian_site,space_key,my_body_export_view_html,my_body_export_view_title,page_id,my_outdir_base, my_outdir_content,my_page_labels,page_parent,user_name,api_token,sphinx_compatible,sphinx_tags,arg_html_output=args.html,arg_rst_output=args.rst,arg_space_pages_short=(all_pages_short if relative_links else []),arg_confluence_compatible=confluence_compatible)
logging.info("Done!")
if args.mode == 'recursive':
###############
## RECURSIVE ##
###############
page_id = args.page
page_name = myModules.get_page_name(atlassian_site,page_id,user_name,api_token)
my_body_export_view = myModules.get_body_export_view(atlassian_site,page_id,
user_name,api_token).json()
my_body_export_view_html = my_body_export_view['body']['export_view']['value']
my_body_export_view_title = my_body_export_view['title'].replace("/","-")\
.replace(",","").replace("&","And").replace(":","-")
server_url = f"https://{atlassian_site}.atlassian.net/wiki/api/v2/spaces/?limit=250"
page_url = f"{my_body_export_view['_links']['base']}{my_body_export_view['_links']['webui']}"
page_parent = myModules.get_page_parent(atlassian_site,page_id,user_name,api_token)
space_key = myModules.get_page_space_key(atlassian_site,page_id,user_name,api_token)
space_id = myModules.get_page_space_id(atlassian_site,page_id,user_name,api_token)
all_pages_full = myModules.get_pages_from_space(atlassian_site,space_id,user_name,api_token)
all_pages_short = []
for n in all_pages_full:
all_pages_short.append({
'page_id' : n['id'],
'pageTitle' : n['title'],
'parentId' : n['parentId'],
'space_id' : n['spaceId'],
}
)
if confluence_compatible:
my_outdir_base = os.path.join(my_outdir_base,space_key)
else:
my_outdir_base = os.path.join(my_outdir_base,f"{page_id}-{my_body_export_view_title}") # sets outdir to path under page_name
my_outdir_content = my_outdir_base
# if args.sphinx is False:
# my_outdir_base = os.path.join(my_outdir_base,f"{page_id}-{my_body_export_view_title}") # sets outdir to path under page_name
# my_outdir_content = my_outdir_base
# else:
# my_outdir_content = my_outdir_base
my_outdirs = []
my_outdirs = myModules.mk_outdirs(my_outdir_base, page_id, confluence_compatible) # attachments, embeds, scripts
my_page_labels = myModules.get_page_labels(atlassian_site,page_id,user_name,api_token)
logging.debug(f"Base export folder is \"{my_outdir_base}\" and the Content goes to \"{my_outdir_content}\"")
all_pages_recursive = []
for p in all_pages_short:
if p['page_id'] == str(page_id):
def get_child_pages(arg_page_id):
children = []
child_pages = list(filter (lambda x: x['parentId'] == arg_page_id, all_pages_short))
for child_page in child_pages:
# Add the children of the provided page id.
children.append(child_page)
# Get the children of the child page.
child_page_id = child_page['page_id']
child_pages = list(filter (lambda x: x['parentId'] == child_page_id, all_pages_short))
[children.append(x) for x in child_pages if x not in children]
# Get the recursive children of the current child page.
[children.append(x) for x in get_child_pages(child_page_id) if x not in children]
return children
all_pages_recursive = get_child_pages(p['page_id'])
all_pages_recursive.append(p)
page_counter = 0
for p in all_pages_recursive:
page_counter = page_counter + 1
my_body_export_view = myModules.get_body_export_view(atlassian_site,p['page_id'],user_name,api_token).json()
my_body_export_view_html = my_body_export_view['body']['export_view']['value']
my_body_export_view_name = p['pageTitle']
my_body_export_view_title = p['pageTitle']
logging.debug("")
logging.debug(f"Getting page #{page_counter}/{len(all_pages_short)}, {my_body_export_view_title}, {p['page_id']}")
my_body_export_view_labels = myModules.get_page_labels(atlassian_site,p['page_id'],user_name,api_token)
#my_body_export_view_labels = ",".join(myModules.get_page_labels(atlassian_site,p['page_id'],user_name,api_token))
mypage_url = f"{my_body_export_view['_links']['base']}{my_body_export_view['_links']['webui']}"
logging.debug(f"dump_html arg sphinx_compatible = {sphinx_compatible}")
myModules.dump_html(atlassian_site,space_key,my_body_export_view_html,my_body_export_view_title,p['page_id'],my_outdir_base,my_outdir_content,my_body_export_view_labels,p['parentId'],user_name,api_token,sphinx_compatible,sphinx_tags,arg_html_output=args.html,arg_rst_output=args.rst,arg_space_pages_short=(all_pages_short if relative_links else []),arg_confluence_compatible=confluence_compatible)
logging.info("Done!")
elif args.mode == 'space':
###########
## SPACE ##
###########
all_spaces_full = myModules.get_spaces_all(atlassian_site,user_name,api_token) # get a dump of all spaces
all_spaces_short = [] # initialize list for less detailed list of spaces
i = 0
for n in all_spaces_full:
i = i +1
all_spaces_short.append({ # append the list of spaces
'space_key' : n['key'],
'space_id' : n['id'],
'space_name' : n['name'],
'homepage_id' : n['homepageId'],
'spaceDescription' : n['description'],
})
if (n['key'] == space_key) or n['key'] == str.upper(space_key) or n['key'] == str.lower(space_key):
logging.debug("Found space: " + n['key'])
space_id = n['id']
space_name = n['name']
current_parent = n['homepageId']
if confluence_compatible:
my_outdir_content = os.path.join(my_outdir_base,space_key)
else:
my_outdir_content = os.path.join(my_outdir_base,f"{space_id}-{space_name}")
if not os.path.exists(my_outdir_content):
os.mkdir(my_outdir_content)
if args.sphinx is False:
my_outdir_base = my_outdir_content
#logging.debug("my_outdir_base: " + my_outdir_base)
#logging.debug("my_outdir_content: " + my_outdir_content)
if space_key == "" or space_key is None: # if the supplied space key can't be found
logging.warn("Could not find Space Key in this site")
else:
space_title = myModules.get_space_title(atlassian_site,space_id,user_name,api_token)
#
# get list of pages from space
#
all_pages_full = myModules.get_pages_from_space(atlassian_site,space_id,user_name,api_token)
all_pages_short = []
i = 0
for n in all_pages_full:
i = i + 1
all_pages_short.append({
'page_id' : n['id'],
'pageTitle' : n['title'],
'parentId' : n['parentId'],
'space_id' : n['spaceId'],
}
)
# Make an index.html file
if args.html == True:
myModules.dump_index_file(all_pages_full, my_outdir_content, space_key, sphinx_compatible, confluence_compatible)
# put it all together
logging.debug(f"{len(all_pages_short)} pages to export")
page_counter = 0
for p in all_pages_short:
page_counter = page_counter + 1
my_body_export_view = myModules.get_body_export_view(atlassian_site,p['page_id'],user_name,api_token).json()
my_body_export_view_html = my_body_export_view['body']['export_view']['value']
my_body_export_view_name = p['pageTitle']
my_body_export_view_title = p['pageTitle']
logging.debug("")
logging.debug(f"Getting page #{page_counter}/{len(all_pages_short)}, {my_body_export_view_title}, {p['page_id']}")
my_body_export_view_labels = myModules.get_page_labels(atlassian_site,p['page_id'],user_name,api_token)
#my_body_export_view_labels = ",".join(myModules.get_page_labels(atlassian_site,p['page_id'],user_name,api_token))
mypage_url = f"{my_body_export_view['_links']['base']}{my_body_export_view['_links']['webui']}"
logging.debug(f"dump_html arg sphinx_compatible = {sphinx_compatible}")
myModules.dump_html(atlassian_site,space_key,my_body_export_view_html,my_body_export_view_title,p['page_id'],my_outdir_base,my_outdir_content,my_body_export_view_labels,p['parentId'],user_name,api_token,sphinx_compatible,sphinx_tags,arg_html_output=args.html,arg_rst_output=args.rst,arg_space_pages_short=(all_pages_short if relative_links else []),arg_confluence_compatible=confluence_compatible)
logging.debug("Done!")
elif args.mode == 'pageprops':
###############
## PAGEPROPS ##
###############
my_page_properties_children = []
my_page_properties_children_dict = {}
page_id = args.page
space_key = myModules.get_page_space_key(atlassian_site,page_id,user_name,api_token)
if relative_links:
space_id = myModules.get_page_space_id(atlassian_site,page_id,user_name,api_token)
all_pages_full = myModules.get_pages_from_space(atlassian_site,space_id,user_name,api_token)
all_pages_short = []
i = 0
for n in all_pages_full:
i = i + 1
all_pages_short.append({
'page_id' : n['id'],
'pageTitle' : n['title'],
'parentId' : n['parentId'],
'space_id' : n['spaceId'],
}
)
#
# Get Page Properties REPORT
#
logging.debug("Getting Page Properties Report Details")
my_report_export_view = myModules.get_body_export_view(atlassian_site,page_id,user_name,api_token).json()
my_report_export_view_title = my_report_export_view['title'].replace("/","-").replace(",","").replace("&","And").replace(":","-")
my_report_export_view_html = my_report_export_view['body']['export_view']['value']
my_report_export_viewName = myModules.get_page_name(atlassian_site,page_id,user_name,api_token)
my_report_export_view_labels = myModules.get_page_labels(atlassian_site,page_id,user_name,api_token)
my_report_export_page_url = f"{my_report_export_view['_links']['base']}{my_report_export_view['_links']['webui']}"
my_report_export_page_parent = myModules.get_page_parent(atlassian_site,page_id,user_name,api_token)
my_report_export_html_filename = f"{my_report_export_view_title}.html"
# str(my_report_export_view_title) + '.html'
# my outdirs
if confluence_compatible:
my_outdir_content = os.path.join(my_outdir_base,space_key)
else:
my_outdir_content = os.path.join(my_outdir_base,str(page_id) + "-" + str(my_report_export_view_title))
#logging.debug("my_outdir_base: " + my_outdir_base)
#logging.debug("my_outdir_content: " + my_outdir_content)
if args.sphinx is False:
my_outdir_base = my_outdir_content
my_outdirs = []
my_outdirs = myModules.mk_outdirs(my_outdir_base, page_id, confluence_compatible) # attachments, embeds, scripts
# get info abbout children
#my_page_properties_children = myModules.get_page_properties_children(atlassian_site,my_report_export_view_html,my_outdir_content,user_name,api_token)[0] # list
#my_page_properties_children_dict = myModules.get_page_properties_children(atlassian_site,my_report_export_view_html,my_outdir_content,user_name,api_token)[1] # dict
(my_page_properties_children,my_page_properties_children_dict) = myModules.get_page_properties_children(atlassian_site,my_report_export_view_html,my_outdir_content,user_name,api_token)
#
# Get Page Properties CHILDREN
#
page_counter = 0
for p in my_page_properties_children:
page_counter = page_counter + 1
#logging.debug("Handling child: " + p)
my_child_export_view = myModules.get_body_export_view(atlassian_site,p,user_name,api_token).json()
my_child_export_view_html = my_child_export_view['body']['export_view']['value']
my_child_export_view_name = my_page_properties_children_dict[p]['Name']
my_child_export_view_labels = myModules.get_page_labels(atlassian_site,p,user_name,api_token)
my_child_export_view_title = my_child_export_view['title'] ##.replace("/","-").replace(":","-").replace(" ","_")
logging.debug(f"Getting Child page #{page_counter}/{len(my_page_properties_children)}, {my_child_export_view_title}, {my_page_properties_children_dict[str(p)]['ID']}")
#logging.debug("Getting Child page #" + str(page_counter) + '/' + str(len(my_page_properties_children)) + ', ' + my_child_export_view_title + ', ' + my_page_properties_children_dict[str(p)]['ID'])
my_child_export_page_url = f"{my_child_export_view['_links']['base']}{my_child_export_view['_links']['webui']}"
#my_child_export_page_url = str(my_child_export_view['_links']['base']) + str(my_child_export_view['_links']['webui'])
my_child_export_page_parent = myModules.get_page_parent(atlassian_site,p,user_name,api_token)
html_file_name = (f"{my_page_properties_children_dict[p]['Name']}.html").replace(":","-").replace(" ","_")
#html_file_name = my_page_properties_children_dict[p]['Name'].replace(":","-").replace(" ","_") + '.html'
my_page_properties_children_dict[str(p)].update({"Filename": html_file_name})
myModules.dump_html(
arg_site=atlassian_site,
arg_space_key=space_key,
arg_html=my_child_export_view_html,
arg_title=my_child_export_view_title,
arg_page_id=p,
arg_outdir_base=my_outdir_base,
arg_outdir_content=my_outdir_content,
arg_page_labels=my_child_export_view_labels,
arg_page_parent=my_child_export_page_parent,
arg_username=user_name,
arg_api_token=api_token,
arg_sphinx_compatible=sphinx_compatible,
arg_sphinx_tags=sphinx_tags,
arg_type="reportchild",
arg_html_output=args.html,
arg_rst_output=args.rst,
arg_show_labels=args.showlabels,
arg_space_pages_short=(all_pages_short if relative_links else []),
arg_confluence_compatible=confluence_compatible
) # creates html files for every child
myModules.dump_html(
arg_site=atlassian_site,
arg_space_key=space_key,
arg_html=my_report_export_view_html,
arg_title=my_report_export_view_title,
arg_page_id=page_id,
arg_outdir_base=my_outdir_base,
arg_outdir_content=my_outdir_content,
arg_page_labels=my_report_export_view_labels,
arg_page_parent=my_report_export_page_parent,
arg_username=user_name,
arg_api_token=api_token,
arg_sphinx_compatible=sphinx_compatible,
arg_sphinx_tags=sphinx_tags,
arg_type="report",
arg_html_output=args.html,
arg_rst_output=args.rst,
arg_show_labels=args.showlabels,
arg_space_pages_short=(all_pages_short if relative_links else []),
arg_confluence_compatible=confluence_compatible
) # finally creating the HTML for the report page
logging.info("Done!")
else:
logging.error("No script mode defined in the command line")