[learning]steem区块链机器人开发学习活动(5)-点赞机器人改进

in hive-180932 •  2 years ago  (edited)

目录:

steem api,官方帮助文档:https://developers.steem.io/apidefinitions/

上节课我们写了一个简单的点赞机器人,有练习的朋友,肯定发现了大量报错吧。

报错主要来自下面的几个因素:
1、VP不够
2、RC不够
3、重复文章相同点赞能量点赞
4、点赞过快(需要间隔3秒)

原因知道了,下面来修改代码改进一下。

获取VP和RC

VP和可以用前面学过的get_accounts获得。RC可以用rc_api获取。
简单示例代码:

import requests
import time

nodes="https://api.justyy.com"
player="nutbox.awesome"
data = {"jsonrpc": "2.0", "method": "condenser_api.get_accounts", "params": [[player]], "id": 1}
response = requests.post(url=nodes, json=data)
result = response.json()["result"]
voting_power=result[0]["voting_power"]/100
print("VP是:",voting_power)
data = {"jsonrpc": "2.0", "method": "rc_api.find_rc_accounts", "params": {"accounts": [player]}, "id": 1}
response = requests.post(nodes, json=data)
rc = response.json()["result"]["rc_accounts"]
max_rc = float(rc[0]["max_rc"])
rc_manabar = float(rc[0]["rc_manabar"]["current_mana"])
rc = int((rc_manabar / max_rc) * 100)
print("RC是:",rc)

获取点赞列表

要避免重复点赞,一个简单办法就是获取文章的点赞列表,如果已经点过就跳过。

点赞列表可以从get_blog的API获取。

简单示例代码:

import requests
nodes="https://api.justyy.com"
player="nutbox.awesome"

data = {"jsonrpc": "2.0", "method": "condenser_api.get_blog", "params": ["maiyude", 0, 3], "id": 1}
r = requests.post(nodes, json=data)
result = r.json()["result"]
vote_list=result[0]["comment"]["active_votes"]
print(vote_list)

输出:

image.png

改进点赞机器人

接下来要改进点赞机器人,我们重新设计一下逻辑。

1、假设手头有个列表,这个列表手动更新,名单里的人点赞最新文章。
2、VP小于80%停止点赞,RC小于50%停止点赞
3、遇到点赞过的,跳过
4、10分钟循环一次

然后翻出上节课的代码,结合上面的几个API改改。

各位可以按照自己的逻辑改改,我这里放出我的代码。

from beem.steem import Steem
from beembase import operations
from beem.transactionbuilder import TransactionBuilder
import requests
import time

nodes="https://api.justyy.com"
player="nutbox.awesome"
keys="5KYFn8tH7"

name_list=[{"name":"maiyude","percent":100},{"name":"justyy","percent":100}]

while True:
    data = {"jsonrpc": "2.0", "method": "condenser_api.get_accounts", "params": [[player]], "id": 1}
    response = requests.post(url=nodes, json=data)
    result = response.json()["result"]
    voting_power = result[0]["voting_power"] / 100
    print("VP是:", voting_power)
    data = {"jsonrpc": "2.0", "method": "rc_api.find_rc_accounts", "params": {"accounts": [player]}, "id": 1}
    response = requests.post(nodes, json=data)
    rc = response.json()["result"]["rc_accounts"]
    max_rc = float(rc[0]["max_rc"])
    rc_manabar = float(rc[0]["rc_manabar"]["current_mana"])
    rc = int((rc_manabar / max_rc) * 100)
    print("RC是:", rc)

    if voting_power >= 80 and rc >= 50:
        print("程序开始")
        for i in name_list:
            name=i["name"]#名字
            weight=i["percent"]*100#点赞比例

            data={"jsonrpc":"2.0", "method":"condenser_api.get_blog", "params":[name,0,3], "id":1}
            r=requests.post(nodes,json=data)
            result=r.json()["result"]

            author=result[0]["comment"]["author"]#文章作者
            permlink = result[0]["comment"]["permlink"]  # 文章链接
            title = result[0]["comment"]["title"]  # 文章标题

            print("点赞:",name,"文章:",title,permlink)

            #检查是否已点赞
            vote_list=result[0]["comment"]["active_votes"]
            flag=False
            for i in vote_list:
                if player == i["voter"]:
                    flag = True

            if flag == False:
                s = Steem(keys=[keys],node=nodes)

                op=operations.Vote(**
                                      {
                                          'voter': player,
                                          'author': author,
                                          'permlink': permlink,
                                          'weight': weight
                                      }
                                      )
                tx = TransactionBuilder(steem_instance=s)
                tx.appendOps(op)
                #把签名添加
                tx.appendSigner(player, "posting")
                tx.sign()
                broadcast=tx.broadcast()
                print(broadcast)
                time.sleep(3)
            else:
                print(name,"已点赞过,跳过")

    print("名单点赞完成,休息10分钟")
    time.sleep(60*10)

课后思考

这程序虽然完善了一点儿,但是如果我想要一天只点某个人一篇怎么办?
请思考一下怎么实现。

课后作业

没有,随便玩

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:  
  ·  2 years ago 

鱼哥rc是干嘛用的。 !shop

[WhereIn Android] (http://www.wherein.io)

  ·  2 years ago 

资源能量RC,任何操作都要消耗,没了就动弹不得

  ·  2 years ago 

你好鸭,maiyude!

@boylikegirl给您叫了一份外卖!

烤肉

吃饱了吗?跟我猜拳吧! 石头,剪刀,布~

如果您对我的服务满意,请不要吝啬您的点赞~

  ·  2 years ago 

石头

[WhereIn Android] (http://www.wherein.io)

  ·  2 years ago 


You lose! 你输了!愿赌服输,请给我点赞~

  ·  2 years ago 

石头

[WhereIn Android] (http://www.wherein.io)

  ·  2 years ago 


You win!!!! 你赢了! 给你1枚SHOP币!

感谢鱼哥。

[WhereIn Android] (http://www.wherein.io)