Steemit-Python-Bot | Part 2: How to return a list of the people who upvoted your post

in #utopian-io7 years ago (edited)

Introduction

<p dir="auto">In this series I will share my experience of building a Steemit bot with the python library. I'll post my functions used as a basis for my bot and try to explain it. The final goal will be to develop a high level API build on top of the Steem-Python library to make bot development easy. <h4>Requirements <h5>1. Python 3.6 <h5>2. steem-python <p dir="auto">I personally use Windows and it was quite challenging to install the library but this <a href="https://steemit.com/utopian-io/@holger80/how-to-install-steem-python-for-windows" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">tutorial helped me a lot. <h4>The Code <h5>1. Setup <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615804/fg7ctkqn17vcjm6lwj2l.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615804/fg7ctkqn17vcjm6lwj2l.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615804/fg7ctkqn17vcjm6lwj2l.png 2x" /> <p dir="auto">Just like in the last tutorial we need to import the library and initialize the Steem class. <h5>2. The function <p dir="auto">The new function <code>get_voter() basically uses the already existing <code>get_active_votes function but makes it easier to use. <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615260/jerlvi130qshiqersu40.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615260/jerlvi130qshiqersu40.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615260/jerlvi130qshiqersu40.png 2x" /> <p dir="auto">The old function takes two inputs and we will combine these into one input ("post_identifier") and later split them into two again. <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615552/cfp1mf3byfepx3ffhgsi.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615552/cfp1mf3byfepx3ffhgsi.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517615552/cfp1mf3byfepx3ffhgsi.png 2x" /> <p dir="auto">After excuting the function, it returns a list like stated in the offical documentation.<br /> <img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616183/jnxf7g4vxadsclb62snm.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616183/jnxf7g4vxadsclb62snm.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616183/jnxf7g4vxadsclb62snm.png 2x" /> <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616307/amx2jjo9bbzazlsvmrak.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616307/amx2jjo9bbzazlsvmrak.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616307/amx2jjo9bbzazlsvmrak.png 2x" /> <p dir="auto">In this example we only want the username of the upvoters but you could also extract other usefull informations like the voting weight. <p dir="auto">To isolate this information we need to create a new empty list <code> voter = [] and then add the value of <code>'voter' to it, every step of a <code>for loop. <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616738/rckmz5g2pnfdu78zpest.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616738/rckmz5g2pnfdu78zpest.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517616738/rckmz5g2pnfdu78zpest.png 2x" /> <p dir="auto">But before we return the list in the next line we filter out our own upvote in line 13. This will prevent possible further errors with double votes. <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617345/i53riwyxgj5cfpc67dqr.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617345/i53riwyxgj5cfpc67dqr.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617345/i53riwyxgj5cfpc67dqr.png 2x" /> <h5>3. Execution <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617381/yyktptbg3te9zyakzyce.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617381/yyktptbg3te9zyakzyce.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617381/yyktptbg3te9zyakzyce.png 2x" /> <p dir="auto">The executed function will return a list like this: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617244/bkpdmedlg7imlp3ph1ub.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617244/bkpdmedlg7imlp3ph1ub.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617244/bkpdmedlg7imlp3ph1ub.png 2x" /> <p dir="auto">As mentioned earlier you only need to provide one argument: <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617527/orkmiaoaxh8fuuqizjzb.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617527/orkmiaoaxh8fuuqizjzb.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517617527/orkmiaoaxh8fuuqizjzb.png 2x" /> <h4>How to get the "post_identifier" <p dir="auto">In my <a href="https://steemit.com/utopian-io/@wil1liam/steemit-python-bot-or-part-1-how-to-find-someone-s-newest-post" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">first tutorial<span> I already explained how to get the newest post of a user. We can use a faster and updated version of this code to get the "post_identifier" (Thanks to <a href="/@emrebeyler">@emrebeyler!). <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517618004/mo3swb35aja4qthp61lr.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517618004/mo3swb35aja4qthp61lr.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1517618004/mo3swb35aja4qthp61lr.png 2x" /> <h2><a href="https://pastebin.com/4hvLpfCS" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Final Code (Pastebin.com) <h4>Curriculum <ul> <li><a href="https://steemit.com/utopian-io/@wil1liam/steemit-python-bot-or-part-1-how-to-find-someone-s-newest-post" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">PART 1: How to find someone's newest post <p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@wil1liam/steemit-python-bot-or-part-2-how-to-return-a-list-of-the-people-who-upvoted-your-post" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Sort:  

Hey, first I want to let you know, I saw your bot gave me test upvote. As long as I'm getting upvotes it is working, if I don't get an upvote, it is broken! ;-)))

On a more serious note, thanks, that you are adding in your tutorial json output, so we can see exactly where is the data you are extracting. How do you get such a nice output? In my case everything is clobbered in one long multi line and I can't make the sense of it. What tool do you use?

One more thing, your tutorials are well thought, with plenty of important information that beginners and wanna-be like me needs it, good job and I hope you continue ;-)

Thanks for the feedback! :)

<p dir="auto">To get a readable output I use python's build in module <code>pprint. <ol> <li><code> import pprint <li><code>pprint.pprint(your_json)

O cool, thanks will thy this out!

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

You can contact us on Discord.
[utopian-moderator]