Best way to get subpart only of page content with Confluence API?

Marchand September 3, 2019

Hi,
Using Confluence API (not plugins), I want to get subpart only of page content (not entire page) in the aim to update it later.
I tried with page properties but I can’t get only unique property but a list of all pages with properties with next command (/rest/masterdetail/1.0/detailssummary/lines?cql=type=page&spaceKey=XXX&headings=XXX).
What is the best way to put data in the page structure and find easily with Confluence API?

Thank in advance for your answer.

1 answer

1 accepted

1 vote
Answer accepted
jira guy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2019

Use the python module from here 

https://pypi.org/project/atlassian-python-api/

It's easy to use. It has useful examples too. 

Stan Ry
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2019

Hi, thanks for the suggestions. Would you please provide an example of patching a specific part of the page, e.g. adding an admonition to the page? Thank you.

Marchand September 3, 2019

Thanks for the explanation.

Currently I am on PHP but I will look for Python because the API actually has 2 interesting methods (get_page_property and set_page_property)...

jira guy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2019

 

You would have to import packages and add authentication as shown in above docs. 

Okay. Here is the example of getting the page content and replacing the content. I am removing {group3} references in the page to nothing. 

def rest_page_update(space,page_id,title):
content = confluence.get_page_by_id(page_id,expand='body.storage').get('body').get('storage').get('value')
body = re.sub('{group3}','', content, flags = re.M)
confluence.update_page(None, page_id, title, body, type='page')
Like # people like this
Marchand September 3, 2019

Thank you, that's what I wanted to do.
I will test on my side if everything works as I want in Python.
Regards,

Stan Ry
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2019

@jira guy 

Fantastic! Thanks a bunch. Could you please post a working sample that could be used to get started. 

I mean a working sample that shows how to auth on confluence and call your 

rest_page_update

API. Kinda new to Python, so any short sample that would work from scratch is highly appreciated.

Thank you. That replacement sample is really cool: so simple and demonstrative.

jira guy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2019

here you go...

import fileinput
import re
import os
import hashlib
from collections import defaultdict
import shutil
from atlassian import Confluence
confluence = Confluence(
url='https://ursite.url.com',
username='username',
password='*****')
def rest_page_update(space,page_id,title):
content = confluence.get_page_by_id(page_id,expand='body.storage').get('body').get('storage').get('value')
body = re.sub('{group3}','', content, flags = re.M)
confluence.update_page(None, page_id, title, body, type='page')
Like Stan Ry likes this
Stan Ry
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2019

Whoaa, thank you @jira guy That's what I am hugely missing from Atlassian's docs. Samples. Any clue on how could I skip the SSL check?

I get the following error:

Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]

When invoking `requests` component, I used to employ the following line to skip SSL verification:

requests.packages.urllib3.disable_warnings()

How is it done with `atlassian` component?

It feels like it's done like that

confluence = Confluence(
url=WIKI_WEB,
username=userName,
password=userPassword,
verify_ssl=False)

and like if setting `verify_ssl` to False is enought. But. Then I get:

TypeError: 'NoneType' object is not subscriptable

 when processing this line:

confluence.update_page(None, page_id, title, body, type='page')

Any clues?

Thank you!

Marchand September 10, 2019

Hi,
I tried with Python Confluence API:
confluence = self.get_confluence()
status = confluence.page_exists(space=self.page_space, title=self.page_title)
if status:
self.page_id = confluence.get_page_id(self.page_space, self.page_title)
print(confluence.get_page_properties(self.page_id))
Problem is same as Web Service: Existing page properties from Confluence Web site are not visible by API...
But when I create a property by API:
property_data1 = {"key": "1234",
"value": {"Test API ": "en cours"}, "version": {"number": 1, "minorEdit": 'false'}}
prop = confluence.set_page_property(self.page_id, data=property_data)
It work fine in database, result from get_page_properties is:
"results": [
{
"id": "2955040",
"key": "1234",
"value": "Test API ",
"version": {
"when": "2019-09-09T17:19:18.533+02:00",
"message": "",
"number": 1,
"minorEdit": false,
"hidden": false
},
"_links": {
"self": "https://confluence.test/rest/api/content/2954600/property/1234"
},
"_expandable": {
"content": "/rest/api/content/2954600"
}
},
{
"id": "2955032",
"key": "example-Property001",
"value": [
"Test API ",
"en cours"
],
"version": {
"when": "2019-09-09T17:01:01.192+02:00",
"message": "",
"number": 1,
"minorEdit": false,
"hidden": false
},
"_links": {
"self": "https://confluence.test/rest/api/content/2954600/property/example-Property001"
},
"_expandable": {
"content": "/rest/api/content/2954600"
}
}
],
"start": 0,
"limit": 10,
"size": 2,
"_links": {
"self": "https://confluence.test/rest/api/content/2954600/property",
"base": "https://confluence.test",
"context": ""
}
}
Unfortunately, API page properties are not visible on Confluence Web site…
I tried changing profile as admin for the same result…
Do you have some idea of what happen ?
Thank in advance for your answer.
Regards,

Like Stan Ry likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events