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

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

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now