Posts

Automated Hive Posts Using Beem

avatar of @geekgirl
25
@geekgirl
·
·
0 views
·
4 min read

This post is a continuation of my FinViz project from yesterday. I was trying to create custom reports for potential stock picks of the day. The goal is to go through thousands of stocks programmatically and sort out some stocks that fit certain criteria. For the purposes of this experiment, I am using my triangle indicator, about which I have written before. Nothing special about the indicator, so I won't provide a link to the post. However, the idea is to apply any other indicators to this project in the future.

Normally, triangle indicator have very few picks a day, about 2-6 picks. Once these picks are identified, then I get charts and other additional data about this stock. Then put them together in a report. The report is very boring, and only has some value for me. Most others would consider it a useless spam.

Now the next step is to store the report or publish online to access on the go and have a chronological records for future reference. I had to try posting on Hive programmatically from an alt account (librarian) for educational purposes and experimentation. After three hundred and thirty lines of code, I was able to finish the project. Of course, the report looks ugly. But it serves the purpose for now. This is not a permanent solution. I want to use custom_json instead in the future.

Beem is a python library created and maintained by @holger80, and is an awesome tool to interact with Hive blockchain. I am not sure if Holger is still maintaining the library. For now it works. Let's look at the code first. Then I will try to explain what is happening in the code. This is not the full code for the project mentioned above, but rather a template to programmatically publish Hive posts using Beem.

from beem.imageuploader import ImageUploader 
from beem import Hive 
from beem.nodelist import NodeList 
 
nodelist = NodeList() 
nodelist.update_nodes() 
nodes = nodelist.get_hive_nodes() 
 
#wif = getpass.getpass(prompt='Enter your Hive active key:') 
wif = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 
hive = Hive(node=nodes, keys=[wif]) 
author = 'librarian' 
image_path = '/Users/librarian/project/charts/COIN_1642474232.png' 
image_name = 'COIN' 
image_uploader = ImageUploader(blockchain_instance=hive) 
img_link = image_uploader.upload(image_path, author, image_name=image_name) 
 
title = "COIN stock price data" 
body = f''' 
This is COIN price data start 
 
![{image_name}]({img_link['url']}) 
 
This is COIN price data end 
''' 
 
parse_body = True 
self_vote = False 
tags = ['stocks', 'coin', 'test'] 
 
hive.post(title, body, author=author, tags=tags, parse_body=parse_body, self_vote=self_vote) 

First we import modules we need from Beem. ImageUploader will help with uploading images, NodeList will be used to get the Hive nodes.

In this example there are two ways to provide private posting keys. First one is commented out, but is the better way, because you will not need to keep the private posting key within the code. For testing purposes I used the second way of just assigning the posting key to a variable wif. Then we create a hive instance proving nodes and private keys.

To upload an image, we will need a full path to the image, and an image name, which usually is the image file name, and author name. After creating an instance of an ImageUploader, which requires a blockchain instance, in this case it is hive variable, we upload the image using the .upload() method. This is where we use image path, image name and author names. This will return an image link, which we can use in the body of the post. img_link['url'] will have the url to the uploaded image.

Now that image is ready, we need to create a title for the post and store it in a variable. We will need a list of tags, if we are using any. Set parse_body to True, and self_vote to False. Most importantly we need to create a body text with proper markdown and include image url in the body text.

Once all of these parts of the post are ready, we can broadcast this transactions with .post() method of the hive instance. Very last line of the code executes the posting transaction, includes all the parts of the post as arguments. When we ran the post, if everything goes well, the post will be published on Hive blockchain.

This is a basic template for publishing Hive posts. There are more interesting parameters like json_metadata and comment_options that can be configured depending on what we are trying to do. While I was able to successfully publish a post with Beem and even integrate Beem in a creating stock reports project, there is more I need to learn.

For example, I wanted to set decline post rewards for these automated posts. However when I tried to do that I got an error. I will have to experiment with this and other feature of Hive posts. To learn more about Beem and how to use it, feel free to read the Beem documentation.

If you have experience with Beem and know how I can add decline post rewards, please let me know in the comments. My understanding is this configuration will go inside of comment_options by setting "allow_votes: False". However, this caused an error.

Posted Using LeoFinance Beta