Part 28: Uploading Images Via An API, Constructing Posts From A Template, Submitting Posts Directly To STEEM
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
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.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"""
{caption}
---
[]({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\nOr just enjoying the view that comes with the job?\n---\n[])(https://i.imgur.com/mSIa5CN.jpg)\n---\n\n\n\nSettings ISO 100 85\nmm f/7/5 1/640 sec \nCamera SONY ILCE-7M3 \nLens FE 85mm F1.4 GM \nDate 2018:07:17 15:31:41 \n
\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
The constructed post:
<center>
Or just enjoying the view that comes with the job?
---
[](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>SONY ILCE-7M3</ b></td></tr>
<tr><td>Lens</td>FE 85mm F1.4 GM</ b></td></tr>
<tr><td>Date</td>2018:07:17 15:31:41</ b></td></tr>
</table>
</center>
Curriculum
Set up:
- Part 0: How To Install Steem-python, The Official Steem Library For Python
- Extracting EXIF (Meta)Data From Images With Python
The code for this tutorial can be found on GitHub!
This tutorial was written by @juliank.
Leave Part 28: Uploading Images Via An API, Constructing Posts From A Template, Submitting Posts Directly To STEEM to:
Read more #utopian-io posts
Best Posts From Steem-Python
We have not curated any of steempytutorials's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Steem-Python
- Python #7 part 1 - Advent of Code 2018 - Topological Sort
- Python #6 - Advent of Code 2018 - Distances between points
- Python #5 - Solving Puzzles from Advent of Code 2018 - String/character functions
- Learn how to program with Python #4 - Solving Puzzles from Advent of Code 2018
- Learn how to program with Python #3 - Solving Puzzles from Advent of Code 2018
- Learn how to program with Python #2 - Solving Puzzles from Advent of Code 2018
- Learn how to program with Python #1 - Solving Puzzles from Advent of Code 2018
- Part 4: Comparing orderbooks from different exchanges in real time
- Part 3: Connecting and managing multiple websocket streams in parallel
- Part 2: Manage local 'STEEM' orderbook via websocket stream from exchange