create account

steem-python for dummies #3 - Coding an upvote bot by emrebeyler

View this thread on: hive.blogpeakd.comecency.com
· @emrebeyler · (edited)
$21.35
steem-python for dummies #3 - Coding an upvote bot
<img src="https://i.hizliresim.com/Gygv9V.jpg">

Hello,

This is the third post of "steem-python for dummies" series. If you didn't read the first and second post, take your time and have a look.

1. [steem-python for dummies #1 - Introduction and Basic Operations](https://utopian.io/utopian-io/@emrebeyler/steem-python-for-dummies-1)
2. [steem-python for dummies #2 - Playing with account data](https://utopian.io/utopian-io/@emrebeyler/steem-python-for-dummies-2-playing-with-accounts)

In this post, we will study the **get_account_history** method of  and create a simple random upvote bot by using it.

#### Requirements of the bot

Bot,

- should listen transfers directed to it.
- should check the memo is valid 
- upvote the memo URL with a random vote (between 1 and 100) 

**Listening Operations**

One can listen transactions by listening new blocks in the each newly produced blocks. However, if you want to see just specific operations for specific accounts, then you have a shortcut. 

Usage:

```
from steem.account import Account

acc = Account('emrebeyler')
print(list(acc.get_account_history(-1, 1)))
```

See the raw [output](https://gist.githubusercontent.com/emre/6bffc3c08c99d5060a69f6b16e2051ea/raw/d0c1fbe81ebb4692f6010122f2193a8ffc777b9c/get_account_history_output.txt). It includes last two operations relating to my account in the chain.

get_account_history has a bunch of parameters for slicing the data, filtering the operations. It's well documented in the source code, copy/pasting from there.

| Parameter   |      Type      |  Help |
|----------|:-------------:|------:|
| index |  int | start index for get_account_history|
| limit |  int |  (Optional) skip items until this index|
| start | int |   (Optional) skip items until this index |
| stop | int |   (Optional) stop iteration early at this index|
| order | (1, -1) |   1 for chronological, -1 for reverse order|
| filter_by |  (str, list) |   filter out all but these operations|
| raw_output | (bool) | return history in raw format


So, for my case, What I am interested is "transfer" operations.

```
account_history = acc.get_account_history(-1, 100, filter_by=["transfer"])
```

This will return a list of transfer operations related to my account.

**Tip:** This will not return latest 100 transfers. Because filtering is done on the steem-python (client) side, so it will get last 100 operations and filter them by transfers.

#### Two steps of success

**First step:**

 Creating an Account instance with bot's posting key.

```
BOT_ACCOUNT = "BOT_ACCOUNT_USERNAME"
s = Steem(nodes=["https://rpc.buildteam.io"], keys=["PRIVATE_POSTING_KEYS"])
acc = Account(BOT_ACCOUNT, steemd_instance=s)
```

**Second step:**

 In an endless loop, check for all transfers and upvote the correct posts with a random vote weight.

```
while True:
    transfers = acc.get_account_history(-1, 250, filter_by=["transfer"])
    for transfer in transfers:
        if transfer["to"] != BOT_ACCOUNT:
            continue

        try:
            post = Post(transfer.get("memo"))
        except ValueError as e:
            if 'Invalid identifier' == e.args[0]:
                print("Invalid post link. Consider a refund. [%s]" %
                      transfer.get("memo"))
                continue

        # get a voting weight between 1 and 100
        vote_weight = float(random.randint(+1, +100))
        post.commit.vote(post.identifier, vote_weight, account=BOT_ACCOUNT)

        print("Sleeping for 3 seconds")
        time.sleep(3)

```

Voila! Bot constantly check for transfers sent to it, and if it sees a valid post, gives a random upvote. Also, reviews the memo is a valid post or not by using steem-python's built-in capabilities.

However, this bot is not production ready. If you want to continue developing it, here are my advises:

**Missing Features**

1. Refunds. 
2. Check for archived posts. (+7 days)
3. Check for duplicate transactions (Bot shouldn't vote same post again and again.)
4. Handle node timeout errors with a try/except blocks.

***

That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts. 


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@emrebeyler/steem-python-for-dummies-3-coding-an-upvote-bot">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authoremrebeyler
permlinksteem-python-for-dummies-3-coding-an-upvote-bot
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"score":84.55162,"default_branch":"master","watchers":59,"open_issues":61,"forks":31,"open_issues_count":61,"archived":false,"mirror_url":null,"forks_count":31,"has_pages":false,"has_wiki":true,"has_downloads":true,"has_projects":true,"has_issues":true,"language":"Python","watchers_count":59,"stargazers_count":59,"size":610,"homepage":null,"svn_url":"https://github.com/steemit/steem-python","clone_url":"https://github.com/steemit/steem-python.git","ssh_url":"git@github.com:steemit/steem-python.git","git_url":"git://github.com/steemit/steem-python.git","pushed_at":"2017-11-21T08:56:52Z","updated_at":"2017-11-24T04:15:20Z","created_at":"2017-03-13T15:37:52Z","deployments_url":"https://api.github.com/repos/steemit/steem-python/deployments","releases_url":"https://api.github.com/repos/steemit/steem-python/releases{/id}","labels_url":"https://api.github.com/repos/steemit/steem-python/labels{/name}","notifications_url":"https://api.github.com/repos/steemit/steem-python/notifications{?since,all,participating}","milestones_url":"https://api.github.com/repos/steemit/steem-python/milestones{/number}","pulls_url":"https://api.github.com/repos/steemit/steem-python/pulls{/number}","issues_url":"https://api.github.com/repos/steemit/steem-python/issues{/number}","downloads_url":"https://api.github.com/repos/steemit/steem-python/downloads","archive_url":"https://api.github.com/repos/steemit/steem-python/{archive_format}{/ref}","merges_url":"https://api.github.com/repos/steemit/steem-python/merges","compare_url":"https://api.github.com/repos/steemit/steem-python/compare/{base}...{head}","contents_url":"https://api.github.com/repos/steemit/steem-python/contents/{+path}","issue_comment_url":"https://api.github.com/repos/steemit/steem-python/issues/comments{/number}","comments_url":"https://api.github.com/repos/steemit/steem-python/comments{/number}","git_commits_url":"https://api.github.com/repos/steemit/steem-python/git/commits{/sha}","commits_url":"https://api.github.com/repos/steemit/steem-python/commits{/sha}","subscription_url":"https://api.github.com/repos/steemit/steem-python/subscription","subscribers_url":"https://api.github.com/repos/steemit/steem-python/subscribers","contributors_url":"https://api.github.com/repos/steemit/steem-python/contributors","stargazers_url":"https://api.github.com/repos/steemit/steem-python/stargazers","languages_url":"https://api.github.com/repos/steemit/steem-python/languages","statuses_url":"https://api.github.com/repos/steemit/steem-python/statuses/{sha}","trees_url":"https://api.github.com/repos/steemit/steem-python/git/trees{/sha}","git_refs_url":"https://api.github.com/repos/steemit/steem-python/git/refs{/sha}","git_tags_url":"https://api.github.com/repos/steemit/steem-python/git/tags{/sha}","blobs_url":"https://api.github.com/repos/steemit/steem-python/git/blobs{/sha}","tags_url":"https://api.github.com/repos/steemit/steem-python/tags","branches_url":"https://api.github.com/repos/steemit/steem-python/branches{/branch}","assignees_url":"https://api.github.com/repos/steemit/steem-python/assignees{/user}","events_url":"https://api.github.com/repos/steemit/steem-python/events","issue_events_url":"https://api.github.com/repos/steemit/steem-python/issues/events{/number}","hooks_url":"https://api.github.com/repos/steemit/steem-python/hooks","teams_url":"https://api.github.com/repos/steemit/steem-python/teams","collaborators_url":"https://api.github.com/repos/steemit/steem-python/collaborators{/collaborator}","keys_url":"https://api.github.com/repos/steemit/steem-python/keys{/key_id}","forks_url":"https://api.github.com/repos/steemit/steem-python/forks","url":"https://api.github.com/repos/steemit/steem-python","fork":false,"description":"Official Python Library for STEEM","html_url":"https://github.com/steemit/steem-python","private":false,"owner":{"site_admin":false,"type":"Organization","received_events_url":"https://api.github.com/users/steemit/received_events","events_url":"https://api.github.com/users/steemit/events{/privacy}","repos_url":"https://api.github.com/users/steemit/repos","organizations_url":"https://api.github.com/users/steemit/orgs","subscriptions_url":"https://api.github.com/users/steemit/subscriptions","starred_url":"https://api.github.com/users/steemit/starred{/owner}{/repo}","gists_url":"https://api.github.com/users/steemit/gists{/gist_id}","following_url":"https://api.github.com/users/steemit/following{/other_user}","followers_url":"https://api.github.com/users/steemit/followers","html_url":"https://github.com/steemit","url":"https://api.github.com/users/steemit","gravatar_id":"","avatar_url":"https://avatars3.githubusercontent.com/u/17434692?v=4","id":17434692,"login":"steemit"},"full_name":"steemit/steem-python","name":"steem-python","id":84843862},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","steempython","python","programming","bots"],"users":["emrebeyler"],"links":["https://utopian.io/utopian-io/@emrebeyler/steem-python-for-dummies-1","https://utopian.io/utopian-io/@emrebeyler/steem-python-for-dummies-2-playing-with-accounts","https://gist.githubusercontent.com/emre/6bffc3c08c99d5060a69f6b16e2051ea/raw/d0c1fbe81ebb4692f6010122f2193a8ffc777b9c/get_account_history_output.txt"]}"
created2017-11-24 13:57:00
last_update2017-11-24 13:58:51
depth0
children8
last_payout2017-12-01 13:57:00
cashout_time1969-12-31 23:59:59
total_payout_value15.518 HBD
curator_payout_value5.831 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,355
author_reputation448,535,049,068,622
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries
0.
accountespoem
weight99
1.
accountfreedom
weight658
2.
accountmisterdelegation
weight549
3.
accountned
weight544
4.
accountruah
weight62
5.
accounttransisto
weight71
6.
accountwackou
weight53
7.
accountxeldal
weight27
max_accepted_payout1,000,000.000 HBD
percent_hbd5,000
post_id21,394,691
net_rshares10,696,775,201,099
author_curate_reward""
vote details (45)
@beerlover ·
<div class='pull-right'>https://cdn.steemitimages.com/DQmaHThyECGhEx8tSfHZbiMFRNYjJ35K92cDgiJjkzBUaJo/One%20sip%20of%20BEER%20for%20you.gif<p><sup><a href='https://steem-engine.com/?p=market&t=BEER'>View or trade </a> <code>BEER</code>.</sup></p></div><center><br><br> <p> Hey @emrebeyler, here is a little bit of <code>BEER</code> from @isnochys for you. Enjoy it!</p> </center>
properties (22)
authorbeerlover
permlinkre-steem-python-for-dummies-3-coding-an-upvote-bot-20191124t050812z
categoryutopian-io
json_metadata"{"app": "beem/0.21.1"}"
created2019-11-24 05:08:15
last_update2019-11-24 05:08:15
depth1
children0
last_payout2019-12-01 05:08:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length379
author_reputation25,765,264,402,130
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,780,926
net_rshares0
@doctorvee ·
Thank you for this documentation I've been learning and training 
properties (22)
authordoctorvee
permlinkre-emrebeyler-steem-python-for-dummies-3-coding-an-upvote-bot-20171207t182551510z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"busy","app":"busy/1.0.0"}
created2017-12-07 18:25:54
last_update2017-12-07 18:25:54
depth1
children0
last_payout2017-12-14 18:25:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length65
author_reputation608,629,613,641
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id22,693,130
net_rshares0
@isnochys ·
2 years old !BEER
properties (22)
authorisnochys
permlinkre-steem-python-for-dummies-3-coding-an-upvote-bot-20191124t050756z
categoryutopian-io
json_metadata"{"app": "beem/0.20.23"}"
created2019-11-24 05:08:00
last_update2019-11-24 05:08:00
depth1
children0
last_payout2019-12-01 05:08:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length17
author_reputation48,455,405,511,148
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id92,780,925
net_rshares0
@jefpatat ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorjefpatat
permlinkre-emrebeyler-steem-python-for-dummies-3-coding-an-upvote-bot-20171124t140030397z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2017-11-24 14:00:24
last_update2017-11-24 14:00:24
depth1
children1
last_payout2017-12-01 14:00:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation26,609,526,234,408
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id21,395,006
net_rshares0
@emrebeyler ·
lightning fast! thanks.
properties (22)
authoremrebeyler
permlinkre-jefpatat-re-emrebeyler-steem-python-for-dummies-3-coding-an-upvote-bot-20171124t140111022z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2017-11-24 14:01:12
last_update2017-11-24 14:01:12
depth2
children0
last_payout2017-12-01 14:01:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length23
author_reputation448,535,049,068,622
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id21,395,074
net_rshares0
@petervroom · (edited)
Brilliant @emrebeyler, thank you
properties (22)
authorpetervroom
permlinkre-emrebeyler-steem-python-for-dummies-3-coding-an-upvote-bot-20180629t074033076z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1","users":["emrebeyler"]}
created2018-06-29 07:40:36
last_update2018-06-29 07:41:45
depth1
children0
last_payout2018-07-06 07:40:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length32
author_reputation12,060,305,749,902
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,710,621
net_rshares0
@utopian-io ·
### Hey @emrebeyler I am @utopian-io. I have just upvoted you at 7% Power!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
- Contribute more often to get higher and higher rewards. I wish to see you often!
- I introduced a competition factor. My vote is based also on how competitive the category used is.
#### Human Curation
- Do you believe you deserved more? Let a human curator know. <a href="https://discord.gg/PrzCKpq">Get in touch on Discord</a>
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-emrebeyler-steem-python-for-dummies-3-coding-an-upvote-bot-20171125t124206418z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2017-11-25 12:42:06
last_update2017-11-25 12:42:06
depth1
children1
last_payout2017-12-02 12:42:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,573
author_reputation152,955,367,999,756
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id21,479,815
net_rshares0
@zhanmusi ·
$0.34
maybe next time.
👍  
properties (23)
authorzhanmusi
permlinkre-utopian-io-re-emrebeyler-steem-python-for-dummies-3-coding-an-upvote-bot-20180422t051937518z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-04-22 05:19:36
last_update2018-04-22 05:19:36
depth2
children0
last_payout2018-04-29 05:19:36
cashout_time1969-12-31 23:59:59
total_payout_value0.342 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length16
author_reputation5,566,405,034,040
root_title"steem-python for dummies #3 - Coding an upvote bot"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id51,424,645
net_rshares52,552,493,993
author_curate_reward""
vote details (1)