Taking my mind off crypto prices with some coding - Related links builder with Python!
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:
- Essentia - Will it be the Mosaic/Netscape Navigator of web 3.0?
- Goldilock - Is it truly the right kind of storage?
- Random market thoughts and some updates
- I am running a service to convert Byteball to STEEM!
- Gaming Matters - STEEM is getting it's own collectible card game!
- CryptoTalk - How blockchain is going to change the way we operate
- Stay Safe - Important Security Update from Microsoft for MELTDOWN Vulnerability (KB4056892)
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!
Projects/Services I am working on:
You can find me in these communities:
Thanks for the script. Manage to get it working perfectly
"- Nagashima Resort in Nagoya - Nagashima Spaland Amusement Park",
"- Tokyo One Piece Tower - Theme park and Cafe",
"- Random Japan Photos #12 - Shops",
"- Comiket Cosplay at Tokyo Big Sight in Odaiba",
"- Restaurant Singapore Hainan Chi-fan in Tokyo",
"- Hundreds of Maneki Neko at Gotokuji Temple, Tokyo",
"- A trip to Kenrokuen Garden in Kanazawa",
"- Tips to survive the hot Japan summer - Part 2",
"- Tips to survive the hot Japan summer - Part 1",
"- Ichiran Ramen Japan \u4e00\u862d - A Tourists Favourite"
You are welcome. Glad that it helps!
Posted using Partiko Android
Cool man. Very helpful little tool it is! Guess many will find it handy if you make it into a service.
Thank you! Will certainly think about making it a service :)
Posted using Partiko Android
Automation is always good.. i think its a good idea.... if STEEMIT can have default features built-in, would be even better...
like if someone put something like $posturl_0 (is current URL) $posturl_1 (previous post URL) $posturl_2 (previous previous post) etc..
Thanks! Indeed. It will be great if it is a built-in function. This feature is pretty common on other blogging sites, so there is no reason why Steemit shouldn't have it.
can make this suggestion to some strong witness i guess... do you have a server you develop these "services" on? maybe we can form a Singapore build team.
@culgin, thanks for the tutorial and the great insights. I am not yet familiar with the language but I think python is the programming language I am going to be peeling by the end of this month.
Thanks for reading!
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