steem-python for dummies #12 - Lookups (Discussion Queries)

in #utopian-io7 years ago (edited)

Hello,

This is the 12. post of "steem-python for dummies" series. If you didn't read the older ones take your time and have a look.

  1. steem-python for dummies #1 - Introduction and Basic Operations
  2. steem-python for dummies #2 - Playing with account data
  3. steem-python for dummies #3 - Coding an upvote bot
  4. steem-python for dummies #4 - Private Memos
  5. steem-python for dummies #5 - Bundling Blockchain Operations
  6. steem-python for dummies #6 - Delegating
  7. steem-python for dummies #7 - Creating Accounts
  8. steem-python for dummies #8 - Calculating Post Rewards
  9. steem-python for dummies #9 - Creating orders in the market
  10. steem-python for dummies #10 - Listening accounts
  11. steem-python for dummies #11 - Listening new blocks

In this post, we will explore a couple of calls on steem daemon.

Discussions API


Do you know how Steemit filters the posts in the Condenser interface? Even though I didn't check it implicitly how they're doing, I am pretty much sure they're using discussions API in the steem daemon.

Getting Trending Posts

from steem import Steem
s = Steem()
query = {"limit": 5} # limit for 5 posts
for p in s.get_discussions_by_trending(query):
    print("%s - %s" % (p["author"], p["permlink"]))

Let's check if that list matches with Steemit interface:


Looks like we have the same list!

Tip: If you want to filter specific tags, just add a tag parameter to the query dict.

from steem import Steem
s = Steem()
query = {"limit": 5, "tag": "python"} # limit for 5 posts
for p in s.get_discussions_by_trending(query):
    print("%s - %s" % (p["author"], p["permlink"]))

This will return last 5 trending posts on #python category.

All discussion related functions

  • get_discussions_by_trending
  • get_comment_discussions_by_payout
  • get_post_discussions_by_payout
  • get_discussions_by_created
  • get_discussions_by_active
  • get_discussions_by_cashout
  • get_discussions_by_payout
  • get_discussions_by_votes
  • get_discussions_by_children
  • get_discussions_by_hot
  • get_discussions_by_feed
  • get_discussions_by_blog
  • get_discussions_by_comments
  • get_discussions_by_promoted

All of these functions gets a query dict in steem-python which should map to original discussion_query struct defined at steemd database api.

So, after the first example about trending, you can pretty much use all functions with the same structure on query dict.


discussion_query definition on steem source code

Tip: Currently, select_authors, select_tags, filter_tags arguments in discussion queries are not supported. See the related issue.

More examples

Promoted list

query = {"limit": 10}
for p in s.get_discussions_by_promoted(query):
    print("%s - %s" % (p["author"], p["permlink"]))

By Highest Payout

query = {"limit": 10}
for p in s.get_discussions_by_payout(query):
    print("%s - %s" % (p["author"], p["permlink"]))

Going back to history

Discussion filter has a maximum limit on limit parameter. It's 100. What if we want to go back?

It has a simple pagination logic.

  1. Get the initial 100 posts.
  2. Select the 100.post and keep in mind its author and permlink.
  3. Get another 100 posts, however add two more parameters. (start_author, start_permlink)
query = {
    "limit": 10,
    "start_author": "canadian-coconut",
    "start_permlink": "the-nightmare-before-christmas-familyprotection-series-anna-s-family-story-part-i"
}
for p in s.get_discussions_by_trending(query):
    print("%s - %s" % (p["author"], p["permlink"]))

canadian-coconut's this post is currently 10. place in the trending. If I start the query with that post, I will get the trending posts between 10 and 20.


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



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @emrebeyler I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Ur giving a great information about the steem
Hoping to see more news like this

Thank you for the contribution. It has been approved.

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

Thanks for this article; it was helpful understand what wasn't very well documented in the API.

Birthdaypost !BEER



Hey @emrebeyler, here is a little bit of BEER from @isnochys for you. Enjoy it!

Coin Marketplace

STEEM 0.31
TRX 0.38
JST 0.059
BTC 95746.69
ETH 3711.36
SBD 4.15