-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfluencePictureDump.py
90 lines (79 loc) · 2.79 KB
/
ConfluencePictureDump.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
#!/usr/bin/env python3
# coding=utf-8
import logging
import argparse
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
import os.path
import time
import magic
from atlassian import Confluence
confluence = Confluence(
url="", # confluence website base url
username="", # your username
password="", # your password
)
sourceSpaceKey = '' # source page's space-key (space overview -> space key)
sourceParentPageName = '' # source page's directory/parent-page NAME (not page-id)
targetSpaceKey = '' # target page's space-key
targetParentPageName = '' # source page's directory/parent-page NAME (not page-id)
def upload_pic(img_url, page_id):
imgDir, fileName = download_pic(img_url, page_id)
print(imgDir)
contentType = magic.from_file(imgDir, mime=True)
confluence.attach_file(imgDir, name=None, content_type=contentType, page_id=childId, title=None, space=None, comment=None)
return fileName
def download_pic(pic_url, page_id):
pic_url = pic_url.split('?')[0]
name = pic_url.split('/')[-1]
dir_path = './pic/'
if not os.path.exists(dir_path):
os.makedirs(dir_path)
dir_path += page_id
if not os.path.exists(dir_path):
os.makedirs(dir_path)
local_pic_path = dir_path + '/' + name
if os.path.exists(local_pic_path):
return local_pic_path, name
while True:
try:
urlretrieve(pic_url, local_pic_path)
break
except:
time.sleep(5)
continue
return local_pic_path, name
soursePageId = confluence.get_page_id(sourceSpaceKey, sourceParentPageName)
targetPageId = confluence.get_page_id(targetSpaceKey, targetParentPageName)
childList = list(confluence.get_page_child_by_type(soursePageId, type='page', start=None, limit=None))
for child in childList:
childId = child['id']
print('childId:', childId)
childPage = confluence.get_page_by_id(childId, expand='body.storage', status=None, version=None)
childHtml = childPage.get('body').get('storage').get('value')
soup = BeautifulSoup(childHtml, 'html.parser')
for imgTag in soup.select('ac\:image'):
# print(imgTag)
for urlTag in imgTag.children:
if urlTag.name != 'ri:url' or 'ri:value' not in urlTag.attrs:
continue
time.sleep(3)
fileName = upload_pic(urlTag['ri:value'], childId)
urlTag.name = 'ri:attachment'
del urlTag['ri:value']
urlTag['ri:filename'] = fileName
try:
urlTag.parent.parent.parent.span.unwrap()
except:
print('Warn: unwrap fail!')
continue
# print(soup)
updateRes = confluence.update_existing_page(childId, childPage.get('title'), body=str(soup))
try:
if updateRes.get('id') != 0:
print(f'{childId} update success!')
moveRes = confluence.move_page(targetSpaceKey, page_id=childId, target_id=targetPageId, position='append')
print(f'{childId} move success!')
except:
print(f'{childId} update fail! Maybe something goes wrong.')
time.sleep(10)