<center></center> This tutorial is a continuation of [Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python), so make sure to read this before starting this tutorial. Today we will learn how to stream the blockchain, what is actually broadcasted to the blockchain and how we can filter it to suit our needs! --- #### What Will I Learn? - How to stream the blockchain - What posts on the blockchain look like - How to print information about a post - How to filter these posts #### Requirements - Python3.6 - steem-python #### Difficulty - Basic --- ### Tutorial #### Streaming the blockchain To access the blockchain and read data from it we can simply import the `Blockchain` class from `steem-python` and use its [`stream()`](http://steem.readthedocs.io/en/latest/tools.html#steem.blockchain.Blockchain.stream) function! This function takes a list of operations to filter for, so since we are interested in posts we can simply filter for that ``` from steem.blockchain import Blockchain blockchain = Blockchain() stream = blockchain.stream(filter_by=['comment']) for post in stream: # DO SOMETHING ``` #### What do posts look like? So, now we have a stream that we can iterate over with a *for loop*, but what do posts actually look like? Posts on the blockchain are in a JSON format. To find out what this looks like we can simply print them! We can use `json.dumps()` to print them in a valid JSON format, but before we do this, we need to create a `converter()` function that can handle objects of type `datetime` since they aren't serialisable ``` import json import datetime blockchain = Blockchain() stream = blockchain.stream(filter_by=['comment']) def converter(object_): if isinstance(object_, datetime.datetime): return object_.__str__() for post in stream: print(json.dumps(post, default=converter)) break ``` which outputs the example that can be found [here](https://raw.githubusercontent.com/amosbastian/steempy-tutorials/master/part_2/example_post_1.json). As you can see it doesn't include that much useful information. This is because we should map posts on the blockchain into a `Post` object! ``` from steem.post import Post stream = map(Post, blockchain.stream(filter_by=['comment'])) ``` Now that we've changed this we can run our program again, and it outputs something much more useful to us, as you can see in the example [here](https://raw.githubusercontent.com/amosbastian/steempy-tutorials/master/part_2/example_post_2.json). So much useful information! --- **Note:** you can also use the `post.export()` function to get a post as JSON, but for the sake of this tutorial I have used `json.dumps()` as it makes it easier to print the JSON in a more readable format. #### Printing information about a post We now know what kind of information is contained in a post, so we can decide which part we want to print! For example, it would be cool to print the author's name, the title of their post and the tags they used. Looking at the the [example](https://raw.githubusercontent.com/amosbastian/steempy-tutorials/master/part_2/example_post_2.json) we can see it has the keys `"author"`, `"title"` and `"tags"`. To print that we can simply use the following code in our *for loop* ``` author = post["author"] title = post["title"] tags = ", ".join(post["tags"]) print("{} posted {} with tags {}".format(author, title, tags)) ``` which for example outputs ``` iseektruth posted with tags ``` Obviously something has gone wrong, because when we looked at the [example](https://raw.githubusercontent.com/amosbastian/steempy-tutorials/master/part_2/example_post_2.json) it clearly had an author, title and tags property, so why isn't it printing it? This is because posts on the blockchain can be either posts or comments, and comments don't have a title or tags! In the next section we will see how we can prevent this from happening! #### Filtering the blockchain To filter posts on the blockchain we can simply use an *if statement*! For example, if we want to make sure that the posts we print are actual posts and not comments we can use the `is_main_post()` function ``` for post in stream: if post.is_main_post(): author = post["author"] title = post["title"] tags = ", ".join(post["tags"]) print("{} posted {} with tags {}".format(author, title, tags)) ``` which outputs the following ``` onegood posted The most beautiful period with tags life, life, photography, children ``` Great, it works! You can filter for anything you want! For example, if you want to only print posts with the tag `utopian-io` you can use the following code ``` for post in stream: tags = post["tags"] if post.is_main_post() and "utopian-io" in tags: author = post["author"] title = post["title"] tags = ", ".join(post["tags"]) print("{} posted {} with tags {}".format(author, title, tags)) ``` which outputs this for example ``` jestemkioskiem posted Moderation guidelines for Utopian's translation category. with tags utopian-io, utopian-io, utopian-io tobias-g posted Indication of edited post with tags utopian-io, utopian-io, utopian-io, suggestion, busy richardoccas posted Utopian-io – Bug report: The chosen language is not saved with tags utopian-io, utopian-io, bug, open-source, language, contribution kad posted New Logo Design For Thomas with tags utopian-io, utopian-io, utopian-io, graphics, design, logo ``` Now you know how to filter posts on the blockchain! You could now try to filter posts by specific authors or the amount of votes for example, the options are limitless! --- **Note:** by the time posts on the blockchain reach your program they could already be deleted, so you should make sure to check this. You can do this by putting everything in a *while loop* and your *for loop* in a *try statement* with an *except clause*, like so ``` while True: try: for post in stream: # DO SOMETHNIG except Exception as error: print(repr(error)) continue ``` #### Curriculum - [Part 0: How To Install Steem-python, The Official Steem Library For Python](https://utopian.io/utopian-io/@amosbastian/how-to-install-steem-python-the-official-steem-library-for-python) - [Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python) #### Credits This tutorial was written by @amosbastian in conjunction with @juliank. --- The code for this tutorial can be found on [GitHub](https://github.com/amosbastian/steempy-tutorials/tree/master/part_2)! <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@steempytutorials/part-2-how-to-stream-and-filter-the-blockchain-using-steem-python">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>
author | steempytutorials | ||||||
---|---|---|---|---|---|---|---|
permlink | part-2-how-to-stream-and-filter-the-blockchain-using-steem-python | ||||||
category | utopian-io | ||||||
json_metadata | {"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":84843862,"name":"steem-python","full_name":"steemit/steem-python","html_url":"https://github.com/steemit/steem-python","fork":false,"owner":{"login":"steemit"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","python","tutorial","steemdev","programming"],"users":["steempytutorials","amosbastian","juliank."],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515873755/xoti08nqe7uilkeoqdnl.png","https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python","http://steem.readthedocs.io/en/latest/tools.html#steem.blockchain.Blockchain.stream","https://raw.githubusercontent.com/amosbastian/steempy-tutorials/master/part_2/example_post_1.json","https://raw.githubusercontent.com/amosbastian/steempy-tutorials/master/part_2/example_post_2.json","https://utopian.io/utopian-io/@amosbastian/how-to-install-steem-python-the-official-steem-library-for-python","https://github.com/amosbastian/steempy-tutorials/tree/master/part_2"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515873755/xoti08nqe7uilkeoqdnl.png"],"moderator":{"account":"shreyasgune","time":"2018-01-14T05:16:10.412Z","reviewed":true,"pending":false,"flagged":false}} | ||||||
created | 2018-01-13 20:05:15 | ||||||
last_update | 2018-01-14 11:04:51 | ||||||
depth | 0 | ||||||
children | 7 | ||||||
last_payout | 2018-01-20 20:05:15 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 30.126 HBD | ||||||
curator_payout_value | 11.770 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 7,127 | ||||||
author_reputation | 31,094,047,689,691 | ||||||
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 29,304,946 | ||||||
net_rshares | 6,222,186,210,014 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
drakernoise | 0 | 622,019,665 | 100% | ||
fratheone | 0 | 1,286,474,726 | 100% | ||
gutzofter | 0 | 22,227,888,189 | 100% | ||
remlaps2 | 0 | 2,757,571,144 | 100% | ||
cub2 | 0 | 59,174,528 | 100% | ||
nicnas | 0 | 19,765,785,017 | 50% | ||
juliank | 0 | 384,197,527,465 | 30% | ||
vot | 0 | 596,533,557 | 100% | ||
davidmendel | 0 | 14,992,851,289 | 76% | ||
tabris | 0 | 391,042,729,418 | 100% | ||
jamescash | 0 | 311,659,371 | 100% | ||
akhyar29 | 0 | 0 | 100% | ||
muksal.mina | 0 | 559,198,445 | 100% | ||
utopian-io | 0 | 5,376,762,754,832 | 3.1% | ||
reyha | 0 | 1,601,897,614 | 19% | ||
rimicane | 0 | 2,301,798,379 | 50% | ||
amosbastian | 0 | 1,693,078,759 | 100% | ||
casberp | 0 | 486,949,823 | 100% | ||
steempytutorials | 0 | 241,006,800 | 100% | ||
mama-andrzejka | 0 | 574,561,039 | 100% | ||
pavolactico | 0 | 104,749,954 | 100% | ||
anitatmj | 0 | 0 | 100% | ||
chaene | 0 | 0 | 100% | ||
opelkus | 0 | 0 | 100% |
These old tutorials are very hard to follow because you can never find the next one (because at the time of posting it did not exist yet.) By the time I'm done with one I need to go back and figure out where I am in retrospect to the next one. No that big a deal. What would be helpful would be some kind of external site with all the links for future blogs. I THINK STEEMIT NEEDS A LINK BOX FOR YOU TO CONNECT ALL YOUR LONG BLOGS!!!~)) DO YOU HAVE ANY BEEMPY TUTORIALS???
author | coininstant |
---|---|
permlink | pt2r2y |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2019-06-14 05:54:36 |
last_update | 2019-06-14 05:56:21 |
depth | 1 |
children | 1 |
last_payout | 2019-06-21 05:54:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 473 |
author_reputation | 87,938,489,196,963 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 86,601,795 |
net_rshares | -75,559,264,868 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
iflagtrash | 0 | -20,113,164,746 | -2% | ||
abusereports | 0 | -55,446,100,122 | -2% |
You're a waste of space. You've been flagged like the trash you are.
author | iflagtrash |
---|---|
permlink | iflagtrash-re-coininstantpt2r2y |
category | utopian-io |
json_metadata | "" |
created | 2019-06-14 05:56:18 |
last_update | 2019-06-14 05:56:18 |
depth | 2 |
children | 0 |
last_payout | 2019-06-21 05:56:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 69 |
author_reputation | 14,709,692,204,215 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 86,601,866 |
net_rshares | -2,580,558 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
scruffyjunkies | 0 | -2,580,558 | -100% |
Thank you for the contribution. It has been approved. You can contact us on [Discord](https://discord.gg/uTyJkNm). **[[utopian-moderator]](https://utopian.io/moderators)**
author | shreyasgune |
---|---|
permlink | re-steempytutorials-part-2-how-to-stream-and-filter-the-blockchain-using-steem-python-20180114t051712559z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-14 05:17:12 |
last_update | 2018-01-14 05:17:12 |
depth | 1 |
children | 2 |
last_payout | 2018-01-21 05:17:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 1.821 HBD |
curator_payout_value | 0.595 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 172 |
author_reputation | 4,924,803,411,962 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,383,066 |
net_rshares | 280,729,666,896 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
shreyasgune | 0 | 114,117,632,742 | 100% | ||
amosbastian | 0 | 1,760,074,357 | 100% | ||
utopian.tip | 0 | 164,851,959,797 | 20.37% |
Thanks!
author | amosbastian |
---|---|
permlink | re-shreyasgune-re-steempytutorials-part-2-how-to-stream-and-filter-the-blockchain-using-steem-python-20180114t102040846z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-14 10:20:42 |
last_update | 2018-01-14 10:20:42 |
depth | 2 |
children | 0 |
last_payout | 2018-01-21 10:20:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 7 |
author_reputation | 174,473,586,900,705 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,426,054 |
net_rshares | 0 |
Hey @shreyasgune, 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!
author | utopian.tip |
---|---|
permlink | re-re-steempytutorials-part-2-how-to-stream-and-filter-the-blockchain-using-steem-python-20180114t051712559z-20180114t100243 |
category | utopian-io |
json_metadata | "" |
created | 2018-01-14 10:02:45 |
last_update | 2018-01-14 10:02:45 |
depth | 2 |
children | 0 |
last_payout | 2018-01-21 10:02:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 161 |
author_reputation | 238,310,597,885 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,423,222 |
net_rshares | 0 |
### Hey @steempytutorials 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! - This is your first accepted contribution here in Utopian. Welcome! #### 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. <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> [](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**
author | utopian-io |
---|---|
permlink | re-steempytutorials-part-2-how-to-stream-and-filter-the-blockchain-using-steem-python-20180114t175221455z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-14 17:52:21 |
last_update | 2018-01-14 17:52:21 |
depth | 1 |
children | 0 |
last_payout | 2018-01-21 17:52:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,533 |
author_reputation | 152,955,367,999,756 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 29,513,456 |
net_rshares | 0 |
Your code example works, thank you. Say I would like to check (I have list of authors) if any of them posted in last i don't know, say few hundred posts what was already posted on stream. I can't use this code, since this is live, ongoing stream, correct? > stream = map(Post, blockchain.stream(filter_by=['comment'])) > for post in stream: How would I go about this?
author | veleje |
---|---|
permlink | re-steempytutorials-part-2-how-to-stream-and-filter-the-blockchain-using-steem-python-20180127t215955366z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-27 21:59:54 |
last_update | 2018-01-27 22:00:30 |
depth | 1 |
children | 0 |
last_payout | 2018-02-03 21:59:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.068 HBD |
curator_payout_value | 0.018 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 370 |
author_reputation | 490,220,361,761 |
root_title | "Part 2: How To Stream And Filter The Blockchain Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,843,179 |
net_rshares | 10,710,689,676 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
veleje | 0 | 10,710,689,676 | 100% |