Skip navigation

python.png

I am not a developer by trade, but I do enjoy hacking some code to make things a little more efficient. Hence the reason I have Optimizer as my WorkType (Archived). I don't have any experience with web development which actually sucks since our community is built on JiveX; however, feel free to contact me if you have interest in schooling me on a few things.   I have a background in Geographic Information Systems (GIS) where I have mainly used Python. So when I went looking for a way to do a few things programmatically, I went to the language with which I was most comfortable.

 

I hardly found any examples to get me going until I stumbled upon the Jive REST Rest API v3.8 → Announcement service which provided a basic Python example. From that example I was able to figure our how to set up the URL and authentication as seen below:

 

import requests, json, base64

uri = "/api/core/v3/places"
base_url = "https://community.com"
url = base_url + uri

#Define headers
user = "AdminUser"
password = "AdminPassword"

auth = "Basic " + base64.encodestring('%s:%s' % (user, password)).replace("\n","");
headers = { "Content-Type": "application/json", "Authorization": auth }

req = requests.get(url, headers=headers )




 

With that in my pocket, I focused on two tasks that I had an immediate need for:

 

  • Remove all members from a group
  • Remove all invites from a group

 

I found the best way to archive a group was to set it as secret and remove all members. I quickly learned that manually removing 20 members at a time (limited by the page view) was a pain. Especially when the group had over 1000 members and over 7000 outstanding invites. I broke the task of removing all members into two basic steps:

 

  1. Create a list of group members.
  2. Loop through list and remove each member in list.

 

About 60 lines of code later I had a simple way to remove all members from a group.

 

import json, base64, requests, time

group = "1000"
placeID = "1000"
maxReturn = 100 #Max number of members to process per loop. 100 is max.
totalMembers = 100 #Number of members in group. Loops through members based upon maxReturn value untill all invites are gotten.
start = 0 #startIndex
memberList = []
name = ""

#Define Headers
user = "username" #User must have social group permissions
password = "password"

auth = "Basic " + base64.encodestring('%s:%s' % (user, password)).replace("\n","");
headers = { "Content-Type": "application/json", "Authorization": auth }

while (start < totalMembers):
    #Group members uri
    uri = "/api/core/v3/members/places/{}?count={}&startIndex={}".format(placeID,maxReturn,start)
    base_url = "https://community.com"
    url = base_url + uri
    start += maxReturn

    try:
        #Create list of member IDs
        req = requests.get(url, headers=headers)
        data = req.content

        #Remove security header
        data2 = data.replace("throw 'allowIllegalResourceCall is false.';", "")
        #Load json with request text
        jsonData = json.loads(data2)

      
        if jsonData["list"]:
            if name != "":
                pass
            else:
                name = jsonData["list"][0]["group"]["name"]
                name2 = name.encode('ascii', 'replace')
                print "Group: {}".format(name2)
            for dataItem in jsonData["list"]:
                if dataItem["state"] == "member":
                    memberList.append(dataItem["id"])
        else:
            print "No members exist for group: {}".format(group)
    except:
        pass
    print "Removing {} members".format(len(memberList))

nMembers = len(memberList)
remaining = 0
for m in memberList:
    uri2 = "/api/core/v3/members/{}".format(m)
    url2 = base_url + uri2
    req2 = requests.delete(url2, headers=headers)
    remaining = remaining + 1
    print "Removed member {} of {}".format(remaining, nMembers)
  
print "Removed all members"










 

 

There is so much that I can do with Python  and the Rest API that makes my life just a little easier. You can see an updated list of Examples using Python and Jive Rest API. Sure it's not as pretty as a custom web app, but it does get the job done in a more efficient way!

Filter Blog