The other day I wrote about how you can interact with Bluesky via your own Python code.
My goal behind that was to be able to cross-post between social media networks and from my own automations.
For example, perhaps on my retro games account I could post a "game of the day" which would pull details such as Computer and Video Games score, best sales rank, etc.
On Peakd, the site I mainly do my Hive blogging, these micro posts are called Snaps.
How they work is each day (or whenever there are enough posts) a new parent post is created. Each of our posts are then actually created as replies to that core post. We are really commenting rather than posting, and this is how they are preventing the Hive blog feeds from getting cluttered with an image and a comment that says "LOL" :)
We all love a good sh'tpost but we don't want them on the main feed, right!
This means although there is no official API that I can see, we can use the trusty Beem library that has been humming along since the steem days.
These are the imports we will need, starting with Hive because to post our reply we need to be authenticated.
from beem import Hive
from beem.discussions import Query, Discussions_by_blog as Discussions
from beem.comment import Comment
from beem.account import Account
Next we need to log in. If we are running interactively we can just ask for the Hive posting key:
wif = input("Enter your posting key: ")
hive = Hive(keys=wif)
account = Account("YOUR USER NAME HERE", blockchain_instance=hive)
If you are wanting to not interact then you will need some combination of environment variables or hive.wallet.unlock("YOUR WALLET PASSPHRASE")
You can see if you authenticated correctly and the information that gives you with a simple for each
loop:
for accountdetail in account:
print(accountdetail)
For our purposes just the name will suffice:
print(account.name)
(This comes in handy later)
Now, how to get the Snaps Container post?
Well, unofficially, I guess the next container will be the most recent post for the account that they use ('@peak.snaps'), right?
This does run the small risk that they might be creating a new container right as we are posting but I think we can live with that.
q = Query(limit=1, tag="peak.snaps")
posts = Discussions(q)
details = Comment(posts[0])
print('Title: ' + str(details.title))
print('Author: ' + details.author)
print('Category: ' + details.category)
print('Body: ' + details.body)
This gets the most recent post and outputs the details.
Of course once you know this is working you don't need to output all this stuff, it is just handy to ensure you are not replying to some random Hive users blog articles!
These are the info we actually need:
perm = details['authorperm']
comment = Comment(perm, hive_instance=hive)
comment.reply("Test from Beem",title="Test from Beem", author=account.name)
In order to reply, Hive needs a permalink of the post we are replying to.
For our reply to post, that is write to the blockchain, we also need an authenticated account, which is what we got earlier.
Finally we set the text of our Snap (though the title is not used in Snaps view), and we pull in our account name as the author.
That is it! We can post (text) to Snaps, hurrah!