Skip to content

Instantly share code, notes, and snippets.

@brainstorm
Created February 11, 2018 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainstorm/b19c737f1126631fc06866529c84100e to your computer and use it in GitHub Desktop.
Save brainstorm/b19c737f1126631fc06866529c84100e to your computer and use it in GitHub Desktop.
Fix posts metadata to migrate from Jekyll to Blogdown
#!/usr/bin/env python
import os
from pathlib import Path
import datetime
import frontmatter
posts_root = os.environ['HOME'] / Path('dev/brainblog/content/post')
for post in posts_root.iterdir():
fname_date = post.name[0:10] # capture the "2018-02-08" "timestamp" from the post filename
tstamp = datetime.datetime.strptime(fname_date, "%Y-%m-%d").timestamp()
utc_time = datetime.datetime.utcfromtimestamp(tstamp)
utc_string = utc_time.strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")
with post.open('r') as f:
post_fm = frontmatter.load(f)
if not post_fm.get('date'):
post_fm.__setitem__('date', utc_string)
post_fm.__setitem__('modified', utc_string)
post_str = frontmatter.dumps(post_fm)
f.close()
with post.open('w') as f:
f.write(post_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment