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

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

目录:

上节课留下了一个思考题,如果我想要一天只点N篇文章怎么实现?

这可以通过get_blog的API获取文章的发文时间,然后统计一下24小时内点赞了多少篇文章。简单代码:

import requests
nodes="https://api.justyy.com"
name="maiyude"
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"]

created = result[0]["comment"]["created"]  # 发文时间

实现方法也很多,可以自己本地建个数据库记录一下,也可以直接读取最近文章对比一下。

重新设计点赞逻辑

这里我简单重新设计下检查逻辑:

1、检查最新文章是否已点赞,已经点赞了就跳过。未点赞继续。

2、检查相邻文章发文时间间距,如果超过一天,点赞继续,未超过一天,继续下面检查.

3、检查24小时内文章点赞次数是否允许范围内,允许范围内继续点赞,否则跳过。

设定一个点赞允许范围。

修改前面几节课的点赞名单设定,再添加一个allow的值,这值的意思为24小时内允许点赞多少篇文章。我设定maiyude一天可以点3篇。

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

修改前面代码

按照我们设定的点赞逻辑,我们继续修改上节课的代码:

大家可以按照自己喜好修改,我就直接放我改的代码了。

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

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

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


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:
            try:
                name=i["name"]#名字
                allow = i["allow"]
                weight=i["percent"]*100#点赞比例

                data={"jsonrpc":"2.0", "method":"condenser_api.get_blog", "params":[name,0,allow+1], "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 t in vote_list:
                    if player == t["voter"]:
                        flag = True

                if flag == False:
                    vote_yes=False
                    print("检查发文时间")
                    created = result[0]["comment"]["created"]  # 发文时间
                    created_last = result[-1]["comment"]["created"]  # 发文时间
                    d1 = datetime.datetime.strptime(created.replace("T", " "), '%Y-%m-%d %H:%M:%S')
                    d2 = datetime.datetime.strptime(created_last.replace("T", " "), '%Y-%m-%d %H:%M:%S')
                    delta = d1 - d2
                    seconds = delta.days * 86400 + delta.seconds
                    if seconds > 86400:
                        print("大于一天,点赞继续", delta)
                        vote_yes = True
                    else:
                        print("小于1天,检查是否超过允许量",allow, delta)
                        vote_time = 0
                        for w in result:
                            vote_list_ii = w["comment"]["active_votes"]
                            for ww in vote_list_ii:
                                if player == ww["voter"]:
                                    vote_time += 1
                        print("范围内已点赞文章数:", vote_time)
                        if vote_time >= allow:
                            print("超过允许量:",allow)
                        else:
                            print("允许范围内,继续点赞:",allow)
                            vote_yes = True

                    if vote_yes == True:
                        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,"已点赞过,跳过")
            except Exception as e:
                try:
                    print("错误:", e)
                    print(r.json())
                except:
                    pass

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

回头测试下有没BUG出现,这里测试出如果是新人发文数量不足,会报错的。怎么解决自己思考了:)

课后思考

我们的点赞机器人已经较为完善了,当然这还不算完成,这还会有各种各样的报错和BUG,等着你运行过程中发现并修理。努力的维护一个点赞机器人吧。

另外这间隔10分钟是不是有点长呢?如果我想发文的时候瞬间就反应过来,要怎么做呢?思考一下吧,

课后作业

没有

Sort:  

感谢鱼哥的付出,您辛苦了。👍

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

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.032
BTC 62302.26
ETH 2994.60
USDT 1.00
SBD 3.96