Skip to content

Instantly share code, notes, and snippets.

@prb112
Created January 30, 2014 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prb112/8708015 to your computer and use it in GitHub Desktop.
Save prb112/8708015 to your computer and use it in GitHub Desktop.
shows how to grab a community data into CSV form. you can then use the spreadsheet to reconcile who is missing from your community
# DEMONSTRATION PURPOSES CODE ONLY
# Run
# - python demo.py EMAIL_LOGIN PASSWORD COMMUNITY_UUID
# demo.py
# Demonstrates the process of getting information from the IBM SmartCloud for Social Business
# 1 - access to the contacts lists
# 2 - access to the community membership list
# 3 - from an access list converting it to a comma separate value
import sys
import urllib
import urllib2
import json
import requests
import base64
import xml.etree.ElementTree as ET
# Gets the Updates from the Server
# na / ca / ap should match your regional smartcloud center
contacts_url="https://apps.na.collabserv.com/lotuslive-shindig-server/social/rest/people/@me/@all"
toplevel_url="https://apps.na.collabserv.com/"
community_url="https://apps.na.collabserv.com/communities/service/atom/community/members"
# Arguments:
uname = sys.argv[1]
upass = sys.argv[2]
communityUuid = sys.argv[3]
debug=1
if debug != 1:
print "--------------------------------------------"
print "- Arguments passed in: "
for arg in sys.argv:
print arg
print "--------------------------------------------"
print
if debug != 1:
print "--------------------------------------------"
print "- Makes Request "
# Generates the Request and The Top Level Handlers
# http://docs.python.org/release/2.5.2/lib/urllib2-examples.html
# http://docs.python.org/2/howto/urllib2.html
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, toplevel_url, uname, upass)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
# Requests the Contact List and Gets the data into CSV Format
response_data = ""
try:
response = opener.open(contacts_url)
response_data = response.read()
if debug != 1:
print response_data
except urllib2.HTTPError, e:
print "Error in HTTP Request" #e.headers
if debug != 1:
print "--------------------------------------------"
jsonData = json.loads(response_data)
entries = jsonData['entry']
for entry in entries:
print entry['displayName'] + ',' + entry['emailAddress'] + ',' + str(entry['objectId'])
if debug != 1:
print "" + json.dumps(entry)
#-----------------------------------------------------------------
# Requests a Specified Community and then Extracts the details
# URL: http://docs.python-requests.org/en/latest/user/quickstart/
ET.register_namespace('a', 'http://www.w3.org/2005/Atom')
ET.register_namespace('opensearch', 'http://a9.com/-/spec/opensearch/1.1/')
ET.register_namespace('snx', 'http://www.ibm.com/xmlns/prod/sn')
ET.register_namespace('app', 'http://www.w3.org/2007/app')
params = {'communityUuid' : communityUuid }
params_encoded = urllib.urlencode(params)
userPass = uname + ":" + upass
base64pass = base64.b64encode(userPass)
headers = { 'Content-Type' : 'atom/xml',
'Authorization' : 'Basic ' + base64pass }
access_url = community_url + '?' + params_encoded
# Looking for https://apps.na.collabserv.com/communities/service/atom/community/members?communityUuid=45de91c4-7e8c-404c-9896-e9f521e51c7d
if debug != 1:
print access_url
r=requests.get(access_url, headers=headers)
if debug != 1:
print r.text
dom = ET.fromstring(r.text )
# Gets the Children
children = dom.findall("{http://www.w3.org/2005/Atom}entry")
for node in children:
if debug != 1:
print node.tag
snxs = node.findall('{http://www.w3.org/2005/Atom}contributor')
for snx in snxs:
o_userid = ""
o_name = ""
userids = snx.findall('{http://www.ibm.com/xmlns/prod/sn}userid')
for userid in userids:
o_userid = userid.text
names = snx.findall('{http://www.w3.org/2005/Atom}name')
for name in names:
o_name = name.text
print o_name + "," + o_userid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment