Previously I showed how you can automate some Snaps posting.
You can see that tutorial here.
One thing missing from that code was the ability to post photographs.
Pictures are a big use of Snaps, it lends itself well to that purpose, and especially due to the fact a lot of Hive folks are not happy when you post just an image and a caption as an actual post.
Lots of dapps have come and gone that were Instagram replacements, I quite like photos as social media, so I am happy we have Snaps now for this.
To keep things simple your photo needs to already exist in the local directory. I am not including any code for taking a photo or selecting the image using a finder!
Beem Image Upload Library
Beem includes the feature for us, so it is a simple case of importing the appropriate classes with from beem.imageuploader import ImageUploader
You could of course use Imgur or whatever, but this keeps things "on the chain" as it were.
Uploading the Photo
Next we create our object
image_uploader = ImageUploader(hive_instance=hive)
You will note you have to authenticate first.
Don't worry, the full code will be shared at the end of the article, just trying to keep to what is different.
Now we can do the upload:
uploaded_image = image_uploader.upload(image_filename, account.name, image_name=image_filename)
image_url = uploaded_image["url"]
print("Uploaded your image as", image_url)
We get the URL back from the uploaded image in the ['url']
value, and for ease of sharing I just use that twice when I create the markdown text string:
picture_markdown = f"![{image_url}]({image_url})"
Really we should have proper alt text here but I am sure you can add that without my help.
Full Code for Posting Image Snaps
Here is a full script for posting images to Snaps from your command line using Python:
# imports
from beem import Hive
from beem.discussions import Query, Discussions_by_blog as Discussions
from beem.comment import Comment
from beem.account import Account
from beem.imageuploader import ImageUploader
# Log in
wif = input("Enter your posting key: ")
hive = Hive(keys=wif)
account = Account("makerhacks", blockchain_instance=hive)
# Upload the pic
image_uploader = ImageUploader(hive_instance=hive)
image_filename = input("Enter the image filename")
uploaded_image = image_uploader.upload(image_filename, account.name, image_name=image_filename)
image_url = uploaded_image["url"]
print("Uploaded your image as", image_url)
# Snap container
q = Query(limit=1, tag="peak.snaps")
posts = Discussions(q)
details = Comment(posts[0])
# Post our snap!
perm = details['authorperm']
comment = Comment(perm, hive_instance=hive)
picture_markdown = f"![{image_url}]({image_url})"
comment.reply(picture_markdown + "This picture was posted via Python using the Beem library!", author=account.name)