-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexporter.py
90 lines (68 loc) · 2.96 KB
/
exporter.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
# coding=utf-8
import json
import time
import os
import logging
import treelib
from api import old_confluence_api
import utils
logger = logging.getLogger(__name__)
def dump_page_list():
pages = old_confluence_api.getPages('duitang') # very long time
with open(utils.PAGES_JSON_FILE_PATH, 'w') as pages_file:
pages_file.write(json.dumps(utils.format_value(pages)))
def dump_page(page_id):
page = old_confluence_api.getPage(page_id)
with open(os.path.join(utils.DATA_DIR, 'pages', page_id + '.json'), 'w') as page_file:
page_file.write(json.dumps(utils.format_value(page)))
def batch_dump(name, func):
pages = utils.load_pages()
ordered_pages = utils.sort_pages(pages)
success_count = 0
fail_count = 0
#is_skip = True
for page in ordered_pages:
#if page['id'] == '13730404':
#is_skip = False
#if is_skip:
#continue
try:
func(page['id'])
except Exception as e:
logger.error('dump %s fail, page id: %s' % (name, page['id']))
fail_count += 1
raise e
time.sleep(0.01)
success_count += 1
logger.info('dump %s, page: %s, title: %s, s/f/t: %d/%d/%d' % (
name, page['id'], page['title'], success_count, fail_count, success_count + fail_count))
logger.info('dump %s, s/f/t: %d/%d/%d' % (
name, success_count, fail_count, success_count + fail_count))
def dump_pages():
batch_dump('page', dump_page)
def dump_comments_for_page(page_id):
comments = old_confluence_api.getComments(page_id)
if len(comments) == 0:
return
with open(os.path.join(utils.DATA_DIR, 'comments', page_id + '.json'), 'w') as comments_file:
comments_file.write(json.dumps(utils.format_value(comments)))
def dump_comments():
if not os.path.exists(os.path.join(utils.DATA_DIR, 'comments')):
os.mkdir(os.path.join(utils.DATA_DIR, 'comments'))
batch_dump('comment', dump_comments_for_page)
def dump_attachments_for_page(page_id):
attachments = old_confluence_api.getAttachments(page_id)
if len(attachments) == 0:
return
with open(os.path.join(utils.DATA_DIR, 'attachments', page_id + '.json'), 'w') as attachment_file:
attachment_file.write(json.dumps(utils.format_value(attachments)))
if not os.path.exists(os.path.join(utils.DATA_DIR, 'attachments', page_id + '_contents')):
os.mkdir(os.path.join(utils.DATA_DIR, 'attachments', page_id + '_contents'))
for attachment in attachments:
with open(os.path.join(utils.DATA_DIR, 'attachments', page_id + '_contents', attachment['id']),
'wb') as content_file:
content_file.write(old_confluence_api.getAttachmentData(page_id, attachment['fileName'], '0').data)
def dump_attachments():
if not os.path.exists(os.path.join(utils.DATA_DIR, 'attachments')):
os.mkdir(os.path.join(utils.DATA_DIR, 'attachments'))
batch_dump('attachments', dump_attachments_for_page)