I've tried using the Python version of the Confluence API module, so I'll introduce it.
For those of you who arrived at this article thinking that there is a python module for Confluence.
As of December 2017, the repository that was in Bitbucket at the time of writing this article was migrated to GitHub. It will be the following repository.
As I moved to GitHub, I feel that it has become easier to check for updates and issue pull requests!
Python API to Confluence
--Package (pypi): https://pypi.python.org/pypi/confluence --Documentation: Python confluence module Documentation (via Sphinx) --Repository: https://bitbucket.org/phoebian/confluence
I'll put it in with pip ~ (I'm sorry for the omission ...)
% sudo pip install confluence
Password:
Downloading/unpacking confluence
Downloading confluence-0.1.tar.gz
…
Successfully installed confluence
Cleaning up...
The authentication information can be omitted by creating a configuration file called config.ini. This time, I will specify the parameters when creating the object.
The target Confluence is Confluence for Jenkins.
python
>>> from confluence import Confluence
>>> conf = Confluence(profile=None, url="https://wiki.jenkins-ci.org", username="xxx", password="xxxx")
>>> o = conf.getPage("Jenkins study session","JA")
>>>
>>> print o['title']
Jenkins study session
>>>
>>> print o['content'][0:100]
h2.Jenkins study session
h3.Tokyo
* [1st Hudson Study Session|Hudson study session]
* [The 2nd Jenkins Study Group]
* [The 3rd Jenkins Study Group]
* [4th Jenkins
>>> o['id']
'58000672'
>>>
Confluence for Jenkins seems to be Version 3.4.7, but it was ok for reading.
The original package (https://pypi.python.org/pypi/confluence) is mainly Page, not for posting / updating Blogs, only basic ones.
In the above sample, if you change the content part of the Page object and set it to conf.storePageContent (o), it will be overwritten and updated. If there is no id, it will be a new registration.
Well, since the original Confluence API is simple, I put the one corresponding to the method for Blog in my repository. (I also added a method to force another specified user to watch the completed page)
Click here for a sample.
#Create an object for posting
newPost = {}
newPost['content'] = content
newPost['title'] = title
newPost['space'] = '~myself'
newPost['author'] = 'My login account'
#Register as Blog
res = conf.storeBlogEntry(newPost)
print res['id']
#Attach tags (page),Blog common)
conf.addLabelByName(u'Advent calendar',res['id'])
#Let me watch the content that I was forced to do (page),Blog common)
conf.watchPageForUser(res['id'], "The account of the person you want to watch")
There are some methods that are not implemented in XML-RPC / SOAP of Confluence API, but I will introduce them a little because they can be operated using REST.
On the command line, you can authenticate with your account-> like the specified content by doing the following.
bash
curl https://URL of confluence/rest/likes/1.0/content/__ID__/likes -X POST --user user:passwd
python
import requests
# res['id']Is the content ID
url = "https://URL of confluence/rest/likes/1.0/content/%s/likes" % res['id']
requests.post(url, auth=(account,password))
Good! Withdrawal is OK if you change it to dislike.
Recommended Posts