DEV Community

Cover image for Day 10 - Generate Dev.to post for 100 days challenge - 100 days 100 python scripts
Ganesh Raja
Ganesh Raja

Posted on

Day 10 - Generate Dev.to post for 100 days challenge - 100 days 100 python scripts

Day 10: dev_to_post_generate

This script uses a template and generates my 100 days python scripts posts for dev.to by checking the last updated file in Readme. This is helpful for me to quickly update dev.to post without manually copy pasting things again and again

import constants,os
import pyperclip
with open('README.md') as f:
    data=f.readlines()
last_updated_date=None

desc=""
for c in reversed(data):
    if "## Day " in c:
        last_updated_date=c.strip()
        break
    desc=c+desc

day,file_name=last_updated_date=last_updated_date[3:].split(":")

user_title=input("Please enter title for "+file_name.strip()+"\n")

title=day.title()+" - "+user_title
desc=desc[2:]

print(desc)
with open(os.path.join(constants.WORKING_DIR,file_name.strip())) as f:
    code= f.read()

with open("data/devto_post_template") as f:
    template=f.read()

template=template.replace("###code###", code)
template=template.replace("This script uses a template and generates my 100 days python scripts posts for dev.to by checking the last updated file in Readme. This is helpful for me to quickly update dev.to post without manually copy pasting things again and again", desc)
template=template.replace("Day 10 - Generate Dev.to post for 100 days challenge", title)
template=template.replace("Day 10", day.title())
template=template.replace("dev_to_post_generate", file_name.strip().replace(".py", ""))

pyperclip.copy(template)
Enter fullscreen mode Exit fullscreen mode

Please Visit my Git Repo to check out all the previous day challenges.

https://github.com/ganeshraja10/automated-python-scripts

Top comments (2)

Collapse
 
rpalo profile image
Ryan Palo

Hey Ganesh! This is a neat series! I love seeing the little applications.

Did you know that Python has its own little mini-templating functionality? Check out string.Template! :)

Collapse
 
ganesh profile image
Ganesh Raja

Thanks, Ryan. I will check it out. :)