Sorry, I have been out for a while, and this one isn't bee themed as I am really digging the Borg theme at the moment. But I present to you:
A comment upvote bot
This bot was created because my simple monitor bot seemed to be the most popular article so far. Source can be found at GitHub If you have other things you would like me to attempt please, ask away!
#!/usr/bin/env python3
import os
import re
from beem import Hive
from beem.account import Account
from beem.blockchain import Blockchain
from beem.comment import Comment
from beem.instance import set_shared_blockchain_instance
from beem.utils import construct_authorperm
posting_key = os.environ["BOT"]
REGEX = '(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9]+)'
botname = "thecrazygm"
weight = 100
hive = Hive(node=['https://api.hive.blog'], keys=posting_key)
set_shared_blockchain_instance(hive)
chain = Blockchain()
followees = Account(botname).get_following()
def summon_upvotebot():
print(f'[Starting up]')
while True:
try:
for post in chain.stream(opNames="comment", threading=True, thread_num=5):
mentions = re.findall(REGEX, post["body"])
comment = Comment(post)
perm = comment.authorperm
parent = construct_authorperm(
Comment(perm).parent_author, Comment(perm).parent_permlink)
author = post['author']
if Comment(perm).is_comment:
if botname in mentions:
print(
f'[{author} just mentioned {botname} in {perm} in reply to {parent}]')
if author in followees:
Comment(parent).upvote(
weight=weight, voter=botname)
print(
f'[{botname} voted {weight}% on {parent} per {author}\'s request]')
else:
print(
f'[{author} tried to summon {botname} but is not in the whitelist]')
except Exception as error:
print(repr(error))
continue
if __name__ == '__main__':
summon_upvotebot()
Now this takes in consideration that you have an env variable called BOT
set to your posting key e.g. export BOT=mypostingkeygoeshere
from the Linux/Mac cli (not even sure how this is done in Windows, someone care to share?)
This is a comment bot, looking for your bot name in a comment to upvote the parent comment and only if that person is in the bot's following list. This was done for two reasons:
- to create a simple 'whitelist'
- to show some of the other ways you can use a monitor bot for neat things
Normally we see another comment saying (this has been upvoted by blahblah, but I feel that is spam)
Until next time,
p.s. I'm still feeling quite sick as why this is so brief and doesn't walk through the code with explanations.
Michael Garcia a.k.a. @TheCrazyGM
Always enjoy seeing people sharing their beem/hive code. I am teaching myself Python and would like to eventually write python code do do things like this on the HIVE blockchain.
@mytechtrail, i started this as a "Learn Python and Hive by example" series, and am planning to continue to do so. (Even writing up a proposal) if you look at some of my earlier posts I'm still finding my footing on style, approach etc. If you have any questions about any of it, please let me know, or suggestions, criticism, constructive or otherwise :)
I too want to learn how to do this, thank you for sharing.
I have created a community Learning Python. Would you be interested in posting there?
Nice!
I know just enough to use google & break things. 😁 When I started working as a network admin programmers were the bane of my existence. Today I wish I'd gotten more involved with programming.
I'd like to take people from just barely to understanding this. Any recommendation on how to achieve that goal? As I'm still trying to find my footing in the style/tone/method of how I go about this.
I don't really have recommendations for you. I do somewhat understand the basics of programming. Though BASIC is the keyword there. lol. I started with BASIC on an atari 500 xl. While I was never proficient I found I learned mostly by repetition.
I've skimmed over several of your tutorial posts and look forward to finding time to give them a try. When I do I'll give you some more feedback.
@patrice
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!