Listing All My Post Links Using Beempy

in hive-175254 •  2 months ago  (edited)

Why would I want to list all my previous posts?

Sometimes I wish I can find my old post, refer to it and link it to my current post.

Searching for old posts using Hive's front end is hard, slow and almost impossible. Using Hive.blog and eSteem App was suppose to be the simplest option. After scrolling down to view posts older than 2 months it seems to get stuck. If you can see posts older than 2 months after waiting for few minutes, consider yourself lucky.

2020-05-09-HiveBlogSlow.png

What is the Solution?

Since I can't use the frontend and copy and pasting. So I have to use Hive library to program something to do the job.

Choosing which library to use

According to hiveprojects.io there are currently 5 libraries we can use to communicate with the Hive Blockchain:

  1. Python Hive Library by @pharesim
  2. Go-Hive b @jrswab
  3. lightwave by @emrebeyler
  4. BEEM by @holger80

After a few moment reading through 4 of the libraries I choose only Python because it is the easiest.

Python is so easy, all you need to know is the keywords like using the command line. It is easy, so if you want to learn more can go to https://www.learnpython.org/ and learn it for FREE. You can search it on YouTube if you prefer to learn by watching videos.

Finally, out of all the four I choose BEEM.

Installing Beem

Before you can Install Beem, you need to have python because it is a python library.

You can easily install Beem with pip

pip install beem

If you are successful in isntalling the library, you can start coding.

Since I am having problem with latest version of Python. I am using Anaconda Python Distro to install Beem and all the library I need.

After you installed Anaconda, open the Anaconda Powershell.

Once you launched the Powershell, type commands line like below:
conda config --add channels conda-forge

That command was to add conda-forge to your channels so you can install beem. Once the conda-forge channel has been enabled, beem can be installed with:
conda install beem

Thats all for the installation, but if you want more information on installing in different ways, please refer to the official installation manual.

The Code

1- Import the library

I will try to explain the code I made, which can be view directly in my github repository.

First, you need to create a new python file. After you create a new python file, insert the library needed on top.

from beem.account import Account
from beem.comment import Comment
from beem.exceptions import ContentDoesNotExistsException

2- Add few important variables

Replace the username in the code with your Hive username.

account = Account(username)
c_list = {} # list of the post
count = 0 

3- Open or Create file

A line to open or create the markdown file to insert the post links. Must have the utf-8 encoding because without that the file can only accept ascii.

f = open("hivepostlist.md",'w',encoding="utf-8")

4- The rest of the code to retrieve all your post

The code below were literally taken from @holger80 example in the official BEEM docs on displaying of all posts. Then added some more properties in order to write a list of links in a Markdown file.

  for c in map(Comment, account.history(only_ops=["comment"])):
    if c.permlink in c_list:
      continue
    try:
         c.refresh()
    except ContentDoesNotExistsException:
         continue
    c_list[c.permlink] = 1
    if not c.is_comment():
         title = ""
         title = title.join(c.title.splitlines()) # Settled the titles with newline problem
             count +=1
         print(str(count) + title)
         f.write(str(count) + ". [" + title + "]" +
         "(" +"https://hive.blog/"+
          c.parent_permlink + "/@"+username+"/"+
           c.permlink + ")" + "\n")
f.close()

You Are Done

When you run the python file with python yourfilename.py you will generate a markdown file like the picture below which is available in my github repository.

List.png

If you want to just download and try the code, just get the file in my github repository.

Next, I will update a few things in the code like adding a progress bar and timer just to measure how long does it take to list down all the posts.

Thank you @holger80 for the cool library and thank you for reading.

Please, follow, upvote and comment below.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Informasi menarik.

Yay!
Your post has been boosted with ESTM. Keep up the good work!
Dear reader, Install Android: https://android.esteem.app, iOS: https://ios.esteem.app mobile app or desktop app for Windows, Mac, Linux: https://desktop.esteem.app
Learn more: https://esteem.app
Join our discord: https://discord.me/esteem