Skip to content

Instantly share code, notes, and snippets.

@pingswept
Created February 6, 2014 02:07
Show Gist options
  • Select an option

  • Save pingswept/8837195 to your computer and use it in GitHub Desktop.

Select an option

Save pingswept/8837195 to your computer and use it in GitHub Desktop.
The sorriest excuse for a Markdown file importer for Ghost that can be imagined.
def insert_post_into_database(list_of_posts):
import sqlite3
con = sqlite3.connect('ghost-dev.db')
cur = con.cursor()
for post in list_of_posts:
print(post)
cur.execute('INSERT INTO posts(uuid, title, slug, markdown, featured, page, status, language, author_id, created_at, created_by) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', post)
con.commit()
con.close()
# The parse_post function is designed to parse a Markdown file with a YAML header as is used by the Blogofile blog compiler.
# You'll probably want to rewrite it to handle your particular file arrangement.
def parse_post(file_handle):
import itertools, slugify, uuid, yaml
lines = file_handle.readlines()
chunks = [list(group) for k, group in itertools.groupby(lines, lambda x: x == '---\n') if not k]
print(chunks[0])
metadata = yaml.load("".join(chunks[0]))
title = metadata['title']
slug = str(slugify.slugify(title, max_length=30, word_boundary=True))
timestamp = metadata['date']
markdown_text = "".join(chunks[1])
return (str(uuid.uuid4()), title, slug, markdown_text, 0, 0, 'published', 'en_US', 1, timestamp, 1)
if __name__ == "__main__":
import os
files_in_dir = os.listdir('path/to/posts')
list_of_posts = []
for filename in files_in_dir:
print(filename)
with open('path/to/posts/' + filename, 'r') as postfile:
list_of_posts.append(parse_post(postfile))
#post = ('76de4d6b-e124-46c8-b3bd-95a24ca717ab', 'I like reading', 'this-is-not-a-pipe', '### Some markdown ### I like babies.', 0, 0, 'published', 'en_US', 1, 1390304939897, 1)
insert_post_into_database(list_of_posts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment