Part 28: Uploading Images Via An API, Constructing Posts From A Template, Submitting Posts Directly To STEEM

steem-python.png

This tutorial is part of a series where different aspects of programming with steem-python are explained. Links to the other tutorials can be found in the curriculum section below. This part will explain how to upload images directly with an API and construct a post with the image.


Repository

https://github.com/steemit/steem-python

What will I learn

  • Installing and configuring imgurpython
  • Uploading an image to Imgur
  • Constructing a post from a template
  • Submitting a post to STEEM

Requirements

  • Python3.6
  • steem-python
  • imgurpython

Difficulty

  • basic

Tutorial

Preface

The STEEM Blockchain allows for direct interaction without using websites like steemit.com or busy.org. Instead a post can be uploaded directly to the Blockchain. If one wants to include an image in this post a 3rd party host for this image is required. Imgur has an API which allows for easy uploading of images with Python.

Setup

Download the files from Github. There 5 are files post_submitter.py which is the main file and takes two arguments account and filename, imgur.py which contains the code for uploading an image to Imgur, EXIF.py which extracts EXIF data from the image and post.json which is a JSON file containing information about the post and also 1 image file for testing.

Run scripts as following:
> python post_submitter.py juliank post.json

Installing and configuring imgurpython

Installation is simple using pip:

pip3 install imgurpython



Screenshot 2018-07-17 16.09.24.png



However to use the Imgur API an account is required. Signing up up is free. Go to the following link and create an account. After that a client has to be added for which you will receive a client_id and client_secret. Add these settings to the imgur.py file.


Screenshot 2018-07-17 16.09.51.png

Uploading an image to Imgur

The Imgur API is simple to use. After setting the account settings uploading an image is done by calling upload_from_path(), it requires the path of the filename. In this case anon is set to True so uploader is anonymous.

from imgurpython import ImgurClient

# account settings
client_id = ''
client_secret = ''

client = ImgurClient(client_id, client_secret)

Upon successful upload a JSON dict is returned containing all the information about the uploaded image.

{
    'id': 'kQPLkLs',
    'title': None,
    'description': None,
    'datetime': 1531837066,
    'type': 'image/jpeg',
    'animated': False,
    'width': 2222,
    'height': 1481,
    'size': 782184,
    'views': 0,
    'bandwidth': 0,
    'vote': None,
    'favorite': False,
    'nsfw': None,
    'section': None,
    'account_url': None,
    'account_id': 0,
    'is_ad': False,
    'in_most_viral': False,
    'has_sound': False,
    'tags': [],
    'ad_type': 0,
    'ad_url': '',
    'in_gallery': False,
    'deletehash': 'ppaO3Z1KasKq1lr',
    'name': '',
    'link': 'https://i.imgur.com/kQPLkLs.jpg'
}

From this dict the link is retrieved to be used in the post.

def upload_image(filename):
    # upload the image as anonymous
    image = client.upload_from_path(filename, anon=True)

    # retrieve only the image url from the retrieved json data
    # return the url
    url = image['link']
    return url

Constructing a post from a template

A post on STEEM requires more than a single image. The additional data required to submit a post is stored in post.json.

{
    "title": "Keeping Watch",
    "tags": "blog street photography zagreb croatia",
    "caption": "Or just enjoying the view that comes with the job?",
    "image": "1.jpg"
}

This data is taken from the JSON file and stored in variables to be submitted in a post.

def submit_post(self, account, filename):
    # Load post data
    post = json.load(open(filename))
    title = post['title']
    tags = post['tags']
    caption = post['caption']
    image = post['image']

The image is upload via the Imgur API returning a url.

# upload the image and retrieve the url
url = imgur.upload_image(image)

Optionally the image is processed for EXIF data which is put into atable, to read more about this follow this tutorial: Extracting EXIF (Meta)Data From Images With Python

# process the iamge for EXIF data to construct a table
table = exif.process_image(image)

With all the required data complete the body of the post be be constructed using a simple template. """ is used to create a multi-line string in Python and textwrap.dedent() is used to remove the tabs in front of each string. This allows for elegant formatting.

# Use template to construct a body for the post
body = textwrap.dedent(
    f"""
    <center>
    {caption}
    ---
    [![image]({url})]({url})
    ---

    {table}

    </center
    """)

Submitting a post to STEEM

Submitting a post to STEEM is done by calling the post() function on the Steem object. 8 out of the 11 arguments are optional, but can be customised depending on preferences.

def post(self,
         title,
         body,
         author,
         permlink=None,
         reply_identifier=None,
         json_metadata=None,
         comment_options=None,
         community=None,
         tags=None,
         beneficiaries=None,
         self_vote=False):

The title and tags were taken from post.json, the body was constructed and the account is set when calling post_submitter.py.

# Submit post to the Steem Blockchain
self.steem.post(title=title, body=body, author=account, tags=tags)

The function post() returns a dict with all the information about the post that was just submitted.

{
    'ref_block_num': 7964,
    'ref_block_prefix': 1056726494,
    'expiration': '2018-07-17T13:45:31',
    'operations': [
        ['comment', {
            'parent_author': '',
            'parent_permlink': 'photography',
            'author': 'juliank',
            'permlink': 'keeping-watch',
            'title': 'Keeping Watch',
            'body': '\n<center>\nOr just enjoying the view that comes with the job?\n---\n[![image](https://i.imgur.com/mSIa5CN.jpg)])(https://i.imgur.com/mSIa5CN.jpg)\n---\n\n\n<table>\n<tr><td>Settings</td><td><b>ISO 100 85\nmm f/7/5 1/640 sec </b></td></tr>\n<tr><td>Camera</td><td><b>SONY ILCE-7M3</b></td></tr>\n<tr><td>Lens</td><td><b>FE 85mm F1.4 GM</b></td></tr>\n<tr><td>Date</td><td><b>2018:07:17 15:31:41</b></td></tr>\n</table>\n\n</center\n',
            'json_metadata': '{"tags": ["photography", "blog", "zagreb", "street", "croatia"]}'
        }]
    ],
    'extensions': [],
    'signatures': ['2004881217455592399c83510cb4c55219f229ff681113972db4850e31634d700d74a02381aad27ac735cbaec0af2509336deed8d48a7f1844abb48518841f4f26']
}

Inside here information like the permlink which is required for upvoting the post can be found.

Running the script

Running the code will upload the image to imgur and construct a post that is submitted to the STEEM Blockchain

python post_submitter.py juliank post.json

Screenshot 2018-07-17 16.37.35.png


The constructed post:

<center>
Or just enjoying the view that comes with the job?
---
[![image](https://i.imgur.com/mSIa5CN.jpg)](https://i.imgur.com/mSIa5CN.jpg)
---


<table>
<tr><td>Settings</td><td><b>ISO 100 85
mm f/7/5 1/640 sec </b></td></tr>
<tr><td>Camera</td><td><b>SONY ILCE-7M3</b></td></tr>
<tr><td>Lens</td><td><b>FE 85mm F1.4 GM</b></td></tr>
<tr><td>Date</td><td><b>2018:07:17 15:31:41</b></td></tr>
</table>

</center>

Curriculum

Set up:

The code for this tutorial can be found on GitHub!

This tutorial was written by @juliank.

Sort:  

Thank you for these tutorials!

Thanks, you are welcome!

Thank you for your contribution.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you ... I very take advantage of your tutorials.

Congratulations @steempytutorials! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of comments

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - The results, the winners and the prizes

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Hey @steempytutorials
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thank you. Very helpful. Question: Is there any downside of hosting images on one's own webserver rather than using IMGUR or some other 3rd party server?

Coin Marketplace

STEEM 0.33
TRX 0.11
JST 0.034
BTC 66363.68
ETH 3207.73
USDT 1.00
SBD 4.27