I use markdown since years to create my articles or most of documentation stuff.
It’s really cool to edit the text files on all of my devices, synchronize them fast through various internet services and create HTML, PDF or even DOCX files out of this sources.
Until last week i converted the markdown sources with pandoc and pasted the html code to the different pages, where i need them. So i copy and paste it to IBM Connections, Evernote or WordPress . On WordPress i use a plugin now to render the markdown textes, but for IBM Connections i didn’t found a way.
So why not using the IBM Connections API to post html, or even convert the file on the fly and put the html automatically?
Posting to IBM Connections Blogs with Python
You can read a lot on the IBM Connections API in the [official documentation](http://www-10.lotus.com/ldd/lcwiki.nsf/xpAPIViewer.xsp?lookupName=IBM Connections 5.0 API Documentation#action=openDocument&res_title=IBM_Connections_API_overview_ic50&content=apicontent), i’m not a developer so i need normally practical examples to use this.
How to connect to IBM Connections REST API with Python?
First we need the URL to connect to the Blogs API:
url = 'https://greenhouse.lotus.com/blogs/stoeps/api/entries'
In this case “stoeps” is the blog handle, the blog where i want to add my post. You find the blog handle when you open the blog through your browser and check the url:
Now we need to authenticate, Connections API can be used with basic authentication, so we create a base64 encoded string of username and password:
import base64
encodedstring = base64.encodestring('christoph.stoettner@stoeps.de'+":"+'myCoolPassword')[:1]
auth = "Basic %s" % encodedstring
To post we have to use a XML File or XML String, which contains a title and body of our post, the REST service needs a Content-type of application/atom+xml:
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:snx="http://www.ibm.com/xmlns/prod/sn">
<title type="text">That's the title</title>
<content type="html"><![CDATA[<p>and some text for the body.</p>]]></content>
</entry>'''
Now let’s connect to the REST service with urllib2 and post the xml string.
import urllib2
request = urllib2.Request( url )
request.add_data( xml ) # add_data changes Method to POST
request.add_header( 'Content-type', 'application/atom+xml' )
request.add_header( 'Authorization', auth )
response = urllib2.urlopen( request )
# let's check return code
if response.code = 201:
print 'Post successfully created'
Some more
We have seen that we can post a xml with title and body. There are more possible ways to get a html string for our post.
I changed the xml variable to fill two variables with title and body.
postTitle = 'A cool API post to blogs'
postBody = '''<p>This is the body of our Blogs post, we can use html tags here</p>
<h2>Fill text to show more content</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo posuere ante, ut faucibus enim sagittis ut. Fusce scelerisque arcu quis mattis fringilla. Pellentesque a est enim. Nam congue sem eget augue porttitor semper. Fusce luctus sit amet ligula sit amet viverra. Cras pulvinar arcu eget velit volutpat, in condimentum nunc luctus. Vivamus vestibulum ante et nisl venenatis consequat. Aliquam ut augue nec lectus sodales eleifend eleifend nec orci.</p>'''
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:snx="http://www.ibm.com/xmlns/prod/sn">
<title type="text">' + postTitle + '</title>
<content type="html"><![CDATA[' + postBody + ']]></content>
</entry>'''
You see you can add html code to the variable postBody. You can read from a html file with:
postBody = open('post.html').read()
Or you can convert a markdown file to html:
import markdown2
markdownFile = 'post.md'
postBody = markdown2.markdown_path( markdownFile )
In my script here i added a function to read the first line of the markdown to set it as title and i added a function to find images integrated in the article, upload them to stoeps/api/media and replace the img src= tag.
So this post is completely written in markdown and i posted it through python to Greenhouse. Here is the link to the post .