# -*- coding: utf-8 -*-
'''
Creates a full Confluence user export as CSV
1. IMPORTANT: should be executed with Python 3 interpreter
2. Change the username, password and baseurl variables to match custom Confluence
3. Execution from terminal: python3 create_full_export_csv_python3.py
4. A new file named confluence_users.csv is now available in current directory
'''
from urllib import request, parse, error
import json
import base64
# CHANGE THESE VARIABLES
CONFLUENCE_USERNAME = "admin"
CONFLUENCE_PASSWORD = "admin"
CONFLUENCE_BASEURL = "http://localhost:1990/confluence"
CONFLUENCE_CSV_FILE_NAME = "confluence-users.csv"
# Search variables
# eg. "admin" or "admin@admin.com"
CONFLUENCE_USER_EXPORT_SEARCH_TERM = ""
# True if enabled users is to be searched
CONFLUENCE_USER_EXPORT_ACTIVE_USERS = True
# True if disabled users is to be searched
CONFLUENCE_USER_EXPORT_INACTIVE_USERS = True
# Starting offset is 0
CONFLUENCE_USER_EXPORT_OFFSET = 0
# Pagesize. 100 is max page size
CONFLUENCE_USER_EXPORT_PAGESIZE = 20
CONFLUENCE_USER_EXPORT_SEARCH = "/rest/confluenceuserexport/1.0/search"
CONFLUENCE_USER_EXPORT_CSV_SERVLET = "/rest/confluenceuserexport/1.0/file/csv"
CONFLUENCE_USER_EXPORT_DOWNLOAD = "/plugins/servlet/confluenceuserexport/admin/download"
JSON_MIME_TYPE = "application/json"
BASIC_AUTH = "Basic " + base64.b64encode(bytes('%s:%s' % (CONFLUENCE_USERNAME, CONFLUENCE_PASSWORD), 'utf-8')).decode('utf-8')
all_users = []
def search_users(search_string, active_users, inactive_users, page_size, offset):
'''
Search for Confluence user with the given parameters
:param search_string: the given search string
:param active_users: true if active users
:param inactive_users: true if inactive users
:param page_size: the given page size eg. 20
:param offset: the given offset eg. 0
:return: None
'''''
request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH}
request_url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_SEARCH
request_body = {"searchString":search_string,
"activeUsers":active_users,
"inActiveUsers":inactive_users,
"pageSize":page_size,
"offset":offset
}
post_request_json_bytes = json.dumps(request_body).encode("utf-8")
post_request = request.Request(request_url, headers=request_headers)
post_request.get_method = lambda: 'POST'
try:
post_request_open = request.urlopen(post_request, data=post_request_json_bytes)
post_response = post_request_open.read()
if post_response:
json_response = json.loads(s=post_response)
if json_response:
found_users = json_response.get("users")
# Check for next page of users in order to search for more users
has_next_page = json_response.get("hasNextPage")
for found_user in found_users:
"activeUsers":activeall_users,.append(found_user)
"inActiveUsers":inactive_users, if has_next_page:
"pageSize":page_size, # Recursive "offset":offsetsearch if next page
} post_request_json_bytes = json.dumps(request_body).encode("utf-8") post_request = request.Request(request_url, headers=request_headers)search_users(search_string, active_users, inactive_users, page_size, (offset + page_size))
except post_request.get_method = lambda: 'POST'error.HTTPError as e:
try:print(e.getcode())
def get_csv_file(file_name):
'''
post_request_open = request.urlopen(post_request, data=post_request_json_bytes)
Get CSV file by file name from Confluence temp directory
:param postfile_responsename: = post_request_open.read()
if post_response:the name of the file to get from Confluence temp directory
:return: None
'''
json_response = json.loads(s=post_response)
request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH}
request_url if= jsonCONFLUENCE_response:BASEURL + CONFLUENCE_USER_EXPORT_DOWNLOAD
request_parameters = {'fileName':file_name}
found_usersget_request = json_responserequest.get("users")
Request(url=request_url + "?" + parse.urlencode(request_parameters),
# Check for next page of users in order to search for more users headers=request_headers)
has_next_page = jsonget_responserequest.get("hasNextPage")
_method = lambda: 'GET'
try:
for found_user in found_users:get_request_open = request.urlopen(get_request)
get_response = get_request_open.read()
if all_users.append(foundget_userresponse)
:
if has_next_page:
with open(CONFLUENCE_CSV_FILE_NAME, 'wb') as csv_file:
post_search(search_string, active_users, inactive_users, page_size, (offset + page_size)csv_file.write(get_response)
except error.HTTPError as e:
print(e.getcode())
def postcreate_csv_csvfile(users_as_json):
'''
Send JSON content to CSV servlet for file generation
:param users_as_json: the Confluence users as JSON
:return: None
'''
users_as_json = users_as_json.encode("utf-8")
request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH}
request_url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_CSV_SERVLET
request_parameters = {'confluenceuserexport_download':'false','confluenceuserexport_users':users_as_json}
post_request = request.Request(url=request_url + "?" + parse.urlencode(request_parameters), headers=request_headers)
post_request.get_method = lambda: 'POST'
try:
post_request_open = request.urlopen(post_request, data=users_as_json)
post_response = post_request_open.read(read()
if post_response:
json_data = json.loads(post_response)
if post_responsejson_data and json_data.get("fileName"):
print(post_response.decode("utf-8 get_csv_file(file_name=json_data.get("fileName"))
except error.HTTPError as e:
print(e.getcode())
# Start searching
postsearch_searchusers(search_string=CONFLUENCE_USER_EXPORT_SEARCH_TERM,
active_users=CONFLUENCE_USER_EXPORT_ACTIVE_USERS,
inactive_users=CONFLUENCE_USER_EXPORT_INACTIVE_USERS,
page_size=CONFLUENCE_USER_EXPORT_PAGESIZE,
offset=CONFLUENCE_USER_EXPORT_OFFSET)
# Call CSV servlet for conversion
all_users_json = json.dumps(all_users)
postcreate_csv_file(users_as_json=all_users_json) |