MMoSP002C- steem-python 开发一个会自动问某个标签下内容的机器人

in STEEM CN/中文5 years ago (edited)

MMoSP002C- steem-python 开发一个会自动问某个标签下内容的机器人

前一期已经通过 Anaconda 安装了steem-python 开发环境.

这一期做一个和社区其他人互动的机器人:对指定标签下的内容(不含留言回复)进行点赞和留言.

本次的留言是固定.实际运行中的机器人,尽量要动态内容,否则很容易被当作滥用机器人,而被踩(downvote)。

机器人的主要完成的工作:

  1. 扫描当前最新的上链数据;
  2. 获得 comment 类型的操作;
  3. 判断内容是否为根贴,而不是留言回复;
  4. upvote点赞投票1%;
  5. 留言回复:'欢迎使用我心爱的标签 #dappcoder, 免费送你一个热情的赞(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 = 'test'
    reply = '欢迎使用我心爱的标签 #dappcoder, 免费送你一个热情的赞(upvote)'

    # 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()

执行程序: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 开发环境

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63493.34
ETH 2578.53
USDT 1.00
SBD 2.79