Steemautomated: 0.0.3 - Voting Is Live! Added Server Side Voting Application

in utopian-io •  7 years ago 

Screenshot 2018-07-24 18.50.48.png

Repository

https://github.com/Juless89/steemautomated

Commit request

https://github.com/Juless89/steemautomated/commit/8f6e256a7b9688252941767772cb6746ccd0e4e1

Website

https://steemautomated.eu/

New Features

Voting is now live

I created a server side version of the standalone voter that uses a MySQL database and Steemconnect authentication. The flow for a vote is as follows:

Every block a list of authors to be upvoted is fetched from the server.

# Fetch author to be upvoted list
self.upvote_list = []
for author in self.db.get_authors():
    self.upvote_list.append(author[0])

The blocks transactions are scanned for comment which are then processed

for transaction in block['transactions']:
    if transaction['operations'][0][0] == 'comment':
        comment = transaction['operations'][0][1]
        self.process_comment(comment)

The comment is verified to be a new post and the author is verified to be in the upvote list. If so post is send to the queue.

def process_comment(self, comment):
    parent_author = comment['parent_author']
    author = comment['author']

    if parent_author == '' and author in self.upvote_list:
        permlink = comment['permlink']
        if self.verify_post(author, permlink):
            print(f'\n\n{self.timestamp} Block: {self.block}')
            print(f"New post\nAuthor: {author}")
            self.add_to_queue(author, permlink)

For each author all voting rules are retrieved and checked for their specific rules. If the daily limit has not been reached than the post can be added to the queue. When the delay is 0 a direct vote is casted. Posts are added to both the queue and the log. The log is used to determine the daily posts an author has already made. As it is possible to make a new post while there is already a post in the queue. In this instance just checking the log would retrieve an incorrent number.

def add_to_queue(self, author, permlink):
    for vote in self.db.get_votes(author):
        voter, weight, limit, delay = vote
        vote_log = self.db.get_vote_log(voter, author, self.timestamp)
        print(f"\nVoter: {voter}\nWeight: {weight}\nLimit: " +
              f"{limit}\nDelay: {delay}")

        if delay != 0:
            self.db.add_to_queue(
                author, voter, weight, limit, delay,
                permlink, self.timestamp,
            )
        elif len(vote_log) < limit:
            self.db.add_to_log(
                author, voter, permlink,
                weight, self.times,
                )
            self.vote(voter, author, permlink, weight)

To perform a vote first the access token is checked to still be valid, if not is is updated. The vote is then broadcasted. In case of any errors the post gets added to the error_log for manual review. As of now it does not check yet for revoked authorisation on the Steemconnect side. A user has revoked access, his rules will result in an error message. Success or fail, the vote is removed from the queue and has its log updated.

def vote(self, voter, author, permlink, weight):
    result = self.db.get_user_auth(voter)
    access_token, refresh_token, expire_on = result[0]
    dt = datetime.now()
    c = Client(
            client_id="",
            client_secret="",
            access_token=access_token,
        )

    try:
        # Verify access_token
        if dt > expire_on:
            access_token = self.refresh_token(refresh_token)
            result = c.refresh_access_token(
                        refresh_token,
                        "login,vote"  # scopes
            )
            access_token = result['access_token']
            refresh_token = result['refresh_token']
            expires_in = result['expires_in']
            self.db.update_authentication_tokens(
                voter,
                access_token,
                refresh_token,
                expires_in,
                self.timestamp,
            )
            print('Updated access token\n')

        # Perform vote
        vote = Vote(voter, author, permlink, weight)
        result = c.broadcast([vote.to_operation_structure()])

        # Log vote
        if 'error' in result:
            message = result['error_description']
            self.db.add_to_error_log(
                voter, author, permlink, weight,
                message, self.timestamp,
             )
        else:
            message = 'Succes'
            self.db.update_log(voter, permlink, message)
            print(f"Voter: {voter}\nAuthor: {author}\n" +
                  f"Permlink: {permlink}\nWeight: {weight}\n" +
                  "Upvote succes\n")
    except Exception as error:
        self.db.add_to_error_log(
                voter, author, permlink,
                weight, error, self.timestamp,
        )

Latest votes

In addition the front page now offers a live overview of the latest votes.
Screenshot 2018-07-28 22.15.08.png

GitHub Account

https://github.com/Juless89

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  7 years ago 

Thank you for your contribution. The code is a good example to teach how to use Steem Python.

  1. You may want to define mysql access details somewhere outside the constructor of Database class, which is easier to configure and maintain.
  2. There are some broad execeptions, which is in general a bad practice.
  3. What are the risks of the database compromised and access tokens leaked?
  4. verify_post may be better named to indicate its purpose e.g. is_new_post
  5. You may want to check if a post has been upvoted already before voting it.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hi @justyy, thanks for your feedback.

I will look to apply it for the next update. About the database security, currently root access is turned of and each database has a separate account. After your tip I changed the default url for phpmyadmin. I will also add SSL and password protect.

Hey @justyy
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

thanks for your information

Hey @steempytutorials
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!