Live Feed of Posts and Comments - Beginner Python Tutorial

in #steem5 years ago

streampython.jpg

Decentralization requires a majority of users to be involved, but awkward interfaces on Steemit can limit discovery and awareness. Python tools can allow novice computer users to view and interact with the blockchain in powerful ways. I will demonstrate how to create a live feed of posts and comments in this tutorial for beginners.

The code was pieced together from the following free resources.

readthedocs.io
developers.steem.io
UtopianDB

If running Microsoft Windows, setup a free Linux virtual machine. The system requirements are low so default settings are fine. I downloaded VMware Workstation 15 Player and Ubuntu 18.04 iso file. Install VMware and create a new virtual machine with Ubuntu iso file. After installing Linux I run Software Updater. Although this tutorial only views information on the blockchain, you may want to vote or transfer STEEM in the future by creating a wallet and importing keys, so open Terminal and run the following commands:

sudo apt-get update
sudo apt-get install curl
sudo apt-get install build-essential
sudo apt-get install libssl-dev
sudo apt-get install python-dev
sudo apt-get install python-pip
pip install -U cryptography
pip install -U steem

The native Text Editor can be used to write your Python code. Copy and save these programs as streamposts.py and streamcomments.py, or whatever names you like.

streamposts.py

from steem import Steem
from steem.blockchain import Blockchain
from steem.account import Account
from steem.post import Post
import json

steem = Steem()
blockchain = Blockchain()
stream = blockchain.stream(filter_by=['comment'])

def converter(object_):
    if isinstance(object_, datetime.datetime):
        return object_.__str__()

while True:
    try:
        for post in stream:
            postauthor = post["author"]
            posttitle = post["title"]
            permlink = post["permlink"]
            postlink = "@" + postauthor + "/" + permlink
            if len(posttitle) > 0:
                print("https://www.steemit.com/" + postlink)
                break

    except Exception as error:
        print(repr(error))
        continue

streamposts.jpg


streamcomments.py

from steem import Steem
from steem.blockchain import Blockchain
from steem.account import Account
from steem.post import Post
import json

steem = Steem()
blockchain = Blockchain()
stream = blockchain.stream(filter_by=['comment'])

def converter(object_):
    if isinstance(object_, datetime.datetime):
        return object_.__str__()

while True:
    try:
        for post in stream:
            postauthor = post["author"]
            posttitle = post["title"]
            postbody = post["body"]
            permlink = post["permlink"]
            postlink = "@" + postauthor + "/" + permlink
            if len(posttitle) < 1:
                print("https://www.steemit.com/" + postlink)
                print(postbody + '\n')
                break

    except Exception as error:
        print(repr(error))
        continue

streamcomments.jpg

To run either program, open Terminal and type "python streamposts.py" or "python streamcomments.py". To exit press Ctrl + C or close Terminal. You can use the information in the tutorials to play around and create your customized feed. This can aid discovering content and regulating misbehavior such as spam. As a beginner, I welcome input on how to improve this code and explore new opportunities.

Sort:  
Loading...



HIVE IS ALIVE!!!

JOIN US, YOU'LL HAVE EXACTLY THE SAME BALANCE AS YOU HAVE HERE ON STEEM WITHOUT THE CENTRALIZATION AND CENSORSHIP!!

https://hive.blog