Part 10: Use Urls To Retrieve Post Data And Construct A Dynamic Post With Steem-Python

This tutorial is part of a series where we explain different aspects of programming with steem-python
. Links to the other tutorials can be found in the curriculum section below. This part will focus on how to use urls to retrieve data from the Steem blockchain and construct a post.
What will I learn
- How to copy multiple urls from your browser
- Construct a permlink from a url
- Retrieve post data from the blockchain
- Construct a dynamic post
Requirements
- Python3.6
steem-python
Difficulty
- Basic
Tutorial
Setup
Start by downloading the files from Gitub. For this tutorial there are 2 files. A textfile in which the urls are stored and the python script itself. For this example we will be constructing a post for the winners of a photography contest. From each post we want to retrieve the title, author and the winning photo.
winners.txt
https://steemit.com/landscapephotography/@rinawonderlands/every-mountain-top-is-within-reach-if-you-just-keep-climbing
https://steemit.com/landscapephotography/@uadigger/5peq5n-winter-in-the-carpathians 2
https://steemit.com/landscapephotography/@matajingga/landscape-photography-mount-batok 1
As some authors post multiple photos in an article there is a second variable which indicates the index of the photo to retrieve. The setup is then like: <url> <photo_index>
where the standard photo_index
is set to 0.
Run the script by with the filename as the first argument
> python main.py winners.txt
Copying multiple urls from your browser
Copy All Urls is a chrome browser extension that allows you to copy all your open tab urls. Perfect when dealing with a large amount of urls.
Constructing the permlink
The unique link for each post, the permlink, can be constructed by removing everything before the @
. It does not matter from which platform you take the link. It works for all platforms, for example steemit.com or busy.org
<platform url>/@<permlink>
Retrieve post data from the blockchain
The permlink
can then be used to construct a Post object from which all the data can be retrieved.
from steem.post import Post
post = Post(permlink)
Using post.export() will retrieve all the data from the post. As can be seen here that is a lot of data. The title
and author
are immediately accessible. While the images are stored underjson_metadata




This is were the photo_index
is used to retrieve the correct image from ['json_metadata']['image']
Construct a dynamic post
By constructing a post from a header, body and a footer a dynamic post can be constructed where the length varies depending on the input.
header = "<center>\n# Test selection\n***\n"
body = ""
footer = "End of test selection\n</center>"
for each url:
body += "Title: {}<br>Author: @{}<br>Permlink: {}<br>Image:{}\n***\n".format(title, author, permlink, image)
post = header + body + footer
Running the code
Using the accompanied winners.txt
file will generate a post containing the 3 photos with the relevant title
, author
and permlink
. Feel free to use your own links or adjust which variables are used to generate the post. Use post.export()
to see which data can be retrieved.
Curriculum
- Part 0: 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
- Part 2: How To Stream And Filter The Blockchain Using Steem-Python
- Part 3: Creating A Dynamic Autovoter That Runs 24/7
- Part 4: How To Follow A Voting Trail Using Steem-Python
- Part 5: Post An Article Directly To The Steem Blockchain And Automatically Buy Upvotes From Upvote Bots
- Part 6: How To Automatically Reply To Mentions Using Steem-Python
- Part 7: How To Schedule Posts And Manually Upvote Posts For A Variable Voting Weight With Steem-Python
- Part 8: How To Create Your Own Upvote Bot Using Steem-Python
- Part 9: How To Calculate A Post's Total Rewards Using Steem-Python
The code for this tutorial can be found on GitHub!
This tutorial was written by @juliank in conjunction with @amosbastian.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @emrebeyler, 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!
I Looooooooooooooove Yooooooooooooooooou Booooooss
Thank you very much for the submission of the tutorial, The exact contents of the post is very meaningful, and useful to all of us, sorry because I am new Beginner in steemit, I really hope there is help, directions,and your fromresponse,
Hey @steempytutorials I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
I know this is old... so some stuff might have changed and be changing... but as for the time of this comment :
^This is critical for making that stuff work.
Once more I might be doing this all wrong but I think the steem-python libraries require you do it like this: