Show me the code! – By Davanum Srinivas

January 4, 2012

Follow a user in Lotus Connections 3.0

Filed under: Uncategorized — Tags: , — Davanum Srinivas @ 3:35 pm

One main feature in Lotus Connections 3.0 is the asymmetric follow of someone (like twitter). The API documentation is here. Since sample code is always better here’s a HTTP GET python snippet to lookup the userid, given an email and then a quick HTTP POST to follow that user.

#!/usr/bin/python
import sys,urllib,urllib2,traceback,base64
from xml.dom import minidom

xml_data_header = """
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
   <category term="resource-follow" scheme="http://www.ibm.com/xmlns/prod/sn/type"></category>
   <category term="profiles" scheme="http://www.ibm.com/xmlns/prod/sn/source"></category>
   <category term="profile" scheme="http://www.ibm.com/xmlns/prod/sn/resource-type"></category>
   <category term="
""".strip()
xml_data_footer = """
" scheme="http://www.ibm.com/xmlns/prod/sn/resource-id"></category>
</entry>
""".strip()

if len(sys.argv) != 4: 
        print 'Usage: follow <userid> <password> <email-of-user-to-follow>' 
        sys.exit(1)

base64string = base64.encodestring('%s:%s' % (sys.argv[1], sys.argv[2]))[:-1]

def getUuidForUser(email):
	uri = "https://w3-connections.ibm.com/profiles/atom/profile.do?format=lite&email=%s" % email
	req = urllib2.Request(uri)
	req.add_header('Content-type','application/atom+xml')
	req.add_header('Authorization', "Basic %s" % base64string)
	dom = minidom.parse(urllib2.urlopen(req))
	element = dom.getElementsByTagNameNS('http://www.ibm.com/xmlns/prod/sn', 'userid')[0]
	return element.firstChild.data

def followUser(uuid):
	uri = 'https://w3-connections.ibm.com/profiles/follow/atom/resources'
	query_string_values = {'source': 'profiles', 'type'  : 'profile'}
	payload = '%s%s%s' % (xml_data_header, uuid, xml_data_footer)

	try :
		if query_string_values:
		    uri = ''.join([uri, '?', urllib.urlencode(query_string_values)])
		req = urllib2.Request(uri, data=payload)
		req.add_header('Content-type','application/atom+xml')
		req.add_header('Authorization', "Basic %s" % base64string)
		response = urllib2.urlopen(req)
		return response.read()
	except urllib2.HTTPError, error:
		return error.read()

print followUser(getUuidForUser(sys.argv[3]))
About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 132 other followers

%d bloggers like this: