-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf2md2.py
303 lines (235 loc) · 8.09 KB
/
conf2md2.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
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
#
# Confluence to Markdown converter
#
# Requires Python 3.5+
# see requirements.txt for Python dependencies
#
# Specify username and password (API token) in environment variables:
# ATLASSIAN_TOKEN=xxxxxxx
#
# Obtain an API token from: https://id.atlassian.com
# Security -> API token -> Create and manage API tokens
#
import requests
from dataclasses import dataclass
from requests.auth import HTTPBasicAuth
import json
import re
import html2text
import argparse
import os
import io
import shutil
import types
try:
from getpass import getpass
except:
def getpass(message): return raw_input(message)
@dataclass
class PageParts:
space: str
contentid: str
title: str = ''
confuri: str = ''
class Confluence(object):
def __init__(self, uri, user, authToken):
self.base = uri
self.creds = (user,authToken)
self.headers = {
'Content-Type': 'application/json'
}
pass
def apiuri(self, uri):
return self.base + '/wiki/api/v2/' + uri#/rest/api/' + uri
def url_parts(uri):
rx = re.compile(r"(http[s*]:\/\/.+\/wiki)\/spaces\/(\w+)\/pages\/(\d+)\/(.+)")
m = rx.match(uri)
if not m:
raise UrlError(f"invalid Confluence URI: {uri}")
return PageParts(
confuri = m.group(1),
space = m.group(2),
contentid = m.group(3)
)
def dumpjson(self,j):
j = json.loads(j)
print(json.dumps(j, indent=4, sort_keys=True))
pass
def lookup_content(self, space, title):
url = self.apiuri('content')
params = f"spaceKey={space}&title={title}&expand=body"
r = requests.get(url, params=params, auth=self.creds, headers=self.headers)
j = json.loads(r.text)
#self.dumpjson(r.text)
return j['results'][0]['id']
def get_content(self, space, pageid, view='storage'):
uri = self.apiuri(f'content/{pageid}')
params = {'expand': f"body.{view}"}
r = requests.get(uri, params=params, auth=self.creds, headers=self.headers)
j = json.loads(r.text)
#self.dumpjson(r)
return j['body'][view]['value']
def grab_binary(self, url, outf):
#urllib.request.urlretrieve("http://example.com", "file.ext")
r = requests.get(url, headers=self.headers, auth=self.creds, stream=True)
with open('img.png', 'wb') as out_file:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, out_file)
#out_file.write(r.raw)#r.raw.decode_content = True
def images(self, pageid):
uri = self.apiuri(f'content/{pageid}/child')
params = {'expand': ['attachment']}
r = requests.get(uri, params=params, auth=self.creds,headers=self.headers)
j = json.loads(r.text)
#print(json.dumps(j, indent=2))
#print(r.text)
return [
PageParts(
confuri = cpp.confuri,
space = cpp.space,
title = result['title'],
contentid = result['id']
)# if result['metadata']['mediaType'] == 'image/png'
for result in j['attachment']['results']
]
def children(self, pageid):
#uri = self.apiuri(f'content/{pageid}/child')
uri = self.apiuri(f'/pages/{pageid}/children')
params = {'expand': ['page']}
r = requests.get(uri, params=params, auth=self.creds,headers=self.headers)
print(r.text)
j = json.loads(r.text)
#print(json.dumps(j, indent=2))
return [
PageParts(
confuri = cpp.confuri,
space = cpp.space,
title = result['title'],
contentid = result['id']
)# if result['metadata']['mediaType'] == 'image/png'
for result in j['page']['results']
]
def verify_creds(args):
if not args.username and 'ATLASSIAN_USER' in os.environ:
args.username = os.environ['ATLASSIAN_USER']
if not args.username:
print("--username required")
return False
if not args.password and 'ATLASSIAN_TOKEN' in os.environ:
args.password = os.environ['ATLASSIAN_TOKEN']
if not args.password:
args.password = getpass("Password: ")
if not args.password:
print("--password required")
return False
return True
def treeimg(level):
if level == 0:
return '0'
if level == 1:
return f'{level}├─'
if level > 1:
return f'{level}├─' + '──'*(level-1)
def print_treepart(s, nodech, fillch):
print(nodech + fillch*3, file=s, end='')
def reverse_chunks(s, k):
f=lambda s,n:s and f(s[n:],n)+s[:n]
return f(s,k)
#return ''.join(list(map(''.join, zip(*[iter(s)]*k))).reverse())
# return ''.join(s[i:i+k][::-1] for i in range(0, len(s), k))
def print_treelines(levelp):
s = io.StringIO()
if(levelp.hassibling):
print_treepart(s, '├', '─')
else:
print_treepart(s, '└', '─')
levelp = levelp.parent
while(levelp):
if(levelp.hassibling):
print_treepart(s, '│', ' ')
else:
print_treepart(s, ' ', ' ')
levelp = levelp.parent
s.seek(0)
return reverse_chunks(s.read(), 4)
def fixup_images(conf, html_string, levelp = None):
#level = types.SimpleNamespace(
# # haschildren = False,
# parent = levelp
#)
prefix = print_treelines(levelp)
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_string, features="html.parser")
images = soup.findAll('img')
for idx,img in enumerate(images):
#imgid =
rx = re.compile(r"\/(\d+)\/")
m = rx.match(img['src'])
imgid = m.group[1] if m else '?'
#level = types.SimpleNamespace(
# hassibling = idx<len(images)-1,
# parent = levelp
#)
prefix = print_treelines(levelp)
print(f"Images: {prefix}++++{imgid}")
#conf.grab_binary(img['src'], 'oof')
#img['src'] = 'img.png'
return str(soup)
def grab_page(conf, cpp, recurse = True, levelp = None, hassibling=False):#space, pageid):
#conf = Confluence(cpp.confuri, args.username, args.password)
children_list = conf.children(cpp.contentid)
#if len(children_list) > 0 and levelp:
# levelp.haschildren = True
image_list = conf.images(cpp.contentid)
#if len(children_list) > 0 and levelp or image_list and len(image_list)>0 and levelp:
# levelp.haschildren = True
level = types.SimpleNamespace(
hassibling = hassibling,
parent = levelp
)
#print(children_list)
prefix = print_treelines(level)
print(f'Pulling: {prefix}[{cpp.contentid}]: {cpp.title}')
html = conf.get_content(cpp.space, cpp.contentid, "view")
html_string = fixup_images(conf, html, level)
# see: https://github.com/Alir3z4/html2text/blob/master/docs/usage.md
h2t = html2text.HTML2Text()
h2t.bypass_tables = False#True
h2t.ignore_images = False
md = h2t.handle(html_string)
#print(md)
for idx, child in enumerate(children_list):# conf.children(cpp.contentid):
#level.hassibling
hs = idx < len(children_list)-1
grab_page(conf, child, levelp = level, hassibling=hs)#level+1)
#def grab_page(url):
# pass
if __name__ == "__main__":
parser = argparse.ArgumentParser('Confluence to Markdown converter')
parser.add_argument('--uri', help = 'URI to confluence page')
parser.add_argument('--username', help = 'Username (must specify if ')
parser.add_argument('--password', help = 'Password')
#parser.add_argument('--space', help = 'Space')
#parser.add_argument('--id', help = 'Content ID')
args = parser.parse_args()
args.uri = 'https://r3-cev.atlassian.net/wiki/spaces/SEC/pages/1967784829/Security+Bug+Fix+Policy'
args.uri = 'https://r3-cev.atlassian.net/wiki/spaces/SEC/pages/3841458249/Security+Issue+Handling'
args.uri = 'https://r3-cev.atlassian.net/wiki/spaces/SEC/pages/3845652552/Vulnerability+Management'
if not args.username:
args.username = os.environ.get('ATLASSIAN_USERNAME')
if not args.username:
print('Please specify --username or ATLASSIAN_USERNAME environment')
sys.exit(0)
if not args.password:
args.password = os.environ.get('ATLASSIAN_PASSWORD')
if not args.password:
args.password = getpass.getpass(f'API token for {args.username}: ')
if not args.uri:
print("must specify --uri")#, or [--space and --content]")
exit(1)
if not verify_creds(args):
exit(1)
cpp = Confluence.url_parts(args.uri)
conf = Confluence(cpp.confuri, args.username, args.password)
grab_page(conf, cpp)