Buffing up Hive Communities with Python Automation

in HiveDevs2 years ago

Buffing up Hive Communities with Python Automation

Earlier this week, I started spending more time on Hive front ends. My attention gravitated toward the HivePizza on-chain community, and I decided to buff it up. This community exists on the usual Hive front-ends, but it also has it’s own breakaway community site (based on the template created by @psorigins), found here: https://writing.pizza/.

Screenshot of https://writing.pizza - Ecency based community front-end.

Screenshots of PeakD community details, widescreen and mobile layouts

Exploring Hive Community Roles Feature

My sprucing-up activities led me to investigate the community roles feature. Communities have 5 different roles: Owners, Admins, Mods, Members, and Muted. Community admins and moderators have the ability to add members and assign titles to members. The titles are customizable text strings that PeakD and Hive.blog display on posts and comments within the community. As a bonus for the creative minds, PeakD renders unicode emojis like 🤖, contained inside title strings.

The role/title system maps closely to what we already have set up in the HivePizza discord guild. The titles like: Champion, Za Baron, Zupervisor, etc. were a natural fit. I just needed some code to set the title strings for a few hundred Hive accounts.

Setting a role and setting a user title are simple Custon_JSON operations that are broadcast/signed by a key for an account with the community Mod role. For efficiency, ideally, these operations could be batched together into fewer custom_json ops; unfortunately, the protocol does not seem to support multiple role/title changes in a single custom_json. Each operation needs to be broadcast separately.

The JSON Protocol

Let’s take a look at the JSON strings. The format for setUser and setUserTitle is straightforward. To add a new member, use the setRole one which specifies the community account name, the account to receive the role, and the name of the role. After adding the member, set their title by specifying the community account name, the account to receive the title, and the string for the title.

In the examples below, the hive-185582 is the account for the HivePizza community. These custom_json operations need to have the ID ‘community’. Also, posting authority/key is a sufficient privilege for broadcasting these operations.

JSON for setting a community role:

JSON for setting a community title:
["setUserTitle",{"community":"hive-185582","account”:”HiveAccountName,”title”:”Some Title”}]

Python Code Example

After understanding the JSON format, Hive dev tools can be used to programmatically assign roles and titles. Using the python-based beem library it looks like this:

from beem import Hive
from dotenv import load_dotenv
import os

# secrets: posting key stored in a .env file
HIVE_API_NODE = 'https://api.hive.blog’
HIVE = Hive(node=[HIVE_API_NODE], keys=[os.getenv('POSTING_KEY')])

MOD_ACCOUNT_NAME = ‘moderator account name’ # name of the moderator account, matching posting key
COMMUNITY_ACCOUNT_NAME = ‘hive-185582’ # name for community account i.e. hive-12456

user = ‘Some Hive User’ # name of user to add as member
title = ‘Some Title’ # title to set for member

# add user as a member
jsondata = '["setRole",{"community”:”%s”,”account":"%s","role":"member"}]' % (COMMUNITY_ACCOUNT_NAME , wallet)

HIVE.custom_json('community', jsondata, required_posting_auths=[MOD_ACCOUNT_NAME])

# add title for member
jsondata = '["setUserTitle",{"community”:”%s,”account":"%s","title":"%s"}]' % (COMMUNITY_ACCOUNT_NAME, user, title)

HIVE.custom_json('community', jsondata, required_posting_auths=[MOD_ACCOUNT_NAME])

Using this code as a foundation, you can throw it into a for loop and feed in a list of account names and desired titles. beem will happily crank through the list assigning the titles, and this will save you a ton of mouse clicking and keystrokes!

Thanks for reading my post! And many thanks for voting for my Hive witness: @pizza.witness


@hivetrending! The Hive.Pizza team manually curated this post.

You can now send $PIZZA tips in Discord via tip.cc!