Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • CONFLUENCE_USERNAME = Confluence administrator username

  • CONFLUENCE_PASSWORD = Confluence administrator password

  • CONFLUENCE_BASEURL = Confluence url eg. https://jira.yourdomain.com

  • CONFLUENCE_CSV_FILE_NAME = Name of the file to generate

To alter the search change these variables:

...

Expand
titleCreate CSV with Python 2...

Code Block
languagepy
# -*- coding: utf-8 -*-
'''
Creates a full Confluence user export as CSV
1. IMPORTANT: should be executed with Python 2.7 interpreter
2. Change the username, password and baseurl variables to match custom Confluence
3. Execution from terminal: python create_full_export_v1csv_python2_7.py
> confluence_users.csv
3.4. A new file named confluence_-users.csv iswill nowbe availablecreated '''
in this directory
'''
import urllib
import urllib2
import json
import base64

# ChangeCHANGE theseTHESE variablesVARIABLES
CONFLUENCE_USERNAME = "admin"
CONFLUENCE_PASSWORD = "admin"
CONFLUENCE_BASEURL = "http://localhost:1990/confluence"
CONFLUENCE_CSV_FILE_NAME = "confluence-users.csv"

# SearchSEARCH variablesVARIABLES
# 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 = 50

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/csvdownload"
JSON_MIME_TYPE = "application/json"
BASIC_AUTH = "Basic " + base64.b64encode('%s:%s' % (CONFLUENCE_USERNAME, CONFLUENCE_PASSWORD))

all_users = []


def postsearch_searchusers(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
    '''''
    header = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH}
    url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_SEARCH
    body = {"searchString":search_string,
            "activeUsers":active_users,
            "inActiveUsers":inactive_users,
            "pageSize":page_size,
            "offset":offset
            }
    json_body = json.dumps(body)
    post_request = urllib2.Request(url, json_body, header)
    post_request.get_method = lambda: 'POST'
    try:
        post_request_open = urllib2.urlopen(post_request)
        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:
                    all_users.append(found_user)
                if has_next_page:
                    postsearch_searchusers(search_string, active_users, inactive_users, page_size, (offset + page_size))
    except urllib2.HTTPError as e:
        print(e.getcode())


def postget_csv_file(usersfile_as_jsonname):
    '''
    Get CSV file Sendby file JSONname contentfrom toConfluence CSVtemp servletdirectory
    :param usersfile_as_jsonname: the name of the file to get from Confluence userstemp as JSONdirectory
    :return: None
    '''
    request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH}
    request_url = CONFLUENCE_BASEURL + CONFLUENCE_USER_EXPORT_CSV_SERVLETDOWNLOAD
    request_parameters = {'fileName'confluenceuserexport_download':'false','confluenceuserexport_users':users_as_json:file_name}
    postget_request = urllib2.Request(url=request_url + "?" + urllib.urlencode(request_parameters),
                                   headers=request_headers)
    postget_request.get_method = lambda: 'POSTGET'
    try:
        postget_request_open = urllib2.urlopen(postget_request)
        postget_response = postget_request_open.read()
        if post(get_response):
            with open(CONFLUENCE_CSV_FILE_NAME, 'wb') as csv_file:
    print(post            csv_file.write(get_response)
    except urllib2.HTTPError as e:
        print(e.getcode())


# Start searching
post_search(search_string=CONFLUENCE_USER_EXPORT_SEARCH_TERM,def create_csv_file(users_as_json):
    '''
    Send JSON  active_users=CONFLUENCE_USER_EXPORT_ACTIVE_USERS,
  content to CSV REST endpoint
         inactive_users=CONFLUENCE_USER_EXPORT_INACTIVE_USERS,
    :param users_as_json: the Confluence users as JSON
     :return:  page_size=CONFLUENCE_USER_EXPORT_PAGESIZE,
   None
    '''
    request_headers = {"Content-type": JSON_MIME_TYPE, "Accept": JSON_MIME_TYPE, "Authorization": BASIC_AUTH}
    request_url = CONFLUENCE_BASEURL + offset=CONFLUENCE_USER_EXPORT_OFFSET)CSV
  # Call CSV servlet for conversion
all_users_json post_request = jsonurllib2.dumpsRequest(all_users)
post_csv(all_users_json)

Expand
titleCreate CSV with Python 3...

Code Block
languagepy
# -*- coding: utf-8 -*-
'''
Creates a full Confluence user export as CSV
1. IMPORTANT: should be executed with Python 3 interpreter
2. Execution from terminal: python3 create_full_export_csv_python3.py > confluence_users.csv
3. A new file named confluence_users.csv is now available
'''
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"

# 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 = 50

CONFLUENCE_USER_EXPORT_SEARCH = "/rest/confluenceuserexport/1.0/search"
CONFLUENCE_USER_EXPORT_CSV_SERVLET = "/plugins/servlet/confluenceuserexport/admin/csv"
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 post_search(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,url=request_url,
                                   data=users_as_json,
                                   headers=request_headers)
    post_request.get_method = lambda: 'POST'
    try:
        post_request_open = urllib2.urlopen(post_request)
        post_response = post_request_open.read()
        if post_response:
            json_data = json.loads(post_response)
            if json_data and json_data.get("fileName"):
                get_csv_file(file_name=json_data.get("fileName"))
    except urllib2.HTTPError as e:
        print(e.getcode())


# Start searching
search_users(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)


all_users_json = json.dumps(all_users)
# Create CSV file with users
create_csv_file(users_as_json=all_users_json)

Expand
titleCreate CSV with Python 3...

Code Block
languagepy
# -*- 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)