{"id":924,"date":"2013-05-05T16:25:27","date_gmt":"2013-05-05T16:25:27","guid":{"rendered":"http:\/\/joelinoff.com\/blog\/?p=924"},"modified":"2013-05-05T16:26:54","modified_gmt":"2013-05-05T16:26:54","slug":"count-all-confluence-pages-in-python-using-xml-rpc-api","status":"publish","type":"post","link":"https:\/\/joelinoff.com\/blog\/?p=924","title":{"rendered":"Count all Confluence pages in python using XML-RPC API"},"content":{"rendered":"<p>I was recently asked how to count the total pages on a 4.2 Confluence server so I provided this python script that shows how to use the XML-RPC API to do it. The technique can be used for more than counting pages. The API provides all sorts of useful operations, like adding users. For more information on the available methods see this page: <a href=\"https:\/\/developer.atlassian.com\/display\/CONFDEV\/Remote+Confluence+Methods\" title=\"https:\/\/developer.atlassian.com\/display\/CONFDEV\/Remote+Confluence+Methods\">https:\/\/developer.atlassian.com\/display\/CONFDEV\/Remote+Confluence+Methods<\/a>.<br \/>\n<!--more--><br \/>\n[crayon lang=&#8221;python&#8221; toolbar=&#8221;always&#8221; title=&#8221;confluence_pages&#8221;]<br \/>\n#!\/usr\/bin\/env python<br \/>\nr&#8221;&#8217;<br \/>\nThis script shows how to use the XML-RPC API to access<br \/>\ninformation from a Confluence server.<\/p>\n<p>Here is how you might use it:<\/p>\n<p>$ confluence_pages.py<br \/>\nURL: http:\/\/confluence<br \/>\nUsername: myself<br \/>\nPassword: <secret><\/p>\n<p>Server Info<br \/>\n  baseUrl          : http:\/\/confluence:8090<br \/>\n  buildId          : 3284<br \/>\n  developmentBuild : false<br \/>\n  majorVersion     : 4<br \/>\n  minorVersion     : 2<br \/>\n  patchLevel       : 5<\/p>\n<p>Misc Info<br \/>\n  Active Users :   580<br \/>\n  Spaces       :   120<br \/>\n  Pages        :  7260<\/p>\n<p>&#8221;&#8217;<br \/>\nimport datetime<br \/>\nimport getpass<br \/>\nimport sys<br \/>\nimport xmlrpclib<\/p>\n<p>def get_credentials():<br \/>\n    &#8221;&#8217;<br \/>\n    Get the access credentials.<\/p>\n<p>    They are accessed positionally from the command line<br \/>\n    or from a prompt.<\/p>\n<p>    @returns a tuple of url, username and password<br \/>\n    &#8221;&#8217;<br \/>\n    url = None<br \/>\n    username = None<br \/>\n    password = None<\/p>\n<p>    if len(sys.argv) > 1:<br \/>\n        url = sys.argv[1]<br \/>\n        if len(sys.argv) > 2:<br \/>\n            username = sys.argv[2]<br \/>\n            if len(sys.argv) > 3:<br \/>\n                password = sys.argv[3]<\/p>\n<p>    if url is None:<br \/>\n        url = raw_input(&#8216;URL: &#8216;)  # ex. https:\/\/docs.tabula.com<br \/>\n    if username is None:<br \/>\n        username = raw_input(&#8216;Username: &#8216;)  # ex. jlinoff<br \/>\n    if password is None:<br \/>\n        password = getpass.getpass(&#8216;Password: &#8216;)<br \/>\n    return url, username, password<\/p>\n<p>def access_confluence(url, username, password):<br \/>\n    &#8221;&#8217;<br \/>\n    Access confluence and report some information.<br \/>\n    @param url      The URL of the server.<br \/>\n    @param username Login username.<br \/>\n    @param password Login password.<br \/>\n    &#8221;&#8217;<br \/>\n    server = xmlrpclib.ServerProxy(url + &#8216;\/rpc\/xmlrpc&#8217;)<br \/>\n    token = server.confluence2.login(username, password)<\/p>\n<p>    # Server<br \/>\n    info = server.confluence2.getServerInfo(token)<br \/>\n    now = datetime.datetime.now()<br \/>\n    print<br \/>\n    print &#8216;Confluence Pages Report &#8216;+now.strftime(&#8216;%Y-%m-%d %H:%M:%S&#8217;)<br \/>\n    print<br \/>\n    print &#8216;  Server Info&#8217;<br \/>\n    maxlen = 0<br \/>\n    for item in sorted(info):<br \/>\n        maxlen = max(len(item), maxlen)<br \/>\n    for item in sorted(info):<br \/>\n        val = info[item]<br \/>\n        print &#8216;    %-*s : %s&#8217; % (maxlen, item, val)<\/p>\n<p>    # Misc<br \/>\n    spaces = server.confluence2.getSpaces(token)<br \/>\n    users = server.confluence2.getActiveUsers(token, True)<br \/>\n    print<br \/>\n    print &#8216;  Misc Info&#8217;<br \/>\n    print &#8216;    %-12s : %5d&#8217; % (&#8216;Active Users&#8217;, len(users))<br \/>\n    print &#8216;    %-12s : %5d&#8217; % (&#8216;Spaces&#8217;, len(spaces))<\/p>\n<p>    num_pages = 0<br \/>\n    for space in spaces:<br \/>\n        space_key = space[&#8216;key&#8217;]<br \/>\n        num_pages += len(server.confluence2.getPages(token, space_key))<br \/>\n    print &#8216;    %-12s : %5d&#8217; % (&#8216;Pages&#8217;, num_pages)<br \/>\n    server.confluence2.logout(token)<br \/>\n    print<\/p>\n<p>def main():<br \/>\n    &#8221;&#8217;<br \/>\n    Run program.<br \/>\n    &#8221;&#8217;<br \/>\n    url, username, password = get_credentials()<br \/>\n    access_confluence(url, username, password)<\/p>\n<p>if __name__ == &#8216;__main__&#8217;:<br \/>\n    main()<br \/>\n[\/crayon]<br \/>\nNote that this script has very weak option handling and, as such, is not suitable for production.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently asked how to count the total pages on a 4.2 Confluence server so I provided this python script that shows how to use the XML-RPC API to do it. The technique can be used for more than counting pages. The API provides all sorts of useful operations, like adding users. For more &hellip; <a href=\"https:\/\/joelinoff.com\/blog\/?p=924\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Count all Confluence pages in python using XML-RPC API<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[7,16],"tags":[],"_links":{"self":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/924"}],"collection":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=924"}],"version-history":[{"count":7,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/924\/revisions"}],"predecessor-version":[{"id":931,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/924\/revisions\/931"}],"wp:attachment":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}