Guest User

Untitled

a guest
Feb 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. from pathlib import Path
  5. import datetime
  6. import frontmatter
  7.  
  8. posts_root = os.environ['HOME'] / Path('dev/brainblog/content/post')
  9.  
  10. for post in posts_root.iterdir():
  11. fname_date = post.name[0:10] # capture the "2018-02-08" "timestamp" from the post filename
  12. tstamp = datetime.datetime.strptime(fname_date, "%Y-%m-%d").timestamp()
  13. utc_time = datetime.datetime.utcfromtimestamp(tstamp)
  14. utc_string = utc_time.strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")
  15. with post.open('r') as f:
  16. post_fm = frontmatter.load(f)
  17. if not post_fm.get('date'):
  18. post_fm.__setitem__('date', utc_string)
  19. post_fm.__setitem__('modified', utc_string)
  20. post_str = frontmatter.dumps(post_fm)
  21. f.close()
  22.  
  23. with post.open('w') as f:
  24. f.write(post_str)
Add Comment
Please, Sign In to add comment