Skip to content

Instantly share code, notes, and snippets.

@lazypower
Created December 18, 2014 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lazypower/acf1ccbe8ae1416aaa76 to your computer and use it in GitHub Desktop.
Save lazypower/acf1ccbe8ae1416aaa76 to your computer and use it in GitHub Desktop.
A quick and dirty data munger/parser for migrating from ghost to raw markdown
#!/usr/bin/env python
import json
import os
import sys
import datetime
with open("old.json", "r") as f:
raw = f.read()
old = json.loads(raw)
posts = old["db"][0]["data"]["posts"]
postcount = len(posts)
# Data Munging
tags = {}
for item in old["db"][0]["data"]["tags"]:
tags[item["id"]] = item["slug"]
post_tags = {}
for item in old["db"][0]["data"]["posts_tags"]:
post_tags.setdefault(item["post_id"], []).append(tags[item["tag_id"]])
# Kickoff the processing
print("Found %s posts" % postcount)
if not os.path.exists('content'):
print("I don't think we're in the pelican directory. Exiting")
sys.exit(1)
def find_post_tags(postid):
if post_tags.has_key(postid):
return ", ".join(post_tags[postid])
return "untagged"
for post in posts:
date = datetime.datetime.fromtimestamp(post["created_at"] / 1e3)
filename = "{}-{}.md".format(date.strftime("%Y-%m-%d"), post["slug"])
print "Processing {}".format(filename)
with open("content/{}".format(filename), 'w+') as f:
f.write("Title: {}\n".format(post['title']))
f.write("Date: {}\n".format(date.strftime("%Y-%m-%d %H:%m")))
f.write("Tags: {}\n".format(find_post_tags(post["id"])))
f.write("Slug: {}\n".format(post['slug']))
f.write("---\n")
f.write("{}".format(post['markdown'].encode('utf8')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment