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

in #utopian-io8 years ago (edited)

steem-python.png

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.

Screenshot 2018-01-21 18.02.09.png

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


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

Sort:  

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

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • 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

I know this is old... so some stuff might have changed and be changing... but as for the time of this comment :

 from steem.steemd import Steemd 

^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:

import steem
from steem import Steem
from steem.post import Post
from steem.steemd import Steemd
stmd = Steemd()
#....
#....
post = Post('permlink', stmd)
# or this might also work:
post = Post('permlink', Steemd())

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.036
BTC 109207.95
ETH 4399.20
USDT 1.00
SBD 0.84