Created
February 11, 2018 00:26
-
-
Save brainstorm/b19c737f1126631fc06866529c84100e to your computer and use it in GitHub Desktop.
Fix posts metadata to migrate from Jekyll to Blogdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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