Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active December 31, 2016 14:54
Show Gist options
  • Save noqqe/7635337eb55e10cf3af9992101a3e26d to your computer and use it in GitHub Desktop.
Save noqqe/7635337eb55e10cf3af9992101a3e26d to your computer and use it in GitHub Desktop.
isso2yaml for markdown blogposts
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from datetime import datetime
import frontmatter
import glob
import codecs
import sqlite3 as lite
home = '/Users/noqqe/Code/noqqe.de/content/blog/'
db = '/Users/noqqe/comments.db'
files = glob.glob(home + '*.md')
con = lite.connect(db)
for f in files:
print f
with open(f, "r") as o:
post = frontmatter.loads(o.read())
cur = con.cursor()
try:
cur.execute('SELECT id from threads where title like "%s%%"' % post['title'])
tid = cur.fetchone()
except lite.OperationalError:
pass
try:
tid = tid[0]
except (TypeError, KeyError):
continue
cur = con.cursor()
cur.execute('SELECT author, created, text from comments where tid = %s' % tid)
comments = cur.fetchall()
c = []
for comment in comments:
if comment[0] is None:
author = "Anonymous"
else:
author = comment[0]
c.append({
'author': author,
'date': datetime.fromtimestamp(comment[1]).isoformat(),
'content': comment[2]
}
)
post['comments'] = c
with codecs.open(f, "wb+", 'utf8') as o:
o.write(frontmatter.dumps(post))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment