Taking my mind off crypto prices with some coding - Related links builder with Python!

in #steem6 years ago (edited)

steem-python.png

Hello Steemians! The crypto market is in a bloodbath 😔. So I have decided to take my mind off the prices and focus on some coding instead. I noticed that some Steemians like to share some links to related posts at the end of their article. One of such Steemians whom I know of is @quotes-haven 😃. Here is an example from his recent post,

I think this is a really good idea but as I tried to do the same, I realized it is quite tedious to construct the list manually. Furthermore, instead of listing recent posts, I want to list related posts by tags instead. That made it even more challenging as some of such posts might be created way back.

Hence, the programmer in me decided to write a simple Python script to do the job 😆. And here are the codes,

from steem import Steem
import json, sys

if len(sys.argv) < 5:
    print('Insufficient arguments. Please run script with the following arguments without brackets: [author] [num_entries] [tag_filter] [link_limit]')
    exit()
author = sys.argv[1]
num_entries = int(sys.argv[2])
tag_filter = sys.argv[3]
link_limit = int(sys.argv[4])

def run():
    try:
        s = Steem()
        permlinks = []      
        data = s.get_blog_entries(author, 0, num_entries)
        for x in range(len(data)):
            if data[x]['author'] == author:
                permlinks.append(data[x]['permlink'])   
        print(json.dumps(permlinks, indent=4, sort_keys=True))

        filtered_list_by_tag = []

        for permlink in permlinks:
            data = s.get_content(author, permlink)
            metadata = json.loads(data['json_metadata'])
            if metadata['tags'].count(tag_filter) > 0:
                filtered_list_by_tag.append('- [' + data['root_title'] + '](https://steemit.com' +data['url'] + ')')
                if len(filtered_list_by_tag) == link_limit:
                    break
        link_file = open("links.txt", "w")
        for link in filtered_list_by_tag:
            line = link + '\n'
            link_file.write(line)
        link_file.close()
        print(json.dumps(filtered_list_by_tag, indent=4, sort_keys=True))

    except Exception as e:
        print(str(e))

if __name__ == '__main__':
    run()

To run the script, simply copy my code into a text file and save it as a Python file. The script takes in 4 arguments,

  • author - The author of the posts that you want build the links for.
  • num_entries - The number of entries in the blog that you want to search for. Maximum is 500 as limited by Steem API and one thing to note is that a resteem is also considered an entry.
  • tag_filter - The related tag that you want to use.
  • link_limit - The number of links that you want to build.

Here is an example of how to run the script (I named the script as steem_link_builder_test.py),

The script will then search through the most recent 500 entries in my blog and construct up to 10 links. The links will be written to a text file called links.txt which will be in the same folder as where you placed the script.

You can then simply copy those links and paste it to your article. Example below:

I am thinking of setting up a web service for Steemians to create the links. Or create a bot to post these links as comments if they subscribe to this service. Do you think it is a service which you will like to have? Let me know and thanks for reading!

steem-divider1.png

Projects/Services I am working on:


You can find me in these communities:

Sort:  

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

Award for the number of upvotes received
Award for the number of upvotes

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

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

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 58092.92
ETH 2616.63
USDT 1.00
SBD 2.43