I recently released the bot called TrendChecker, which helps you to find out how high your post is in Hot/Trending/Promoted. If my bot receives a transaction with a valid url as the memo it checks where your post is in Hot or Trending and then sends your money back. The bot is completely build only with steem-python. This is a tutorial for people who are interested in the code behind the project and want to learn something about steem-python and how to develop bots for Steem. I will try to explain the backbone of this project and explain one aspect that might help you with your project.
What will I learn?
- How to find out how high your post is in Hot or Trending.
- How to check for incoming transactions.
Requirements
- Python 3.6
- steem-python
- requests (only if you want to run the full bot)
Difficulty
- Basic
Tutorial Contents
Setup
This is pretty simple because it is basically the same for every project: just import Steem and initialize the class. In addition, you will need the build in module json
. In addition, I created two .json
files: the first is for storing the account name and the private keys and the second is to avoid double spending. More on that later on in the tutorial.
Finding a specific post in Hot or Trending
Only a very small percentage of all posts reach the general top 100 of Hot or Trending and therefore we want to get the top 100 for every tag of our post.
To get the tags we need to use s.get_content(author,permlink)
. However, the user only sent us a full url of the post with this format: steemit.com/main-tag/@author/permlink
. If we split this url every time a “/” comes up the author will be fifth element and the permlink the sixth one. The only thing left to do is to remove the “@” from the string with the author’s name.
After getting the content, we can finally extract all the tags. However, these tags are located in the json_metadata
of the post and if we call post[“json_metadata”][“tags”]
we will get an error. We need to load this .json “file” as a json first and then we can get the tags.
Sometimes users send wrong urls and we don’t want our bot to stop. Therefore we will put these few lines of code into a try: | except:
construct. If something fails for whatever reason the bot will simply return "TRENDCHECKER error! wrong url or post doesn't exist..."
.
These are all the functions from the official documentation to get posts by some factor.
To try out which post they will return you can simply test most of them by going to the corresponding steemit.com url.
get_discussions_by_votes()
--> https://steemit.com/votes/
get_discussions_by_active()
--> https://steemit.com/active/
get_discussions_by_payout()
--> https://steemit.com/payout/
Now we can get all hot, trending and promoted post for every tag in our list of tags. Then we will create 3 variables. Please note here that that could be done with only one variable! These variables will count what rank our post has in Hot/…. .
As the last step, we’ll create a for loop
for the Hot, Trending and Promoted posts. If the author of the post in one of this loops is equal to the author of our post we will stop the for loop
by writing break
. If this isn’t the case we will add 1 to our counter (variable).
Note: While I’m writing this tutorial I noticed that checking for the author isn’t enough because if you released multiple post in a short period of time they all could be in Hot/Trending. Now If your second post was more successful it likely surpassed the first post in its ranking. However, if your looking for your first post you will instead get the ranking of your second post. To fix this we will need to check if the permlink
is the same, too.
The beautiful ability of strings is that you can add them to each other similarly to numbers. For our memo (the message that will be send back to the user) we will start with a little “brand” message.
Now for every tag we’ll add (Tag):
to the memo. If the tag is nothing (general hot/trending page) we will add All:
.
If we find our matching post in Hot or Trending we will add str(rank) + “.” + “ in hot/trending/promoted”
.
After we gone through all post in Hot/Trending/Promoted we will add a simple comma as a separation.
The last step is to return this memo which will now be send back to the user.
Checking for incoming transactions
Obviously, we want our bot to run without any breaks and delays. Therefore the first thing we have to do is an infinitive loop
. The simplest one is while 1:
.
Secondly we need to import one more steem-python class to get the incoming transactions: the account class
.
It easily allows us to access account data. It needs to
be initialized with your account name and the Steem()
class.
With the help of this class we now can get our account history which we can filter_by transfer
. There is also another command for getting the account history which relies on the standard Steem class
.
The only catch with this function is that you can’t filter it.
The -1
sets the order: the newest transaction/index comes first. 500
is the limit of the history. Getting 500 items from our history is definitely overkill and we would need much less. However, if the bot was in real use and went down for some hours it would still process most transactions and send the money back after it went back online because your transaction is still likely to be within our limit of 500. If the limit were 3 and I got 3 Bid bot advertisements in that time frame the bot wouldn’t see your transaction and therefore not send your money back.
Next up are the conditions for when we want our bot to act:
- the transaction has never been processed before --> is not in our trx_list
- the transaction was sent to me not from me
- the transaction has a memo
- the memo contains a url
If all these conditions are true we can be pretty certain that the user wanted to use our bot. Now we can add this new trx_id
to our list of transaction so that it doesn’t get processed twice, generate a memo (discover where to post is in hot…) and send the money back to the user.
If you want me to cover these three functions write a comment down below.
Now we are basically finished, however, we want to ensure that our bot can run without any downtime. The bot sometimes failed at this part for whatever reason so we again put it in a try: | except:
construct. If the bot fails to execute these previous lines of code he waits 30 seconds and the initializes the Steem()
and Account()
Class again. For the ability to sleep we need to import the build in package time
.
With this code the bot ran successfully for 3-5 days straight. However, the bot stilled stopped once or twice with this error. If anyone could help me please write a comment or contact me on Discord (wil1liam#8715).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "trendchecker.py", line 118, in <module>
s = Steem(node,keys=keys)
File "/home/konstantin/steem-python/steem/account.py", line 34, in __init__
self.refresh()
File "/home/konstantin/steem-python/steem/account.py", line 37, in refresh
account = self.steemd.get_account(self.name)
File "/home/konstantin/steem-python/steem/steemd.py", line 168, in get_account
return first(self.call('get_accounts', [account]))
File "/home/konstantin/steem-python/steembase/http_client.py", line 223, in call
*args)
File "/home/konstantin/steem-python/steembase/http_client.py", line 243, in call
return_with_args=return_with_args)
File "/home/konstantin/steem-python/steembase/http_client.py", line 263, in _return
raise RPCError(error_message)
steembase.exceptions.RPCError: Assert Exception:api_itr != _registered_apis.end(): Could not find API
I hope you liked this tutorial! Leave any feedback or questions in the comments below!
You check out my previous work if you liked it!
To view the full code visit my Github repository.
Curriculum
Bot Development
Steemit Python Bots
- Part 1: How to find someone's newest post
- Part 2: How to return a list of the people who upvoted your post
- Part 3: Relike someones post if they liked yours
Steemit Python Analysis
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@rdvn Thanks man! and thanks again for the fast approval! :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @rdvn, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey there!
A little tip for your coding style.
Here you are nesting way too many ifs.
It is alot nicer to read if you get rid of the indentions like this:
Greetings,
Martin
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for the advice! :)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Really cool article. Too bad I only found it now. I added it to my steemit wiki: https://yoirtuts.com/index.php?title=Steemit
I will add you on discord. Did you end up solving the issue?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for adding it :)
I currently really busy right now so I haven't fixed the error yet
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey man, great post! Most of it went straight over my head because i'm not really intelligent in coding. Nonetheless, I saw you were a teen. Me and a couple of the teens on this platform are banding together to make a teen community. I was wondering if you would like to join? We definitely need the coding help, and I think you would make an excellent member to the teen team!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@iamturtless I would definitely like to join!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Alright man, great! Just discord me and we can talk there. @Iamturtless#0853
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
If this is true you are a really cool kid @wil1liam. When did you start programming? I am really honored to meet you. My older son is in middle school and he gets sick of programming. I asked him to start with scratch and he doesn't like it.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@mineopoly I think I started 2-3 years ago with a little bit HTML and CSS . However, I never really got into it. Then last year I made a school project with 2 friends and we programmed it with Python. I couldn't write a single if statement at the time but after the project I started learning it, too... and now I'm here today.
Scratch may be an easy thing to learn although I would still not completely recommend it because I didn't like it back then as well as now.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I see. Thanks a lot. I know you are busy at school and with projects. I will try python. He doesn't see the usefulness yet.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Maybe he will release it one day but don't try to force him too much because kids often do the opposite of what their parents suggest (especially in puberty) :(
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thank u very much!! i think u shouldn't of done that as it is dropping at the moment and keep it and wait till it goes up. i will try send you something when i gain more or remember. lol i put it straight in savings. i didn't realise it was from you till i scrolled down woops! very great idea. amazing your 16 keep going !!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks! :) and no please don't send me 0.001 back xD
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
lol its amazing ur doing this!!! hahaha xxx
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
wtf i just saw my wallet wtf !!!! thank u xxxx stop it !!!! lol xxx
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
omfg thank u!!!xxx
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
I just wanted to say thanks for your posts on bot development. I'm in the middle (ok, more like very early stages) of teaching myself python and your tutorials are just what I need to continue moving forward.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you! I hope to see your bots one day...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Nice job, any ideas for your next bot?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks! One the 5th I will post a development update for this bot with the design contest winner and the switch to a web based interface.
Furthermore I will launch two bots in the coming weeks:
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Awesome dude, great work, I'm learning Solidity myself, and have dabbled in Python. I really want to write a couple of scripts, so I'm sure there's plenty I can learn from you!
Cheers
Cg
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks, man!
Solidity will be one of the languages of the future so I should pick it up, too!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
i have up voted your 39 post you can check and now i am following you i support my followers and expect same from them and thanks for free money dear ,Its you you can have it any time when you need it. I am interested in steemit earning if you can guide me .It will be very help full.I am waiting for your reply dear
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
THANKS for your amazing support.
Generally you shouldn't upvote any posts that are older then7 days because after 7 days the rewards will be paid out to the author and after that the author can't receive any rewards from that post.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Oh I see i don't know that .thanks for telling me and sorry for bad typing .So could U guide me about steemit earning ?
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@hashmieali77 sorry for the late reply! If you want to talk add me on Discord wil1liam#8715
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
ok i am also on discord,dear
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Good work! Keep doing your awesome work, it will pay off ;)
Greetings
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for your nice words! I will definitely keep posting 💪
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey!
Sau cooles Tutorial! Sehr unterstützenswert!
Als ich in deinem Alter war, also vor 3 Jahren (:D), hab ich auch noch extrem viel programmiert.
Mittlerweile habe ich gar keinen Plan mehr..
Hast nen Follow ;)
Greets!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Danke Mann! Cool mal jemand deutschen zu treffen!
Hast du angefangen Partys zu feiern oder warum programmierst du nicht mehr? xD
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Meine Hasslehrerin und 12. Klasse Theorie haben's mir versaut..
Außerdem habe ich jetzt andere Interessen.^^
Aber mal sehen wohin mich mein Weg führt ;)
greets!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Mies... meine Mathe Lehrerin macht mir auch gerade das Leben schwer.
Aber "andere Interessen" klingt ziemlich gut ^^
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Ach die Deutschen sind hier stark vertreten ;)
Und weil davor mal was von wegen discord gesagt wurde D-A-CH Discord.
Auf alle Fälle mal vorbeischauen (falls du das noch nicht gemacht hast :) )
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You seem like a smart dude who doesn't post shitty content. May be a good idea to quit sending memos, as most see them as spam.
You may want to involve in some discord communities, you may get more exposure that way.
As for the bot crash, why don t you use a process controll app? Just restart the thing , haha.
I've used pm2 in the past, and i just love it .Mostly used for node js, but if you specify an interpreter , python works too, i quess .
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks for the advice I think I should switch...
Yeah, I also should probably quit spamming memos and engage more but otherwise my reach is very low :( This issue really got worse with the invention of bid based voting bots. Now posts from normal people can't reach the top in Hot or Trending anymore...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Awesome work man, you got it right 🙋🙋🙋
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thanks man!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
excellent post sir @wil1liam about steem python bot. Very informative for us. Thanks for sharing.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
WARNING - The message you received from @iripilippopolis is a CONFIRMED SCAM!
DO NOT FOLLOW any instruction and DO NOT CLICK on any link in the comment!
For more information, read this post: https://steemit.com/steemit/@arcange/phishing-site-reported-uppervotes-dot-ml
Please consider to upvote this warning if you find my work to protect you and the platform valuable. Your support is welcome!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @wil1liam I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hello,
I'm bot. I am here because @wil1liam wanted to know how high this post is in Hot or Trending.
You want to know what your post's rank in Hot or Trending is?
1. copy your Steemit.com post url
2. send 0.001 SBD/STEEM to @trendchecker with the url as the memo
3. wait 20 seconds to receive the results
As a gift your post will be upvoted for free!
- This bot was development by @wil1liam.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
It works 🎉🎉🎉🎉🎉
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You got a 11.65% upvote from @slimwhale courtesy of @prometheus21!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you for the Steem!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit