I made a Python script that will post to my blog

I made a Python script that will post to my blog

5 minute read

I use Jekyll for my blog now, but the problem with that is I have to manually go into the posts directory and write my posts. It's not like WordPress or Drupal where I can just go to a webpage and get a WYSISYG editor and it posts to the blog. I have to put the file in the posts folder with the: layout type, name, title, time and tags. Then I need to build the blog site, then I can take the contents from the site folder and upload it to my webserver, well I use Git for that; however, I'm debating on using rsync because Git takes a little more space. I also don't really need the tracking because I keep a local copy on my laptop... although if I move to another machine it would be easy to:

 git pull 

Anyway, I made a script that accepts two arguments, the first argument is required. You pass in the location of the file you want to post, if it's in the current directory you just need to pass in the name. The second argument is optional, it's the location of your blog, it will test for a folder called _posts so you need to make sure that's in blog folder location.

This is a work in progress, there's some other things I want to add to it. And this is is version 1.0.

import sys
import os.path
import time
import datetime

#Print error and exit
def errorMessage(messsage):
    print("There was an error!")
    print("Error: ", messsage)

# This is for counting the args print(len(sys.argv))
if len(sys.argv) <= 1:
    errorMessage("You need to provide a file!")

fileName = (sys.argv[1])

#Test if file exists
if not os.path.isfile(fileName):
    errorMessage("File was not found!")

#Open file
fileOpen = open('%s' % fileName,'r')
fileRead = fileOpen.read()
fileRead = fileRead.split("\n")

#Replace all spaces in file name with dash and remove any periods
fileName2 = fileName.split(".")[0]
fileName2 = fileName.replace(" ","-")

#Set the post name
postName = str(datetime.date.today()) + '-' + str(time.strftime("%H-%M-%S")) + "-" + fileName2.split(".")[0] + ".html"
postTime = str(datetime.date.today()) + " " + str(time.strftime("%H-%M-%S"))

#Find tags in file
tagsInFile = ''
with open("%s" % fileName) as f:
    for line in f:
        if "tags:" in line:
             tagsInFile = line
if len(tagsInFile) > 0:
    tagsInFile = tagsInFile.split(":")[1]
#time to create the file
print("Post name " + postName)
print("File name2 " + fileName2)
fileWrite = open("%s" % postName, 'w+')
fileWrite.write("layout: post\n")
fileWrite.write("name: " + (fileName2.split(".")[0]).replace("-"," ") + "\n")
fileWrite.write("title: " + (fileName2.split(".")[0]).replace("-"," ") + "\n")
fileWrite.write("time: %s\n" % postTime)
#Test if there's tags and write them into the post
if len(tagsInFile) > 0:
    fileWrite.write("tags:" + tagsInFile + "\n")

#This will now write out the lines in the file, excluding tags
num = 0
for x in fileRead:
    if not "tags:" in fileRead[num]:
        fileWrite.write(x + "\n")
    num + 1

#Now it's time to move this file to the blog
if len(sys.argv) <= 2:
    errorMessage("No path to the blog provided. Will leave the post here.")

#Path provided, now test it
blogPath = sys.argv[2]
pathToSite = blogPath + "_posts"
if not os.path.isdir(pathToSite):
    errorMessage("The blog path provided doesn't exist.")

#Move the file
newName = pathToSite + "/" + postName
os.rename(postName, newName)
python, script, post, to, jekyll, blog