Hive tools #2 - Python Bot to vote on favorite author's comments

in StemSocial9 months ago

image.png

Hi,
a day without creating a bot, is a day lost :D

Today I would like to introduce a tool that allows you to vote on the comments of a particular user.
The need came from me, and I did not have such an alternative available on the "market", so I wrote myself for my own needs. (Or I'm not aware of :D )
The principle of operation is simple:

in settings.ini you set

[DEFAULT]
minPostLifeInMinutes = 5 <- miminal life of the comment
checkSecondsInterval = 5 <- every how long it should check the user's written comments
postingKey = xyz <- posting key for voter account
voterName = buddy <- the name of the voting account
weight = 100 <- voting power from 1 to 100
authorToVoteComment = grzegorz2047 <- whose account should check if a vote was made

I took out as many variables as possible that can be modified.

The bot can be fired up even on your local computer or any virtual private server with linux.
The requirements to run, are:

  1. python
  2. pip - access to a repository of python packages
  3. in the case of Linux, "screen" is also useful.

Once the requirements are met, the following command is executed to download the required libraries:

pip install -r requirements.txt

and then to run the script we call the command console command:

python index.py

The way bot works is pretty simple. When an author posts a comment somwhere, algorithm will look for last 20 comments and check if between 5-7 minutes there was any comment, if yes then like that comment. This can be used in any way you want or modify as you like :)
The source code of the script looks like this:

import pprint
from pick import pick
# initialize Hive class
from beem import Hive
from beem.discussions import Query, Discussions
from datetime import datetime
import configparser
import schedule
import time
from beem.account import Account
from beem.vote import ActiveVotes
from beem.transactionbuilder import TransactionBuilder
from beembase.operations import Vote
import getpass

h = Hive()
q = Query(limit=2, tag="")
d = Discussions()
config = configparser.ConfigParser()
config.read('settings.ini')

postingKey = config['DEFAULT']['postingKey']
author = config['DEFAULT']['authorToVoteComment']
voter = config['DEFAULT']['voterName']
weight = int(config['DEFAULT']['weight'])

posts = d.get_discussions('created', q, limit=2)
client = Hive('https://api.hive.blog/')


def vote(voter, author, permlink, weight):
  
  
  try:
    tx = TransactionBuilder(blockchain_instance=client)
    tx.appendOps(Vote(**{
      "voter": voter,
      "author": author,
      "permlink": permlink,
      "weight": int(float(weight) * 100)
    }))

    tx.appendWif(postingKey)
    signed_tx = tx.sign()
    broadcast_tx = tx.broadcast(trx_id=True)

    print("Vote cast successfully: " + str(broadcast_tx))
  except Exception as e:
    print('\n' + str(e) + '\nException encountered.  Unable to vote')

def get_comments_to_vote(author, voter):
  # 5 comments from selected author
  q = Query(limit=20, start_author=author) 

  # get comments of selected account
  comments = d.get_discussions('comments', q, limit=20)
  minPostLifeInMinutes = int(config['DEFAULT']['minPostLifeInMinutes'])
  maxMinutes = 7
  now = datetime.now().astimezone()
  commentsToVote = []
  # print comment details for selected account
  for comment in comments:
    comment_created_time = comment['created']
    diff = now - comment_created_time
    minutes = divmod(diff.total_seconds(), 60) 
    minutes = minutes[0]

    if minutes >= minPostLifeInMinutes and minutes < maxMinutes:
      votes = comment['active_votes']
      for vote in votes:
        if vote['voter'] != voter:
          commentsToVote.append(comment)
          break
  return commentsToVote


def main():
  print("I'm on my duty, sir!")

  commentsToVote = get_comments_to_vote(author, voter)
  number_of_comments_to_vote = len(commentsToVote)
  if number_of_comments_to_vote == 0:
    print("No comments to vote")
  else:
    print(number_of_comments_to_vote)


  for commentToVote in commentsToVote:
    permlink = commentToVote['permlink']
    vote(voter, author, permlink, weight)


checkSecondsInterval = int(config['DEFAULT']['checkSecondsInterval'])
schedule.every(checkSecondsInterval).seconds.do(main)
while True:
    schedule.run_pending()
    time.sleep(1)

All the files are available on github.

If you have any ideas for creating a useful tool (BOTH not too time consuming), I'd be happy to look at those ideas and try to implement it

Sort:  

Congratulations @grzegorz2047! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Happy Birthday to the Hive Blockchain

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.