Skip to content

Instantly share code, notes, and snippets.

@daisyUniverse
Last active April 22, 2025 18:34
Show Gist options
  • Save daisyUniverse/da5266c7664a5448f88bdd6b80153f43 to your computer and use it in GitHub Desktop.
Save daisyUniverse/da5266c7664a5448f88bdd6b80153f43 to your computer and use it in GitHub Desktop.
BlogSync - a service + timer + script combo that periodically reads content from a folder, and then converts it to Jekyll formatting (changing the name, adding the file headers) and writes it to the appropriate blogs _posts folder
[Unit]
Description=Run a script to sync blog directory from NextCloud to my Jekyll blogs
After=network.target
[Service]
Type=simple
WorkingDirectory=/home/daisy/blogsites
ExecStart=/usr/bin/python3 /home/daisy/blogsites/jekyllformatter.py
[Install]
WantedBy=timers.target
[Unit]
Description=Timer for the BlogSync service
[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
Unit=blogsync.service
[Install]
WantedBy=timers.target
# Jekyll Formatter
# Converts normal .MD files into Jekyll formatted files
# Daisy Universe [D]
# 04 . 22 . 25
import os.path
import time
from datetime import datetime
from pathlib import Path
src = Path("/mnt/blogs/")
dest = "/home/daisy/blogsites/"
pathlist = src.glob('**/*.md')
for path in pathlist:
# Generate a new Jekyll friendly name based on the file creation date
pathstr = str(path)
timeCreated = datetime.fromtimestamp(os.path.getctime(pathstr))
dateCreated = str(datetime.strftime(timeCreated, "%Y-%m-%d")).split(" ")[0]
newname = ( dateCreated + "-" + pathstr.split("/")[-1].replace(" ","-") )
section = pathstr.split("/")[-2]
print( "[" + section + "] " + newname)
print("SOURCE: " + pathstr)
# Read the file and modify it to be Jekyll friendly
title = pathstr.split("/")[-1].replace(".md", "")
with open(pathstr, "r+") as fp:
lines = fp.readlines()
lines.insert(0, "---\n\n")
lines.insert(0, "layout: post\n")
lines.insert(0, ("title: " + title + "\n"))
lines.insert(0, "---\n")
fp.seek(0)
output = lines
# Write new data to target path
newpath = ( dest + section + "/_posts/" + newname)
print("DESTINATION: " + newpath + "\n")
with open(newpath, "w+") as fp:
fp.writelines(output)
os.chmod(newpath, 0o644)
os.chown(newpath, 1000, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment