Today I'd like to show you a skeleton of how to implement upvote robot using python.
This post would not talk about whether good or bad to leverage our work to robots, this is only a post about steemd programming, technically.
This shows only a skeleton and a really rough upvote policy so don't expect this robot will bring rewards to you. Hope everyone to discover and upvote good contents by his own.
Okay, Now let's begin!
Install steem-python
At the time of writing, I found that steem has publish their own python library: steem-python. It is extremly simple to write the bot using steem-python!
firstly, install steem-python using pip3 install -U steem
--- since steem-python requires python3.
Writing code
then create a bot.py
file using vim or whatever editor you like, and type the following lines:
from steem import Steem
from steem.blockchain import Blockchain
from steem.post import Post
from steem.utils import is_comment
def run():
# upvote posts with 100% weight
upvote_pct = 100
whoami = 'your-steem-username'
# stream comments as they are published on the blockchain
# turn them into convenient Post objects while we're at it
s = Steem(keys=['your-post-private-key'])
b = Blockchain(s)
stream = map(Post, b.stream(filter_by=['comment']))
for post in stream:
if is_comment(post):
continue
if post.json_metadata:
tags = post.json_metadata.get('tags', [])
# if post in 'art' tags
if tags and 'art' in tags:
post.upvote(weight=upvote_pct, voter=whoami)
if __name__ == '__main__':
run()
Some explanation
Let's do some explanation, stream = map(Post, b.stream(filter_by=['comment']))
will keep collecting all comment transactions on steem blocks and map them to a stream of Post
object. In the following for
loop, we first filter out the purely comments --- since comments and posts are both represent as comments in steem block. Then we get the post's tags and upvote the posts with the 'art' tag.
The last but the most important, you should replace your-steem-username
and your-post-private-key
with your own. I'm sure you know your username :P.
To find the post private key, you need to goto "Wallet -> Permissions" page, you will find all your four keys and post key at first, but notice that this post key is the public key, what we need is the private key so click the "SHOW PRIVATE KEY" button to get it.
NOTE!!! The private post key owns the power of posting and upvoting, you should make sure only yourself knows this key!
Run it!
Type python3 ./bot.py
command and wait to go steemd.com to see your activities!
中文
这篇文章将用 python 实现一个点赞机器人的骨架. 先不讨论用机器人点赞好不好, 我们仅仅从编程角度分享一种实现方式, 所以这篇文章只是个技术分享文 :P. 而且这篇文章里展示的代码仅仅是个大概架子, 点赞的策略也是粗暴异常, 非编程专业的童鞋不要期望能直接 copy 代码拿去赚收益啦 :P.
ok, 接下来我们开始.
安装 steem-python
steem-python 是官方提供的 python 开发库, 使用这个库去实现我们的机器人意想不到的简单. 由于 steem-python 要求 python3, 所以我们用以下命令安装:
pip3 install -U steem
编写代码
安装好 steem-python 后, 用你喜欢的编辑器创建一个 bot.py
文件, 键入如下内容:
from steem import Steem
from steem.blockchain import Blockchain
from steem.post import Post
from steem.utils import is_comment
def run():
# upvote posts with 100% weight
upvote_pct = 100
whoami = 'your-steem-username'
# stream comments as they are published on the blockchain
# turn them into convenient Post objects while we're at it
s = Steem(keys=['your-post-private-key'])
b = Blockchain(s)
stream = map(Post, b.stream(filter_by=['comment']))
for post in stream:
if is_comment(post):
continue
if post.json_metadata:
tags = post.json_metadata.get('tags', [])
# if post in 'art' tags
if tags and 'art' in tags:
post.upvote(weight=upvote_pct, voter=whoami)
if __name__ == '__main__':
run()
一点解释
stream = map(Post, b.stream(filter_by=['comment']))
这句会持续的从 steem 区块上收集新的 "comment" 信息, 将其映射成 Post
对象流, 然后下面的 for
循环会遍历这些 Post
对象, 剔除纯评论 (因为在 steem 区块上, 评论和文章都存储为 "comment" 类型), 然后看到文章里有 "art" 标签的就给点赞! 很粗暴的方式.
最重要的一点就是不要忘了替换代码里的 your-steem-username
和 your-post-private-key
为你自己的, 你的账户名我想你肯定知道的. 至于 your-post-private-key
如何获取, 你需要到 "Wallet -> Permissions" 界面, 这里列着你的四个 keys, 第一个就是 post key, 但要注意这显示出来的是 post public key, 我们需要点击 "SHOW PRIVATE KEY" 按钮查看 private key.
注意!!! private post key 掌管着你的发文和点赞, 千万不要泄露这个 key!!
运行它!
现在你可以运行 python3 ./bot.py
了, 运行后可以等一会儿, 然后到 steemd.com 上查看你的点赞记录~
结语
最后再说一句.
希望我们大家都用心发现好的内容, 亲自为好的内容点赞, 这才是我们大家聚集在这个社区的意义.
并且关于机器人点赞的收益坛哥已经在他的 Steemit的点赞收益率受什么因素影响? 一文讨论过了, 文章内容的甄别机器人是无法与人相比的, 只有我们亲自发现好的内容并点赞它, 才能获得最高的收益.