Last active
December 31, 2016 14:54
-
-
Save noqqe/7635337eb55e10cf3af9992101a3e26d to your computer and use it in GitHub Desktop.
isso2yaml for markdown blogposts
This file contains hidden or 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 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