In the previous phase, the steem-python development environment has been installed through anaconda.
In this phase, we will make a robot to interact with other people in the community: upvote and post on the content under the designated tag (excluding message comment)
This content of comment is fixed. In the actual operation of the robot, try to dynamic content, otherwise it is easy to be used as an abusive robot and be downvoted.
The main tasks of the robot are as follows:
- Scan the latest data of steem blockchian;
- Get the operation of 'comment' type;
- Judge whether the 'comment' is a root post rather than a reply;
- Upvote 1% to the post;
- Reply to the message: 'welcome to use my favorite tag, #dappcoder, and give you a warm upvote for free'
Directly on the code, there are some necessary comments, to help you understand.
Demo a greeting robot to tag with upvote(MMoSP002_hello_tag_with_vote.py):
from contextlib import suppress
from steem.blockchain import Blockchain
from steem.post import Post
import random
def run():
# upvote posts with 0.1~0.2% weight
weight = random.randrange(10, 20, 2) / 100
account = 'dappcoder'
tag = 'dappcoder'
reply = 'welcome to use my favorite tag, #dappcoder, and give you a warm upvote for free'
# stream comments as they are published on the blockchain
# turn them into convenient Post objects while we're at it
bc = Blockchain()
stream = map(Post, bc.stream(filter_by=['comment']))
for post in stream:
print('post:', post)
if post.is_comment():
print('not is root post:', post)
continue
print('post.json_metadata:', post.json_metadata)
if post.json_metadata:
tags = post.json_metadata.get('tags', [])
print('tags:', tags)
# if tag in tags and len(tags) < 10:
if tag in tags:
print(account, 'do upvote:', weight)
post.upvote(weight=weight, voter=account)
print(account, 'do comment:', reply)
post.reply(reply, title="", author=account, meta=None)
if __name__ == '__main__':
with suppress(KeyboardInterrupt):
run()
Run it :python MMoSP002_hello_tag_with_vote.py
English list:
#esteem #hive-139531 #steemdevs #community #programming
MMoSP000E-< make money on steem-python > Preface
MMoSP001E- install steem python with anaconda
中文列表
#cn #hive-180932 #chinese #hive-143316
MMoSP000C-《steem-python赚钱实战教程》序言
MMoSP001C- Anaconda 安装 steem-python 开发环境
MMoSP002C- steem-python 开发一个会自动问某个标签下内容的机器人